WCF Services Configuration: how can I access AppSettings in code using C #

I have googled around and just found a few lines on how to configure the WCF service configuration, which is not what I want LOL.

Basically, I have a factory class in my service that reads the Custom property from the configuration file to determine which data access object to create, however, when I try to test it, I get a NullReferenceException.

Here is the code:

    public string Config { get; set; }

    public ProjectFactory()
    {
        Config = ConfigurationManager.AppSettings["ProjectDAOConfig"].ToString();
        LoadDAO();
    }

And here is the custom property in the We.config file for the service:

<configuration>
    <configSections>
         //Sections removed to make it tidier
    </configSections>
   <appSettings>
       <add key="ProjectDAOConfig" value="stub"/>
   </appSettings>
   <connectionStrings/>

why doesn't it work? Is the property an invalid configuration file? If so, I have to create the App.Config file as it currently does not exist

EDIT: I used the same method on the asp.net website and it worked perfectly.

+3
1

-, web.config.

, app.config , . , unit test, app.config unit test.

+6

Source: https://habr.com/ru/post/1724024/


All Articles