Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:33:01+00:00 2026-05-29T09:33:01+00:00

I am following this great article on how to create a Provider framework in

  • 0

I am following this great article on how to create a Provider framework in .NET

Basically, this article explains greatly how to end up with a configuration file like the following:

   <configuration>
     <configSections>
        <section name="data" type="DataProviderConfigurationSection" />
      </configSections>
      <data defaultProvider="MyDataProvider">
         <providers>
            <add name="MydataProvider" type="MyDataProvider"  />
         </providers>
      </data>
   </configuration>

Where the <add/> element allows you to define a provider.

However, I would like to know how to extend the add entry with custom attributes.

For example:

<providers>
  <add name="MydataProvider" type="MyDataProvider" myProperty="myValue" myProperty2="myValue2" ... />
</providers>

Any help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:33:01+00:00Added an answer on May 29, 2026 at 9:33 am

    Here is what I finally found. This is a very specific question about extending the element with more attributes and how to handle them when implementing a Provider Framework. All the answers about custom configuration sections are OK but not addressing the original question.

    If you need to implement a custom Provider, like the MembershipProvider, but for your own purpose, you need to definitely read this article: Creating Your Own Provider Framework

    It is excellent reading. Now if you need to extend the element with your own attributes, here is what you need to change…

    1) Next is the code discussed in the article (There might be some adaptations):

    using System;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration.Provider;
    using System.Collections.Specialized;
    
    
        public abstract class DataProvider : ProviderBase
        {
            // Define the methods to be used by the provider.  These are custom methods to your own provider.
            public abstract void Get();
            public abstract void Delete();
        }
    
        public class DataProviderCollection : ProviderCollection { }
    
    
    
    
        //The name is typically the same as the abstract class, minus the Provider part. Sticking to our (fake) example. we'd have a static class called Data.
        public static class Data
        {
            private static bool _isInitialized = false;
    
            private static DataProvider _provider;
            public static DataProvider Provider
            {
                get
                {
                    Initialize();
                    return _provider;
                }
            }
    
            private static DataProviderCollection _providers;
            public static DataProviderCollection Providers
            {
                get
                {
                    Initialize();
                    return _providers;
                }
            }
    
            private static void Initialize()
            {
                DataProviderConfigurationSection dataConfig = null;
    
                if (!_isInitialized)
                {
                    // get the configuration section for the feature
                    dataConfig = (DataProviderConfigurationSection)ConfigurationManager.GetSection("data");
    
                    if (dataConfig == null)
                    {
                        throw new ConfigurationErrorsException("Data is not configured to be used with this application");
                    }
    
                    _providers = new DataProviderCollection();
    
                    // use the ProvidersHelper class to call Initialize() on each provider
                    ProvidersHelper.InstantiateProviders(dataConfig.Providers, _providers, typeof(DataProvider));
    
                    // set a reference to the default provider
                    _provider = _providers[dataConfig.DefaultProvider] as DataProvider;
    
                    _isInitialized = true;
                }
            }
    
            public static void Get()
            {
                Initialize();
                if (_provider != null)
                {
                    _provider.Get();
                }
            }
    
            public static void Delete()
            {
                Initialize();
                if (_provider != null)
                {
                    _provider.Delete();
                }
            }
        }
    
        public class MyDataProvider : DataProvider
        {
    
    
    
    
            public override void Get()
            {
                // Get Code
            }
    
            public override void Delete()
            {
                // Delete Code
            }
        }
    
        public class DataProviderConfigurationSection : ConfigurationSection
        {
            public DataProviderConfigurationSection()
            {
                _defaultProvider = new ConfigurationProperty("defaultProvider", typeof(string), null);
                _providers = new ConfigurationProperty("providers", typeof(ProviderSettingsCollection), null);
                _properties = new ConfigurationPropertyCollection();
    
                _properties.Add(_providers);
                _properties.Add(_defaultProvider);
            }
    
            private readonly ConfigurationProperty _defaultProvider;
            [ConfigurationProperty("defaultProvider")]
            public string DefaultProvider
            {
                get { return (string)base[_defaultProvider]; }
                set { base[_defaultProvider] = value; }
            }
    
            private readonly ConfigurationProperty _providers;
            [ConfigurationProperty("providers")]
            public ProviderSettingsCollection Providers
            {
                get { return (ProviderSettingsCollection)base[_providers]; }
            }
    
            private ConfigurationPropertyCollection _properties;
            protected override ConfigurationPropertyCollection Properties
            {
                get { return _properties; }
            }
        }
    
        public static class ProvidersHelper
        {
            private static Type providerBaseType = typeof(ProviderBase);
    
            /// <summary>
            /// Instantiates the provider.
            /// </summary>
            /// <param name="providerSettings">The settings.</param>
            /// <param name="providerType">Type of the provider to be instantiated.</param>
            /// <returns></returns>
            public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
            {
                ProviderBase base2 = null;
                try
                {
                    string str = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();
                    if (string.IsNullOrEmpty(str))
                    {
                        throw new ArgumentException("Provider type name is invalid");
                    }
                    Type c = Type.GetType(str, true, true);
                    if (!providerType.IsAssignableFrom(c))
                    {
                        throw new ArgumentException(String.Format("Provider must implement type {0}.", providerType.ToString()));
                    }
                    base2 = (ProviderBase)Activator.CreateInstance(c);
                    NameValueCollection parameters = providerSettings.Parameters;
                    NameValueCollection config = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
                    foreach (string str2 in parameters)
                    {
                        config[str2] = parameters[str2];
                    }
                    base2.Initialize(providerSettings.Name, config);
                }
                catch (Exception exception)
                {
                    if (exception is ConfigurationException)
                    {
                        throw;
                    }
                    throw new ConfigurationErrorsException(exception.Message,
                        providerSettings.ElementInformation.Properties["type"].Source,
                        providerSettings.ElementInformation.Properties["type"].LineNumber);
                }
                return base2;
            }
    
            public static void InstantiateProviders(ProviderSettingsCollection providerSettings, ProviderCollection providers, Type type)
            {
                foreach (ProviderSettings settings in providerSettings)
                {
                    providers.Add(ProvidersHelper.InstantiateProvider(settings, type));
                }
            }
        }
    

    2) This is the config file that you use for the above code:

      <configuration>
        <configSections>
          <section name="data" type="DataProviderConfigurationSection" />
        </configSections>
        <data defaultProvider="MyDataProvider">
          <providers>
            <add name="MydataProvider" type="MyDataProvider"  />
          </providers>
        </data>
      </configuration>
    

    3) Now, here is what you need to modify in order to use read the attributes in the <add> element in the configuration file.

        public abstract class DataProvider : ProviderBase
        {
    
            public string MyAttribute1 { get; set; }
            public string MyAttribute2 { get; set; }
            public string MyAttribute3 { get; set; }
    
            // Define the methods to be used by the provider.  These are custom methods to your own provider.
            public abstract void Get();
            public abstract void Delete();
    
            public override void Initialize(string name, NameValueCollection config)
            {
    
                MyAttribute1 = config["MyAttribute1"];
                MyAttribute2 = config["MyAttribute2"];
                MyAttribute3 = config["MyAttribute3"];
    
                base.Initialize(name, config);
            }
        }
    

    4) The configuration file looks like this:

      <configuration>
        <configSections>
          <section name="data" type="DataProviderConfigurationSection" />
        </configSections>
        <data defaultProvider="MyDataProvider">
          <providers>
            <add name="MydataProvider" type="MyDataProvider" MyAttribute1="MyValue1" MyAttribute2="MyValue2"   />
          </providers>
        </data>
      </configuration>
    

    And as a bonus, here is a Unit test to validate it works:

    [TestMethod]
    public void RunMyDataProviderTest()
            {
                DataProvider dataProvider = Data.Provider;
    
                Assert.IsInstanceOfType(dataProvider, typeof(MyDataProvider));
    
                Assert.AreEqual(dataProvider.MyAttribute1, "MyValue1");
                Assert.AreEqual(dataProvider.MyAttribute2, "MyValue2");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using the following jQuery plugin: jQuery OEmbed. This plugin works great in
Following this question: Good crash reporting library in c# Is there any library like
Following this example, I can list all elements into a pdf file import pyPdf
So I am following this guide: http://technotes.1000lines.net/?p=23 and I am going through the steps.
I'm following this guide: http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_01/programs.html and I'm trying to create my first ruby program.
After reading the tips from this great Nettuts+ article I've come up with a
I am following this tutorial: https://github.com/amatsuda/kaminari/wiki/How-To:-Create-Infinite-Scrolling-with-jQuery So far everything works great, but I am
I have a many-to-many model, following the example in this great railscast My model
I am following this great post to configure my rails production server on ubuntu
I'm following this article on how to get the battery status on a windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.