我有两个项目:
我的类库项目包含一个app.config文件.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="TestEntities" connectionString="metadata=res://*/DBNews.csdl|res://*/DBNews.ssdl|res://*/DBNews.msl;provider=System.Data.SqlClient;provider connection string="{0}"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
从Console项目我想从类库中访问设置,所以我尝试过:
var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll");
config.ConnectionStrings.ConnectionStrings[0].Name; // LocalSqlServer
// seems to be the wrong assembly.
Run Code Online (Sandbox Code Playgroud)
和:
var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll.config");
// invalid exePath
Run Code Online (Sandbox Code Playgroud)
我该如何访问DLL app.config?
I need to use Dependency Injection in a static class.
the method in the static class needs the value of an injected dependency.
以下代码示例演示了我的问题:
public static class XHelper
{
public static TResponse Execute(string metodo, TRequest request)
{
// How do I retrieve the IConfiguracion dependency here?
IConfiguracion x = ...;
// The dependency gives access to the value I need
string y = x.apiUrl;
return xxx;
}
}
Run Code Online (Sandbox Code Playgroud)