简体   繁体   中英

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="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.

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

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

在此处输入图片说明

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.

文件夹安全设置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM