2

I am building a class library and I am wondering what is the best way to make sure some settings are in the app.config/webconfig.

The class library needs a connection string and an app key.

I know with nuget you can like inject settings into the config files but I am not using nuget so wondering if anyone has any ideas, or do I have to just do try catches and see if those settings are in the config files?

chobo2
  • 83,322
  • 195
  • 530
  • 832
  • 1
    How about check if the settings are present, and then throw exceptions if they are not. – Adriaan Stander Sep 26 '12 at 18:31
  • You could turn the required settings into constructor parameters. – cadrell0 Sep 26 '12 at 18:32
  • @cadrell0 - I was thinking this but the code I am looking at(that I want to move into a library) is all static(so I guess I could have a static constructor??) – chobo2 Sep 26 '12 at 18:34
  • Static constructors cannot have parameters, you would have to have some sort of `Initialize` method. I try to avoid this when possible. Are the methods that use the settings static? Or is it just that they use static fields? This starts to get a little off topic, but why are they static? – cadrell0 Sep 26 '12 at 18:37
  • Hmm ya thought static constructor would not work. I did not write the code(I am trying to take the code and make it in a library before the copy and paste button comes out and it is in 10 different projects). I think it just static to save from making an object but I am not 100% sure yet. – chobo2 Sep 26 '12 at 18:40
  • "I think it just static to save from making an object". I wouldn't use static in this case. Make the objects. It will make life easier in multiple ways. – cadrell0 Sep 26 '12 at 18:42
  • 2
    "Yeah" is actually spelled ["yeah"](http://www.merriam-webster.com/dictionary/yeah) by the way, not "ya" unless you're Yiddish, or trying to express incredulity at Captain Obvious, as in "Ya think?" – Robert Harvey Sep 26 '12 at 18:52
  • 1
    @RobertHarvey you might be my hero. – cadrell0 Sep 26 '12 at 19:46

3 Answers3

1

You can choose between different kind of solutions:

  1. You can say that giving you the required information, is the user responsibility. so you just make sure that when the user creates instance of your object, it gives you the required information. That's pretty good option for class-libraries. you don't want to tell who-ever is going to use your code, how to manage his settings. Let's say he has his own way to save configuration - why do you have to be tied for specific configuration type (System.Configuration).

  2. If you insist on using the app.config/web.config, you can just have one for your assembly, and not use the application main one. you can use ConfigurationManager.OpenExeConfiguration. to know where your assembly is running from, use Assembly.GetExecutingAssembly().Location. This way, you can open your own configuration file, and just make sure in the documenation that the user must update the settings.

  3. you can just tell him that he have to specify the configuration info in his app.config/web.config, and throw exception if the key your are looking for doesn't exists...

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Shahar Gvirtz
  • 2,418
  • 1
  • 14
  • 17
0

Try this

http://ninjectconfigurer.codeplex.com/

After clear reading of the question ,the above link won't work but The following solution might work

Invalid or erroneous setting is really a compile time issue. You can use msbuild script to validate if the app.config file has mandatory settings. If not then an error should be raised while compiling.

To read the contents of a config file use Xmlread msbuild task - Read Config Value in MSBuild Task

To Fail on missing settings use the error condition for Xmlread task as shown belo(though for a different task) and the attach this task to your project file as shown -

How to fail an MSBuild when content files are missing

Community
  • 1
  • 1
NiladriBose
  • 1,849
  • 2
  • 16
  • 31
  • 1
    What is it? How does it work? Please provide enough information in your answer that it will be complete if the link breaks. – cadrell0 Sep 26 '12 at 18:38
  • Ya I am not getting what it is. When I am talking about injecting I mean writing stuff into the config file automatically if it does not exist. – chobo2 Sep 26 '12 at 18:39
  • Ok this allows you to inject settings into objects based on constructor argument name. Misinterpreted question thought you wanted to get rid of magic strings and try catch. – NiladriBose Sep 26 '12 at 18:43
0
        //test settings...
        var a = System.Configuration.ConfigurationManager.AppSettings["myAppKey"];
        if (a == null)
        {
            Console.WriteLine("Application key does not exists!");
        }
        var b = System.Configuration.ConfigurationManager.ConnectionStrings["myConName"];
        if (b == null)
        {
            Console.WriteLine("Connection does not exists!");
        }
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46
  • Should I do this in the constructor or each method? – chobo2 Sep 26 '12 at 20:36
  • If we are talking about win app, console or library, constructor would be a good point. Inside web app I would create my own class that inherits from Page. Inside this class I would override on page init and check for settings. All pages that requre this check will just need to inherit from my class. – Gregor Primar Sep 26 '12 at 20:44
  • so simple logic is ok in the constructor + throwing exceptions? – chobo2 Sep 26 '12 at 21:24