1

Its a windows service and I can read appkeys from app.config but can't read from extnernal appSettings file which is on the same path. Below is my app.config:

  <appSettings file="Scheduler.dev.AppSettings.config">
    <add key="ErrorEmailTo" value="xxx@domain.com" />
  </appSettings>

My external appSetting file is as below:

<appSettings>  
  <add key="ErrorEmailFrom" value="test@xxxxxx.com" />
  <add key="ErrorEmailhost" value="smtp.ddd.local" />
  <add key="ErrorEmailPort" value="25" />
  <add key="ErrorEmailEnableSsl" value="true" />
  <add key="ErrorEmailUserName" value="test.user@xxxxxxxx.com" />
  <add key="ErrorEmailPassword" value="password" /> 
</appSettings>

Below is my code to read keys:

 protected override void GetDetails()
 {
        try
        {
            var ErrorEmailTo = ConfigurationManager.AppSettings["ErrorEmailTo"];
            var ErrorEmailFrom = ConfigurationManager.AppSettings["ErrorEmailFrom"];
            var ErrorEmailhost = ConfigurationManager.AppSettings["ErrorEmailhost"];
            var ErrorEmailPort = ConfigurationManager.AppSettings["ErrorEmailPort"];
            var ErrorEmailEnableSsl = ConfigurationManager.AppSettings["ErrorEmailEnableSsl"];
            var ErrorEmailUserName = ConfigurationManager.AppSettings["ErrorEmailUserName"];
            var ErrorEmailPassword = ConfigurationManager.AppSettings["ErrorEmailPassword"];          
        }
        catch (Exception ex)
        {               
            throw ex;
        }
 }

I can code can read first key which is coming form app.config but others remain null. Even if I move my keys into app.config I can read all keys using above code.

What I have tried is:

  • checked names, path
  • deleted all temp/ bin files but no luck

Please help.

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
user576510
  • 5,777
  • 20
  • 81
  • 144
  • 1
    possible duplicate of [External AppSettings File NOT merging with web.config](http://stackoverflow.com/questions/18832307/external-appsettings-file-not-merging-with-web-config) – CodingWithSpike Dec 04 '14 at 22:32
  • @CodingWithSpike details are different, suggested post has no accepted answer. I have tried suggetions but they are not working. – user576510 Dec 04 '14 at 22:39
  • 1
    The "Scheduler.dev.AppSettings.config" file is included in the build, isn't it? – dyson Dec 04 '14 at 22:57
  • @barrick I m running it from my visual studio from development machine, can you please tell how I can check if it is included in build ? – user576510 Dec 04 '14 at 23:45
  • 3
    @user576510 First - is Scheduler.dev.AppSettings.config in the project's \bin\Debug\ directory (which is where the project is running from if you're using F5 from VS if you've not changed it)? If not, then right-click on the file name in the Solution Explorer in Visual Studio and select Properties, you should see a small menu including the option `Copy to Output Directory`. If that's not set to `Copy always`, change to that, save and build. The file should then appear in the output directory (e.g. ..\bin\Debug\) – dyson Dec 04 '14 at 23:58

3 Answers3

1

The problem is that the app.config is read differently than other XML files. This includes some handling by the runtime itself, such as being able to enforce .NET versions or redirect DLLImports in Mono.

If you want to be able to read these elements, try converting them into a dictionary using System.Linq.XML instead of relying on the runtime to convert them.

using System.Linq.XML;

...
public static Dictionary<string, string> ConfigValues
    = XDocument.Load(configFilePath)
        .Root
        .Elements()
        .Where(e => e.Name == "add")
        .ToDictionary(
            e => e.Attributes().FirstOrDefault(a => a.Name == "key").Value.ToString(),
            e => e.Attributes().FirstOrDefault(a => a.Name == "value").Value.ToString());
Ethan Cabiac
  • 4,943
  • 20
  • 36
David
  • 10,458
  • 1
  • 28
  • 40
1

What @barrick said above resolved this for me plus one more step

  • in Visual Studio Solution Explorer right click on the external config file you are trying to use -> click "Include In Project" (dah, sorry I am new to Visual Studio, took me time to find this one)

  • right click the same config file -> "Properties" in drop down -> in Properties window below Solution Explorer -> "Advanced" -> "Copy to Output Directory" -> click "Do not copy" drop down -> change to "Copy if newer" or some posts say "Copy always" should work too

enter image description here

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
0

I have had a specific case where WCF service config file was loading an external file. The service was hosted on IIS. The problem was with Security settings of the folder where the external file resided. It was not in the same folder with the main config file and not within the project folder.

To resolve the issue, I had to add IIS_IUSRS from my local machine to the list of users of this folder. That allowed the service to access the folder with an external file.

Folder security settings

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77