Jeff's Junk

The sillynonsense and .NET musings of Jeff Putz

News

My Sites

Populating a ProviderCollection from custom config in beta 2

I'm a little lost. I'm not clear on how to populate a ProviderCollection based on a custom configuration section. The way I first learned it (pre-beta) isn't possible anymore, and of course all of the docs appear to be placeholders.

For example, in web.config, I have something like this:

<mySection defaultProvider="PopForums">
        <providers>
            <add name="MyProvider" type="MyProvider, MyAssy" connectionStringName="Whatever" />
        </providers>
</mySection>


Then my custom config section looks something like:

public class MySection : ConfigurationSection
{
   [ConfigurationProperty("defaultProvider", DefaultValue = "Whatevah")]
   public string DefaultProvider
   {
      get { return (string)base["defaultProvider"]; }
      set { base["defaultProvider"] = value; }
   }

   [ConfigurationProperty("providers", RequiredValue = false)]
   public ProviderCollection Providers
   {
       get { return (ProviderCollection)base["providers"]; }
   }
}


Now how do I get this stuff initialized? The code I'm revisiting from a year ago doesn't even compile because the classes I used, some of them anyway, are gone. I can't find any newer examples.
Posted: Jul 24 2005, 08:30 PM by Jeff | with 2 comment(s)
Filed under:

Comments

Matt Berther said:

I have an article posted that has essentially the same configuration section [1].

To actually instatiate the providers I do something like this in my static class.

class MyProvider
{
private static bool isInitialized;
private static void Initialize()
{
if (!MyProvider.isInitialized)
{
ProviderSection config = ConfigurationManager.GetSection("myProvider") as ProviderSection;
if (config.DefaultProvider == null)
{
throw new ProviderException("Provider not initialized.");
}

MyProvider.providers = new MyProviderCollection();
ProvidersHelper.InstantiateProviders(config.Providers, MyProvider.providers, typeof(MyProviderBase));

MyProvider.provider = MyProvider.providers[config.DefaultProvider];
if (MyProvider.provider == null)
{
throw new ConfigurationErrorsException("Default provider not found.",
config.ElementInformation.Properties["defaultProvider"].Source,
config.ElementInformation.Properties["defaultProvider"].LineNumber);
}

MyProvider.providers.SetReadOnly();
MyProvider.isInitialized = true;
}
}
}

Hope this helps!

[1] http://www.mattberther.com/2005/06/000637.html
# July 24, 2005 10:14 PM

Jeff said:

Update... I figured out what I really wanted to do. Will post later.
# July 26, 2005 1:09 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)