user576510
user576510

Reputation: 5915

can't read Keys from external appSettings file?

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="[email protected]" />
  </appSettings>

My external appSetting file is as below:

<appSettings>  
  <add key="ErrorEmailFrom" value="[email protected]" />
  <add key="ErrorEmailhost" value="smtp.ddd.local" />
  <add key="ErrorEmailPort" value="25" />
  <add key="ErrorEmailEnableSsl" value="true" />
  <add key="ErrorEmailUserName" value="[email protected]" />
  <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:

Please help.

Upvotes: 1

Views: 1461

Answers (3)

mikheld
mikheld

Reputation: 1

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

Upvotes: 0

frmbelz
frmbelz

Reputation: 2553

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

Upvotes: 1

David
David

Reputation: 10708

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());

Upvotes: 1

Related Questions