How to ensure ConfigurationManager points to correct App.config - c#

Recently created a new class library project and added a App.config file to it. App.config file contains DB connection string. But the current code
ConfigurationManager.ConnectionStrings["DBConnectionName"] gives me "null" .
But ConfigurationManager.ConnectionStrings[0] gives me result with name "LocalSqlServer" . But my App.config does not have any entry like this.
I don't see any other config files in my project . Then why this is happening ?
How could i ensure am pointing to correct App.Config?

App.Config and other config files are read from the entry point of the program. Thus, if it is an application, the app.config file stored in the windows application project is read.
LocalSqlServer is the default SQL server when no other connection string is defined. This is the reason why it exists inside ConnectionStrings property.

Please add the connection string to App.config of your main application, the executable. Alternatively, if you want to define your own config file then make use of ConfigurationManager.OpenExeConfiguration Method.
More details in following link:
http://msdn.microsoft.com/en-us/library/ms224437.aspx

Related

Asssembly with EF context class connection string in an assembly

Hope you can help with this one, although so for here does seem to b a resolution.
I have a class library which connects to a SQL Server database using an EF context class with a connection string in the app.config file.
I then reference this dll in an ASP.NET app but have to add the connection sting again in the web.config file
I would have though by referencing the dll and thus the connection string, I would not require to add the connection string again.
Perhaps I am missing something or has anyone else found a resolution to this ?
The connection string is read from the executables app.config (i.e. windows forms/WPF/console applications). For web applications the connection string is taken from the web.config file of the startup project (i.e. where the global.aspx.cs) is located.
You cannot use the app.config from your model DLL directly. You have to put the connection strings in the "main" projects.
What might help is some kind of scripting, but that's up to you.
You could try to use the configSource attribute and configure and external file:
<ConnectionString configSource="shared.config" />
The content of the file replaces the content of the element though. Another thing is that you need to include the file in your web app project and toggle copy to output.
And another warning:
the external connection strings file must be in the same directory as
the root web.config file, so you'll have to take precautions to ensure
you don't check it into your source repository.
More about that and best practices about deploying sensitive data can be found here:
http://www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure#con

Configure the connection string for multiple windows service

I am having multiple windows service in my current project. And all are having different app.config files & connection string is defined in the same config file.
Now I want to update the app.config in such a way that all the windows services share the connection string from the single file.
My directory structure is as given below :
RootFolder ->
Service A
Service B
I have tried using below 2 approaches but didn't work out :
AppSettings's "File" attribute. - Is there anything can be done that
I can assign the few key value's in AppSettings section & I can use
those in connectionstring section.
ConnectionString's "ConfigSource" Attribute - This attribute contains only relative path so it is not working for me.
Thank you so much in advance. Let me know if any further information is required.
One method can be using a symbolic link for the common app.config file.
So you put the common app.config file containing the connectionStrings section somewhere accessible by all services and in your services' installation directories you create a symbolic link pointing to that file using mklink
Such as:
mklink common.app.config.link "C:\Root\App.config"
And in the app.config of the Windows services you point to that link:
<connectionStrings configSource="common.app.config.link">
</connectionStrings>
It requires a bit maintenance but I guess it can be automated as well if you put mklink command in a script.
Hope this helps.
UPDATE:
Common app.config : X:\common.app.config
Project 1 Installation Path: X:\Project1\
Project 2 Installation Path: X:\Project2\
In X:\Project1\ create the link
mklink common.app.config X:\common.app.config
In X:\Project1\App.config file update the configSource
So bottomline is in configSource you give the path of link which is pointing to the actual file. If you're only providing the name of the link make sure it's in the same folder as you exe file, as shown in the image

Object reference not set to an instance of an object in windows service

