472,958 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1882
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.