首页 > 解决方案 > 获取程序集配置

问题描述

我有一个用作 nuget 包的类库。Nuget 包将在一个或多个 Web Api 中使用。

我是我的类库,我希望能够读取 web api 的配置设置。

这可以用Assembly.GetCallingAssembly(), 在这种情况下是我的 web api 吗?

原因是 Web api 将有一个从 Azure Key Vault 获得的 connectionString,我想在我的类库(nuget 包)中读取这个 connectionString。我不想将 connectionString 作为参数传递给 nuget 包。它应该是自动修复的。

我在我的类库中尝试过这样的事情:

    string path = Assembly.GetCallingAssembly().Location; // the web api
    var config = ConfigurationManager.OpenExeConfiguration(path);

但是我在这里找不到密钥库中的 connectionString。

这是我的 Startup.cs 中的代码,其中一个 web api 添加了 azure key vault

public Startup(IWebHostEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json") appsettings.json in.
        .AddEnvironmentVariables();

    if (env.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();  M
    }
    builder.AddKeyVaultConfiguration();
    Configuration = builder.Build();
}

连接字符串看起来像这样:Azure:Database:ConnectionString,现在我想在我的 nuget 包(类库)中访问它

标签: c#.net-core

解决方案


推荐阅读