Read a app.config value:
string configFile =
System.Configuration.ConfigurationManager.AppSettings["connStr"].ToString();
Config file:
<appSettings>
<add key="connStr" value="Data Source=Dolphin-PC;
Initial Catalog=jsptpd_SYS;
Persist Security Info=True;User ID=sa;
Password=ccir"/>
</appSettings>
Get an error:
Object reference not set to an instance of an object
the \program and debug\ folder contains app.config file.
Why woud this happen
Because, System.Configuration.ConfigurationManager.AppSettings["connStr"] is null
How could i deal it?
A more valid question. There isn't anything wrong with your code here. the main reason that you are finding it null is probably because your app.config is not present in the directory you are running the application from. It could be because app.config is part of some library and it is not copied to the output directory. You need to make sure that either app.config is in your main project or you copy it to the directory (you are running your applicatior from) explicitly.
To fixed the problem,you need to know the princeple of ConfigurationManager how to read the config file:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
To solve this problem,you could make sure your bin/ directory generate yourprogram.exe.config file,so the ConfigurationManager could read.
If this file missing,you coud make sure your app.config attribute generate operation is not Embeded resource.If it is true,the app.config will embeded to the exe file and configurationmanager could not read.

Is there a way to have web.config values overwrite app.config values?

I have a web project that references a dll project that uses an app.config value. Is there a way to put the app.config values in the web.config so when the web project is run it pulls the values from the web.config? I've heard of this happening but I'm not 100% sure of it.
You can use the configSource attribute/property of each config element - this allows you to put sections of your configuration in an external file.
Once in an external file, you can reference this file from both your web.config and app.config files.
I dont think there is a wasy - but to open manually the app.config file ( if your start entry point is the web...)
so youll have to open each section and analze it.

Why wont my application read my MyApplication.dll.config file?

I'm trying to use application settings with a C#.NET project I am working on. But I can't seem to get it to return anything other then the default values. Through the project properties I've added a single setting, DBConnectionString, and set its value to a connection string I want to use. Its scope is set to "application".
Doing this created a number of files including Settings.settings, Settings.Designer.CS, and app.conifg. From what I understand the two Settings.* files define a custom Settings class that derives from ApplicationSettingsBase. The Settings class then has custom, type safe, properties that can be used to set and retrieve each setting. The app.config file is a XML file that stores the actual settings values.
When I build my project it looks like the app.config file is copied to the target directory as MyApplication.dll.config. My program runs fine and is able to use the default connection string.
Next I tried to edit the MyApplicaiton.dll.config file to change the connection string. I ran my program again, but it continued to use the default value. I noticed that in my Settings.Designer file there is a DefaultSettingValueAttribute with the original default string. I tried removing this attribute, but then when I tried to retrieve the connection string setting it just returned null.
This is the code I'm using to read the connection string.
string conn = Properties.Settings.Default.DbConnectionString
What am I doing wrong? How can I get it to return the value in the settings file and not the default value?
Update:
Sorry I should have mentioned this. I'm actually writing a plug-in for another application through a public API. So my project is a DLL not an EXE.
You cannot read settings from *.dll.config files. If you library needs any special settings you need to put them in your app.config or web.config files.
EDIT: You can include the external config files in the main application or web config file. Look here for details.
This question discusses how to manage configuration files for large projects.
Settings files and .config files are different things (I do not know why VS automatically added a .config when you created a Settings file). But, the settings file is compiled into a class and is referenced like you said. If you decompile the dll with .NET reflector the Settings class will be in there. It is used for holding constant values or external resources. For example: error message strings, icons, or images.
The config file is for settings which can change frequently or between environments (dev, test, prod). For a connection string you should use the <connectionStrings> section of the config file. And the property can be referenced using System.Configuration.ConfigurationManager[ "connectionStringName" ].
However, from your original post it looks like your .dll is going to be used in a larger project (either an .exe of web project). One thing to note is that all projects only use one .config file. And that is the config file for the main project. Websites the web.config file, and exe's use XXX.XXX.XXX.exe.config (as you saw, *.exe.config files are renamed copies of the app.config files). dll's do not have usable config files. All dll's will look at the main project's .config file to retrieve information.
If your connection string is never going to change then by all means use the Settings file. Otherwise, use a config file and let the developer of the main project determine what to populate the connection string with.

Categories

Resources