7

In my .net 4 solution, i have two different projects- an web application project and a class library project.

In web application project, database connection string is in web.config file. I would like to access that connection string from class library project. Is it possible? if yes, how?

If there is any better approach to get connection string, please let me know.

1 Answer 1

7

To access it from your class library add a reference to System.Configuration then use System.Confinguration.ConfigurationManager.ConnectionStrings.

It's not ideal to read this from a class library. After all, can you say that your class library will always be consumed by something with a configuration file? Certainly not if you share it with other developers, especially of different platforms.

Consider:

  1. IoC - use dependency injection to provide a dependency that contains configuration settings. These would be populated by the consuming library (web app).
  2. Pass the settings to the class library when consuming elements that depend on them.

e.g.:

public class MyLibraryContainer
{
    private string _connectionString;

    public MyLibraryContainer(string connectionString)
    {
        _connectionString = connectionString;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

"can you say that your class library will always be consumed by something with a configuration file?" Since the library was written in C#, I think this is a valid assumption. That being said, I agree that accessing the ConfigurationManager directly in the class library is wrong, as this functionality is outside the scope of the library.
@EladLachmi "Since the library was written in C#, I think this is a valid assumption". I disagree. Just because the library is written in C# doesn't mean it will be consumed by a .NET solution. It could be, for example, iterop'd into java or anything else for that matter. YOu do have me curious now what would happen if you consumed configuration manager in a library whose caller has no support. There's one for the TODO list! :)
@EladLachmi On the other hand, if you wrote this, and have no intention of sharing it, then it would be a safer assumption.
Since class libraries can only take config information from consuming applications, I don't think you could access ConfigurationManager from a class library with a consuming application which does not support a configuration file. Behind the scenes, configuration manager creates a static class which accesses the config file through the Application object. In a non-.net application, the Application object does not exist and thus, the configuration information cannot be read.
And one more thought came to mind. The permissions to access the config file in the file system are through the user of the AppPool, so if no AppPool exists, there will be no user with which to access the config file on disk.

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.