Reputation: 1368
I have a solution with four projects - Project 1, Project 2, etc. Each project has an app.config file. I am trying to reference the System.Configuration.ConfigurationManager.ConnectionStrings from Project 4 in a call from Project 1.
So in Project 1, I have:
UtilitiesClass util = new UtilitiesClass();
string connectionString = util.getConnectionString();
In Project 4, I have a utility:
public string getConnectionString()
{
string cx = System.Configuration.ConfigurationManager.ConnectionStrings["Shared Items.Properties.Settings.ConnectionString"].ConnectionString;
return (cx);
}
The error I get is that the connection string is null. I have run this from Project 4 independently and it gets the connection string without error.
Upvotes: 0
Views: 220
Reputation: 23
You can try adding a Resource file (.resx) to a project, add a connection string and change its accessibility to public, so you can use it trough the projects. Hope this helps you
Upvotes: 1
Reputation: 6222
In general you cannot use application configuration readers in anything other than the "entry point project" - i.e. the project that is loaded by IIS. Best practise here is to have the "entry point project" read its application settings file and provide that to the rest of the application through some kind of service class.
The reason for it being "best practise" is that you can then unit test your subsidiary projects and classes without the dependency of an external file (which of course will only have one set of properties per test deployment). By setting up the parameters at the point of entry - in this case the Test Case, you can simultaneously test many configurations at once.
Upvotes: 1