473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

.NET 2.0 Configuration (attempt 2)

I was basically wanting to know how to use the System.Configuration
namespace to be able to load an arbitrary number of unknown attributes on an
element in a custom section in the .config into a NameValueCollection.

Basically I want to do something similiar to what occurs with a
System.Configuration.Provider.ProviderBase Initialise method - the provider
is passed a NameValueCollection containing attribute names and their
associated values from the .config. see...

http://msdn2.microsoft.com/en-us/lib...nitialize.aspx

All the examples I've seen with the .NET 2.0 config api deal with scenarios
where the attributes are predefined and loaded using static code.

One brute force solution that comes to mind is just to by-pass the
Configuration api and use XML/DOM to parse the web config . But if there's
a more elegant solution that leverages the .NET Config API I'd like to use
it.

I've already asked this question once, I had one response which in summary
suggested I should use the System.Configuration Namespace.... not exactly
helpful.

Thanks in advance

Michael
Feb 1 '07 #1
4 1899
ProviderBase is an abstract class, meaning that it is not used; only derived
classes are used. A Configuration Section is serialized class data. As such,
the class which reads/deserializes the Configuration Section must know what
to expect in the attributes. In this case, the class must know the names,
types, and acceptable values of each Name/Value in the collection. It is
because ProviderBase is abstract that it does not define what attributes
there should be, what types they are, or what the acceptable values are.

..Net Framework 2.0 Configuration is strongly-typed, and for good reasons,
beyond the scope of this answer. For a more accurate idea of an
implementation of ProviderBase, see the documentation for the
SqlMembershipProvider class, which is derived from ProviderBase:

http://msdn2.microsoft.com/en-us/lib...pprovider.aspx

As you will see, it is strongly-typed, and knows exactly what attributes it
is looking for.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

"Michael Lang" <micklang at gmail.comwrote in message
news:uu**************@TK2MSFTNGP05.phx.gbl...
>I was basically wanting to know how to use the System.Configuration
namespace to be able to load an arbitrary number of unknown attributes on
an element in a custom section in the .config into a NameValueCollection.

Basically I want to do something similiar to what occurs with a
System.Configuration.Provider.ProviderBase Initialise method - the
provider is passed a NameValueCollection containing attribute names and
their associated values from the .config. see...

http://msdn2.microsoft.com/en-us/lib...nitialize.aspx

All the examples I've seen with the .NET 2.0 config api deal with
scenarios where the attributes are predefined and loaded using static
code.

One brute force solution that comes to mind is just to by-pass the
Configuration api and use XML/DOM to parse the web config . But if
there's a more elegant solution that leverages the .NET Config API I'd
like to use
it.

I've already asked this question once, I had one response which in summary
suggested I should use the System.Configuration Namespace.... not exactly
helpful.

Thanks in advance

Michael


Feb 1 '07 #2
Hi Michael,

I do not think it is possible to use unknown attributes because you have to
derive from ConfigurationElement and provide information about the expected
attributes. But maybe the following example may be an option for you?

<configuration>
<configSections>
<section name="testaddins"
type="WindowsApplication1.AddinConfigurationSectio n,WindowsApplication1" />
</configSections>

<testaddins>
<addins>
<addin path="1">
<addinproperties>
<property name="test1" value="a" />
<property name="test2" value="b" />
</addinproperties>
</addin>
<addin path="2">
<addinproperties>
<property name="test3" value="c" />
<property name="test4" value="d" />
</addinproperties>
</addin>
</addins>
</testaddins>
</configuration>

In this example I have used subelements (<property>) to describe arbitrary
data for each addin instead of attributes as you suggested. If this might
solve your problem please let med know and I would be glad to provide you
code to read this configuration section.

HTH, Jakob.
--
http://www.dotninjas.dk

"Michael Lang" wrote:
I was basically wanting to know how to use the System.Configuration
namespace to be able to load an arbitrary number of unknown attributes on an
element in a custom section in the .config into a NameValueCollection.

Basically I want to do something similiar to what occurs with a
System.Configuration.Provider.ProviderBase Initialise method - the provider
is passed a NameValueCollection containing attribute names and their
associated values from the .config. see...

http://msdn2.microsoft.com/en-us/lib...nitialize.aspx

All the examples I've seen with the .NET 2.0 config api deal with scenarios
where the attributes are predefined and loaded using static code.

One brute force solution that comes to mind is just to by-pass the
Configuration api and use XML/DOM to parse the web config . But if there's
a more elegant solution that leverages the .NET Config API I'd like to use
it.

I've already asked this question once, I had one response which in summary
suggested I should use the System.Configuration Namespace.... not exactly
helpful.

Thanks in advance

