I have a library in a large asp.net 4.8 site that uses the ConfigurationManager deep down in the bowels of the project. At the time, no one had a problem with the dependency on that library. Well, we're upgrading to asp.net 5 (i.e. Core) which doesn't really use the ConfigurationManager anymore, instead uses a DI version of IConfguration. Unfortunately the access of the connection strings is so deep, passing along this object from the Controller down through the libraries isn't feasible. Is there any kind of "ConfigurationManager.ConnectionStrings" equivalent that reads the application.json file's ConnectionStrings section?
Asked
Active
Viewed 276 times
0
-
1Does this answer your question? [How to use System.Configuration.ConfigurationManager in .netstanard library for .net core and .netframework](https://stackoverflow.com/questions/61529217/how-to-use-system-configuration-configurationmanager-in-netstanard-library-for) – gunr2171 Apr 02 '22 at 03:44
2 Answers
1
You can use the following Code
public class SampleClass
{
private readonly IConfiguration _config;
public SampleClass(IConfiguration config)
{
_config = config;
}
public string GetConnectingString(string name)
{
return _config.GetConnectionString(name);
}
}

M Rizwan
- 83
- 8
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 02 '22 at 13:08
0
Assuming you have a connectionstrings section in your appsettings.json, you can use the GetConnectionString() method on the IConfiguration.
connectionString = configuration.GetConnectionString("DefaultConnection");

Lee
- 703
- 6
- 20