3

I have build a WCF service that uses Web.config to get some appSettings. In visual studio it works great but when I publish and instal the service it suddenly gets its appSettings from App.config and not from Web.config.

I know this because I loop through appSettings and printed the result to the console with this code:

foreach (string key in ConfigurationManager.AppSettings.AllKeys)
{
     Console.WriteLine("Name: {0} Value: {1}", key, ConfigurationManager.AppSettings[key]);
}

My configs look like this:

Web.config

  ....
  <appSettings>
    <add key="IQDir" value="C:\Program Files (x86)\Ridder iQ Client (lokaal)\Bin"/>
    <add key="FileURL" value="localhost:8080/WebPortal_2.0/"/>
  </appSettings>
  ....

App.config

....
  <appSettings>
    <add key="test1" value="wtf is going on!"/>
    <add key="test2" value="waargh"/>
    <add key="test3" value="I am getting a headache over here!!"/>
  </appSettings>
....

When I run in visual studio I get:

enter image description here

But when I use the published code inside live environment I get this:

enter image description here

Why is this happening and how can I force ConfigurationManager to get appSettings from Web.config instead of App.config.

botenvouwer
  • 4,334
  • 9
  • 46
  • 75
  • Why do you have web.config AND app.config? – stuartd Nov 29 '13 at 12:39
  • 1
    Have you tried using `System.Web.Configuration.WebConfigurationManager` instead? – Tobberoth Nov 29 '13 at 12:41
  • @Tobberoth I have tried a lot of things including `WebConfigurationManager` All same results. – botenvouwer Nov 29 '13 at 12:42
  • 1
    @sirwilliam First of all, why do you have an app.config? Second of all, are you sure you're testing this correctly? From what I can see in the MSDN documentation, WebConfigurationManager can not open app.configs, it only opens web application and machine configuration files. – Tobberoth Nov 29 '13 at 12:50
  • @Tobberoth thats why I asked the question I do not understand why this is happening. In the testing environment it does what it should do but when I publish it starts acting weird. – botenvouwer Nov 29 '13 at 12:53

2 Answers2

2

If you have a standard WCF-project, you should only have the Web.config-file, not App.config.

I would skip the old way of using appSettings altogether. Use applicationSettings instead, by using the Settings-tab in the project's properties.

It will create this in Web.Config:

<applicationSettings>
    <Projectname.Properties.Settings>
        <setting name="Testenvironment" serializeAs="String">
            <value>True</value>
        </setting>
    </Projectname.Properties.Settings>
</applicationSettings>

For more information: appSettings vs applicationSettings. appSettings outdated?

Community
  • 1
  • 1
cederlof
  • 7,206
  • 4
  • 45
  • 62
2

configurationManager is used to pick values from the configuration file of project under which it is running. For example, you have exposed your wcf on web server S1 and you are consuming it in a console app from client machine M1. Now if your c# code is running on S1 then it will pick values from web.config from wcf code folder on S1. But if this code is running on client machine M1 (the console app consuming the service), then it will pick values from machine M1. What you are facing, normally happened after publish.

Amer Zafar
  • 419
  • 2
  • 11
  • 2
    I am running this from my local machine right now to test it does not work on any machine I have tested so far. The strange thing is that `WebConfigurationManager`gets its appSettings from App.config. Is there a way to see the path where `WebConfigurationManager` gets its .config file from? – botenvouwer Nov 29 '13 at 13:35