locked
Unable to create and use custom settings provider RRS feed

  • Question

  • Hi guys,

    I'm trying to create custom settings provider and it doesn't go well. I've already spent five hours on it with no luck :( In few words my goal is to store settings alongside with the binary instead of having per-user settings that will be stored in local user's folder. I'm using VS2010 on Win7 x64 if that matters. Code of my class is very simple. I'm testing it in console application (.Net 3.5). It looks something like this:

      public class MyProvider : SettingsProvider
      {
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
          var result = new SettingsPropertyValueCollection();
          foreach (SettingsProperty property in collection)
          {
            Debug.WriteLine(property.ToString());
          }
    
          return result;
        }
    
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
          Debug.WriteLine(collection.Count);
        }
    
        public override string ApplicationName
        {
          get
          {
            return "MyApp";
          }
          
          set { }
        }
      }
    

    I have only one setting field. Here is content of .setting file

    <?xml version='1.0' encoding='utf-8'?>
    <SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="console.Properties" GeneratedClassName="Settings">
     <Profiles />
     <Settings>
      <Setting Name="Test" Provider="console.MyConfig" Type="System.String" Scope="User">
       <Value Profile="(Default)" />
      </Setting>
     </Settings>
    </SettingsFile>
    

    and finally here is code that is manipulating with settings

    Settings.Default.Test = "aaa";
    Settings.Default.Save();
    
    

    I'm expecting two things to happen here - call to MyProvider.GetPropertyValues() and then MyProvider.SetPropertyValues(). Unfortunately it doesn't happen because  SettingsPropertyNotFoundException ("Value cannot be null. Parameter name: name") is thrown in Settings.Designer.cs. Source code is bellow. I marked line where exception happens:

    namespace console.Properties {
      
      
      [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
      [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
      internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
        
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
        
        public static Settings Default {
          get {
            return defaultInstance;
          }
        }
        
        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Configuration.SettingsProviderAttribute(typeof(console.MyProvider))]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public string Test {
          get {
            return ((string)(this["Test"]));
          }
          set {
            this["Test"] = value; // EXCEPTION IS THROWN HERE
          }
        }
      }
    }
    

    No calls to MyPrivader were made at all.

    Any ideas what I'm doing wrong?

    Friday, July 16, 2010 5:19 AM

Answers

All replies

  • Hi,

    This article shows a simple demo about how to write and use custom settings provider, is it what you are looking for?


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, July 19, 2010 6:26 AM
  • I started with this article. It comes up on top of Google search. But it's not what I'm looking for. I'm looking for an answer why I'm getting an exception when I specify custom settings provider. It seems to fail regardless of provider's implementation. It doesn't even get to the point when provide's code is executed.
    Monday, July 19, 2010 6:01 PM
  • I figured it out. I have to explicitly override and call ProviderBase.Initialize(string name, NameValueCollection config) in my class.

    Thank you guys.

    • Marked as answer by ruslanv Monday, July 19, 2010 7:27 PM
    Monday, July 19, 2010 7:27 PM
  • Hey ruslanv,

    I am facing the same problem. Can you explain your solution a bit more, please?

    Thank you!

    Monday, May 2, 2011 11:05 PM
  • Hi ruslanv and all,

    How do you initialize this type of personal SettingsProvider into your Main() function of Program.cs ?

    As you did, I've created my own PrefSettingProvider at my namespace level (called TestApp),

    I've updated the autogenerated partial Settings class (Settings.cs) according my personal needs but still getting a TypeInitializationException exception when calling Settings.Default?

    I've tried also to create another class and created a constructor that returns Settings.Default and tried to call it directly at runtime but getting the same Exception.

    Thanks a lot for your help,

    Richard

     

    Thursday, September 1, 2011 2:56 PM
  • I know this is a very old post, however I'm wondering how you solved this. I'm trying to do the same thing but I am struggeling to get my SettingsProvider to be initialized.

    Can you share some code?

    Thanks!

    SB

    Sunday, July 20, 2014 9:30 PM