相关疑难解决方法(0)

如何从另一个程序集中读取app.config?

我有两个项目:

  • 控制台项目(Test.exe)
  • 类库项目(Test.Data.dll)

我的类库项目包含一个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=&quot;{0}&quot;" 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

c# app-config

15
推荐指数
2
解决办法
9483
查看次数

Use dependency injection in static class in .NET Core

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)

c# static dependency-injection class

6
推荐指数
1
解决办法
2635
查看次数

标签 统计

c# ×2

app-config ×1

class ×1

dependency-injection ×1

static ×1