ConfigurationManager.AppSettings[key] not accessible within class library - c#

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?

The reason for this error was due to the fact that I did not:
Have a reference to System.Configuration.dll in my class library.
Did not have the using System.Configuration
After adding performing the steps above I am not able to access ConfigurationManager.AppSettings["APP_SETTINGS"]

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"])

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>

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.

Related

ConfigurationManager throwing null exception when trying to read connection string in .NET 4.0

I am working on .NET Framework 4.0 Class Library project along with ADO.NET. I have connectionString inside the App.config which I am trying to read using ConfigurationManager but getting null reference exception.
Error
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name ="dbConnection" connectionString="Data Source=xyz;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Reading ConnectionString
using System.Configuration;
var x22 = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
in app config
<connectionStrings>
<add name="[name]" connectionString="[YOUR CONNECTION]" />
</connectionStrings>
call your connectionString
ConfigurationManager.ConnectionStrings["name"].ConnectionString
In your solution explorer and under reference node, check if assembly System.Configuration has been added related to you needed Configuration Class and there is not any error regarding to your references.
But, your code is correct and I suggest to you try to avoid this error by checking null situation in return value of that command. Maybe config file is not exist in your default path. check config file to be exist then try to read it's data.

How to move connectionString from App.config to code in C#?

I want to remove connectionString from App.config and add it to code. I'm using WinForms(C#) and .Net Framework 4.8. I have textBoxes and a datagridview on my form. I'm using Microsoft Access Database (.mdb). I want to do this because database's password is visible in App.config. Is there any way to do that
Here is my app.config XML code :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Locker.Properties.Settings.pw_dbConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\files\pw_db.mdb;Persist Security Info=True;Jet OLEDB:Database Password=XYZ"
providerName="System.Data.OleDb" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
I would suggest you not to define the connection string in code, better you can encrypt it in the app.config file.
Please follow the following steps:
Rename app.config to web.config
Open command prompt and type:
%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef
"connectionStrings" path of the web.config>
Rename web.config back to app.config
If you open the file in notepad, you will see the encrypted value and you can continue using it as you were using before.

app.config in a Class Library - asp.net C#

I have a class library application and am unable to configure a CONFIG file. My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="value test"/>
</appSettings>
</configuration>
and the call in my application:
var test = ConfigurationManager.AppSettings["test"];
But when I run, always passes a null value.
Class libraries do not use a .config file. Only applications (app.config) and web site s(web.config) read the "config" file.
The class library can use the Configuration Manager to read a value, but they must running inside of an app/web site.
In ASP.Net rename 'app.config' to 'web.config' and place it in the root folder of web site.
You have to put the appSettings tag in the web.config of your website in order to use ConfigurationManager.AppSettings["test"] if you do not want to do that then make the setting xml file and read it in your library code.
Add this code to the web.confg
<configuration>
<appSettings>
<add key="test" value="value test"/>
</appSettings>
</configuration>
And this is get value
var test = WebConfigurationManager.AppSettings["test"];
If you are passing string you may use
in webconfig
<appSettings>
<add key="test" value="value test"/>
</appSettings>
In cs.
String update = Convert.ToString(ConfigurationManager.AppSettings["test "]);
thank u all!
I used my config from my web application and solved the problem. It was something simple that I was complicating.

Error: Configuration system failed to initialize

Used console application and I have a problem on the app.config file. By right, the code should be correct according to the various sources found in the Internet but my coding is not working for no reason. Anyone can advise me on this?
And the <configuration> appears to be underlined with wriggly blue line, is it a warning or something? I dont know why it appears like this.
[EDITED]
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSetting></configSetting>
<connectionStrings>
<add name="Production"
providerName="System.Data.SqlClient"
connectionString="Data Source=EBSDLAB1SERVER2;Initial Catalog=Production;Integrated Security=True"/>
</connectionStrings>
</configuration>
The error shows, Unhandled Exception: System.Configuration.ConfigurationErrorException:Configuration system fail to initialize ---
System.Configuration.ConfigurationErrorException: Unrecognized configuration section connectionstring
Configuration system failed to initialize
In your App.Config file you need a section for connection strings, it should look like:
<connectionStrings>
<clear />
<add name="Production"
providerName="System.Data.ProviderName"
connectionString="ConnectionStringGoesHere" />
</connectionStrings>
There's more information regarding this here.
I managed to solve this problem which is to copy the app.config file to the same folder as the console application.exe and rename the app.config file according to the console application.exe!
Thank you all for the help!

Connection String Problem

I'm new to Visual Studio and MySQL. I'm creating a Login Page and connect it with MySQL. But whenever I add a connection string. I always got an error.
Can anybody tell me what's the problem in here?
Please.
This is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
I'm not entirely familiar with MySQL connections, but your config file should look more like this
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;"/>
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="Database=timekeeping;uid=root;pwd=admin;Option=3;" />
</connectionStrings>
</configuration>
You probably have a syntax error in your App.config file.
Check the InnerException for details.
This usually means there is a problem with your web.config file. Probably a tag out of place, not closed, or otherwise jacked.
http://msmvps.com/blogs/kevinmcneish/archive/2010/01/06/fixing-quot-configuration-system-failed-to-initalize-quot-exception.aspx
<configuration>
<appSettings>
<add key="ODBCDriver" value="Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=timekeeping;uid=root;pwd=admin;Option=3;"/>
</appSettings>
</configuration>
But whenever I add a connection
string. I always got an error.
Your web.config or app.config configuration is not legal in its current form - this could be because of a typo, i.e an unclosed tag, otherwise malformed XML or a section that is unknown or nested in a section where it doesn't belong.

Categories

Resources