[英]How to use System.Configuration.ConfigurationManager in .netstanard library for .net core and .netframework
I am migrating the .netframework 4.7.2 class library to .netstandard 2.0 library.我正在将 .netframework 4.7.2 class 库迁移到 .netstandard 2.0 库。
ConfigurationManager is used in the .netframework class library to read the app settings. ConfigurationManager 在 .netframework class 库中用于读取应用程序设置。
Any alternative option available for the System.Configuration.ConfigurationManager in .netstanard. .netstanard 中的 System.Configuration.ConfigurationManager 可用的任何替代选项。
The migrated class library needs to be used in a .net core web API and old .netframework web application. The migrated class library needs to be used in a .net core web API and old .netframework web application.
You could install ConfigurationManager
via nuget package , which targets.Net Standard 2.0.您可以通过nuget package安装
ConfigurationManager
,它针对.Net Standard 2.0。
As for ASP.Net Core, have a look at Microsoft.Extensions.Configuration .至于 ASP.Net Core,请查看Microsoft.Extensions.Configuration 。
As I said, you should not depend directly on how the configuration is obtained in a class library.正如我所说,您不应该直接依赖如何在 class 库中获取配置。 Keep your class library flexible.
保持 class 库的灵活性。
Don't do this in your class library:不要在您的 class 库中执行此操作:
public void UpdateProductName(int productId, string name)
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"]))
{
connection.ExecuteNonQuery("UPDATE dbo.Products SET Name = @Name WHERE Id = @ProductId", new { ProductId = productId, Name = name });
}
}
By directly using ConfigurationManager in your class library, you've tied the class to only allowing one form of obtaining the connection string.通过在 class 库中直接使用 ConfigurationManager,您已将 class 绑定到仅允许一种形式的获取连接字符串。 That means if you want to use appsettings.json, or environment variables, or KeyVault, or any other method of obtaining configuration, you can't.
这意味着,如果您想使用 appsettings.json、环境变量、KeyVault 或任何其他获取配置的方法,则不能。
Instead, do this:相反,请执行以下操作:
public class MyDatabaseRepository
{
readonly string _connectionString;
public MyDatabaseRepository(string connectionString)
{
_connectionString = connectionString;
}
}
public void UpdateProductName(int productId, string name)
{
using (var connection = new SqlConnection(_connectionString))
{
connection.ExecuteNonQuery("UPDATE dbo.Products SET Name = @Name WHERE Id = @ProductId", new { ProductId = productId, Name = name });
}
}
Now that allows you to obtain a connection string however you want in the consuming application.现在,这允许您在使用应用程序中获取所需的连接字符串。
In a .NET Core app, you might do this to get the connection string from appsettings.json via Microsoft.Extensions.Configuration:在 .NET 核心应用程序中,您可以通过 Microsoft.Extensions.Configuration 从 appsettings.json 获取连接字符串:
string connectionString = Configuration.GetConnectionString("MyDatabase");
MyDatabaseRepository repository = new MyDatabaseRepository(connectionString);
repository.UpdateProductName(1, "Transformer");
And it still allows you the flexibility to use System.Configuration in your .NET Framework app:它仍然允许您在 .NET 框架应用程序中灵活地使用 System.Configuration:
string connectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
MyDatabaseRepository repository = new MyDatabaseRepository(connectionString);
repository.UpdateProductName(1, "Transformer");
Coupled with Dependency Injection , this becomes a very flexible and powerful tool.再加上依赖注入,这成为一个非常灵活和强大的工具。
Thanks to this answer: https://stackoverflow.com/a/54259119/672110感谢这个答案: https://stackoverflow.com/a/54259119/672110
If you check the result of the call to ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
如果您检查对 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 的调用结果;
I added a ProjName.dll.config to a.Net 5 Web project, and that allowed a NetStandard2.0 project to get config values from ConfigurationManager.ConnectionStrings["x"] and ConfigurationManager.AppSettings["y"]我将 ProjName.dll.config 添加到 a.Net 5 Web 项目中,这允许 NetStandard2.0 项目从 ConfigurationManager.ConnectionStrings["x"] 和 ConfigurationManager.AppSettings["y"] 获取配置值
While this is clearly the wrong way to develop new code, this is allowing us to move forward with a large.Net Framework migration to.Net 6.虽然这显然是开发新代码的错误方式,但这让我们能够继续将大型 .Net 框架迁移到 .Net 6。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.