Jay Dave
Jay Dave

Reputation: 981

ConfigurationManager.AppSettings[key] not accessible within class library

I am unable to access CloudfigurationManager from within an method. The code was originally a klass library and I added a configuration file (App.config) where I am simply adding the values for a hard coded test for Azure SDK.

OpenSessionWithAzure()

 private void OpenSessionWithAzure()
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSetting["APP_SETTINGS"])// Visual Studio is unable to identify ConfigurationManager
    }

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <connectionStrings>
   <add name="StorageConnectionString"   connectionString="DefaultEndpointsProtocol=https;AccountName=userName;AccountKey=Key"/>
   <add name="SasPolicyName" connectionString="myPolicy"/>   
 </connectionStrings>
</configuration>

Is there a reason why I am unable to access the ConfigurationManager within my application or a known workaround?

Upvotes: 0

Views: 3130

Answers (4)

Derek
Derek

Reputation: 837

The method signature you use must be consistent with the setting in your App.config.

With the setting below,

<configuration>
  <appSettings>
    <add key="StorageConnectionString" value="XXXXXXXXX"/>
  </appSettings>
</configuration>

use System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].

And System.Configuration.ConfigurationManager.ConnectionStrings["StorageConnectionString"] for

<configuration>
  <connectionStrings>
    <add name ="StorageConnectionString" connectionString="XXXXXXXXXX"/>
  </connectionStrings>
</configuration>

Upvotes: 1

gunvant.k
gunvant.k

Reputation: 1086

You need to add reference to System.Configuration and then add key-value pair in appSettings section

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
          <add key="SasPolicyName" value="myPolicy" />
          <add key="APP_SETTINGS" value="MySetting" />
   </appSettings>
  <connectionStrings>
          <add name="StorageConnectionString"   connectionString="DefaultEndpointsProtocol=https;AccountName=userName;AccountKey=Key"/>
  </connectionStrings>
</configuration>

Then use like below,

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSetting["APP_SETTINGS"])

Upvotes: 1

Jay Dave
Jay Dave

Reputation: 981

The reason for this error was due to the fact that I did not:

  1. Have a reference to System.Configuration.dll in my class library.
  2. Did not have the using System.Configuration

After adding performing the steps above I am not able to access ConfigurationManager.AppSettings["APP_SETTINGS"]

Upvotes: 1

Scott Hannen
Scott Hannen

Reputation: 29252

When you attempt to read from appSettings, it reads from the .config file of the executing application. So in the simplest implementation you'd just need to add those configuration settings to that application's .config file (app.config or web.config.) There are other ways to approach it if you absolutely need the library to have its own .config file, but adding it to the executing application's .config is what's done most commonly.

Upvotes: 0

Related Questions