Michael
Feb 1 '07 #3
I had thought my solution might be to derive my configured class from
ProviderBase.

My class is also an abstract class, and it is designed to be inherited and
allow deriving classes to configure themselves by adding attributes to the
config. However I was wanting to use this class for both ASP.NET and other
project types.

Whilst ProviderBase resides in the System.Configuration.Provider namespace
it talks about ASP.NET. Plus I haven't seen any examples of how you go
about using the ProviderBase without the
System.Web.Configuratoin.ProviderHelper.

This seems like a bit of a mess to me. In my mind there is no way you
should have things in System.Configuration.Provider having dependencies on
things in System.Web.Configuration.

Do you know of sample code that uses ProviderBase for a custom configuration
for something other than ASP.NET ?

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
ProviderBase is an abstract class, meaning that it is not used; only
derived classes are used. A Configuration Section is serialized class
data. As such, the class which reads/deserializes the Configuration
Section must know what to expect in the attributes. In this case, the
class must know the names, types, and acceptable values of each Name/Value
in the collection. It is because ProviderBase is abstract that it does not
define what attributes there should be, what types they are, or what the
acceptable values are.

.Net Framework 2.0 Configuration is strongly-typed, and for good reasons,
beyond the scope of this answer. For a more accurate idea of an
implementation of ProviderBase, see the documentation for the
SqlMembershipProvider class, which is derived from ProviderBase:

http://msdn2.microsoft.com/en-us/lib...pprovider.aspx

As you will see, it is strongly-typed, and knows exactly what attributes
it is looking for.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

"Michael Lang" <micklang at gmail.comwrote in message
news:uu**************@TK2MSFTNGP05.phx.gbl...
>>I was basically wanting to know how to use the System.Configuration
namespace to be able to load an arbitrary number of unknown attributes on
an element in a custom section in the .config into a NameValueCollection.

Basically I want to do something similiar to what occurs with a
System.Configuration.Provider.ProviderBase Initialise method - the
provider is passed a NameValueCollection containing attribute names and
their associated values from the .config. see...

http://msdn2.microsoft.com/en-us/lib...nitialize.aspx

All the examples I've seen with the .NET 2.0 config api deal with
scenarios where the attributes are predefined and loaded using static
code.

One brute force solution that comes to mind is just to by-pass the
Configuration api and use XML/DOM to parse the web config . But if
there's a more elegant solution that leverages the .NET Config API I'd
like to use
it.

I've already asked this question once, I had one response which in
summary suggested I should use the System.Configuration Namespace.... not
exactly helpful.

Thanks in advance

Michael



Feb 3 '07 #4
OK for anyone out there I can spare some pain, I've worked a solution out.

---------------------------------------------------------------------------------------------------------
// CustomSection...

public class CustomConfig : ConfigurationSection
{
/// <summary>
/// Provider section configuration
/// </summary>
public const string ProviderName = "Provider";

/// <summary>
/// Gets the configuration settings for the <see
cref="YourCustomProvider"/>
/// </summary>
[ConfigurationProperty(ProviderName, IsRequired = true)]
public ProviderSettings ProviderConfig
{
get
{
return (ProviderSettings)this[ProviderName];
}
}
}

// Config...

<mysection>
<subsection>
<Provider name="YourTestProvider"
type="YourAssembly.UnitTests.TestProvider, YourNamespace.YourCustomProvider,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</subsection>
</mysection>

/* Most examples out there deal with a collection of providers. For my
needs I only need a single provider, so if you need a collection you can
adapt this code by substituting ProviderSettings with
ProviderSettingsCollection, then you'll need to loop through this collection
of ProviderSettings applying the code below to each member of the
collection.

I think an example for configuring single provider is probably quite useful
to any people out there who don't need or want a collection of providers for
there architecture...

The following code is basically a substitute for the
System.Web.Configuration.ProvidersHelper.
*/

YourCustomProvider providerInstance;
CustomConfig retConfig =
(CustomConfig)System.Configuration.ConfigurationMa nager.GetSection("mysection/subsection");
if (retConfig != null && retConfig.ProviderConfig != null)
{
ProviderSettings providerSettings = retConfig.ProviderConfig;
Type providerType = Type.GetType(providerSettings.Type);
providerInstance = Activator.CreateInstance(providerType) as
YourCustomProvider;
providerInstance.Initialize(providerSettings.Name,
providerSettings.Parameters);
}

---------------------------------------------------------------------------------------------------------

In my view the whole ProviderBase thing is dire need of refactoring.
ProvidersHelper shouldn't be in System.Web.Configuration. You could also
argue that all the ProviderSettings and ProviderSettingsCollection in
System.Configuration should be in the System.Configuration.Provider
namespace.

Maybe I'm getting lazy but I think the Provider related stuff is spread
across too many namespaces, making it unnecessarily confusing.

Michael
"Michael Lang" <micklang at gmail.comwrote in message
news:OY**************@TK2MSFTNGP02.phx.gbl...
>I had thought my solution might be to derive my configured class from
ProviderBase.

My class is also an abstract class, and it is designed to be inherited and
allow deriving classes to configure themselves by adding attributes to the
config. However I was wanting to use this class for both ASP.NET and
other project types.

Whilst ProviderBase resides in the System.Configuration.Provider namespace
it talks about ASP.NET. Plus I haven't seen any examples of how you go
about using the ProviderBase without the
System.Web.Configuratoin.ProviderHelper.

This seems like a bit of a mess to me. In my mind there is no way you
should have things in System.Configuration.Provider having dependencies on
things in System.Web.Configuration.

Do you know of sample code that uses ProviderBase for a custom
configuration for something other than ASP.NET ?

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>ProviderBase is an abstract class, meaning that it is not used; only
derived classes are used. A Configuration Section is serialized class
data. As such, the class which reads/deserializes the Configuration
Section must know what to expect in the attributes. In this case, the
class must know the names, types, and acceptable values of each
Name/Value in the collection. It is because ProviderBase is abstract that
it does not define what attributes there should be, what types they are,
or what the acceptable values are.

.Net Framework 2.0 Configuration is strongly-typed, and for good reasons,
beyond the scope of this answer. For a more accurate idea of an
implementation of ProviderBase, see the documentation for the
SqlMembershipProvider class, which is derived from ProviderBase:

http://msdn2.microsoft.com/en-us/lib...pprovider.aspx

As you will see, it is strongly-typed, and knows exactly what attributes
it is looking for.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

"Michael Lang" <micklang at gmail.comwrote in message
news:uu**************@TK2MSFTNGP05.phx.gbl...
>>>I was basically wanting to know how to use the System.Configuration
namespace to be able to load an arbitrary number of unknown attributes on
an element in a custom section in the .config into a NameValueCollection.

Basically I want to do something similiar to what occurs with a
System.Configuration.Provider.ProviderBase Initialise method - the
provider is passed a NameValueCollection containing attribute names and
their associated values from the .config. see...

http://msdn2.microsoft.com/en-us/lib...nitialize.aspx

All the examples I've seen with the .NET 2.0 config api deal with
scenarios where the attributes are predefined and loaded using static
code.

One brute force solution that comes to mind is just to by-pass the
Configuration api and use XML/DOM to parse the web config . But if
there's a more elegant solution that leverages the .NET Config API I'd
like to use
it.

I've already asked this question once, I had one response which in
summary suggested I should use the System.Configuration Namespace....
not exactly helpful.

Thanks in advance

Michael




Feb 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anthony J Biondo Jr | last post by:
Hi Everyone: We are trying to configure our Intranet to be able to support ASP.net Web Applications. We are Running Windows 2000 with Application Center on 2 boxes. When we check our applications...
7
by: Richard Kerr | last post by:
Hi all, Have a look at this config file structure: <appSettings> <add key="connstr" value="workstation id=test;packet size=4096;user id=sa;data source=test;persist security info=False;initial...
1
by: Nick | last post by:
Can someone please explain why? In VS2005, I created a starter kit personal website. Then used web configuration tool to setup user / roles etc (just like it says to in the welcome screen)... ...
0
by: ben | last post by:
I've been trying to get this to work. Using the June 2005 release under Framework 1.1. I first set up the app.config so that my settings get saved to the registry. That works, so I know I'm...
2
by: UJ | last post by:
How do you add a new value to a configuration file through the configuration block through code? When I try and add a new value I get an exception. TIA - Jeff.
7
by: Aaron Gray | last post by:
Hi, I am having problems making Apache reload itself. I have tried a PHP exec calling a bash shell script that does a KILL -HUP on Apache to attempt to make it reload its configuration. The...
0
by: =?Utf-8?B?U3VzYW4=?= | last post by:
We are trying to move to a 64 bit architecture, but I'm still having some issues. I have the code with the 32 bit version of Oracle working fine on a 64 bit computer with Windows Server 2003 (also...
6
by: Jonathan Wood | last post by:
Okay, I've been through this a couple of times now but it's still not coming together. I have an existing Website that I need to add ASP.NET membership functionality to. So, after manually...
0
by: kamalkgarg | last post by:
I am having vnc-server-4.0-0.beta4.1.6 installed on RHEL3.0. I was often being reported about the following error while connecting to VNC "Connection failed: Too many security failures." After...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.