0

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.

6
  • Thanks, Stuart. So how would I change my utility to use a parameter to specify which config file I'd like to use? Commented Oct 28, 2016 at 17:23
  • 1
    By project you mean database? As I understand you have one app which handle for databases? You are trying to implement kind of multi tenant application? Commented Oct 28, 2016 at 17:25
  • 1
    You can't, it always loads the startup project config. Commented Oct 28, 2016 at 17:25
  • @kat1330 - yes - I have one project that handles the database. It seems as there must be some way to specify the app.config that you would like to use. They all get copied into the BIN directory Commented Oct 28, 2016 at 17:28
  • 1
    Missy, I'm just curious. If you have one project that handles all the database stuff, why do you need to read the connection string in the other projects? Commented Oct 28, 2016 at 19:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a good idea. I am making a forms application. I am guessing the same applies?
Yes. The application configuration should be read by the application entry point and then provided to the rest of the application - the rest of the application should never know about the presence of the config file at all.
1

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

4 Comments

Thank you, that's a really good idea. Do you think I can encrypt this like I do with the config.app?
But a RESX is compliled into the application. This removes the benefit o having the settings in an external file in the first place. One might as well have the connection string as a constant in the code.
@PhillipH Either of those is probably subject to being visible if someone uses a disassembler or decompiler - yes?
Yes. All .net code can be trivially decompiled. You keep things that you want to change after compilation time in a config file. If you have no intention of changing them after compilation then resource files are good. Database connection strings are almost always stored in a config file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.