c# - 获取程序集配置
问题描述
我有一个用作 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 包(类库)中访问它
解决方案
推荐阅读
- javascript - 赛普拉斯和谷歌地图拖动移动元素,而不是地图
- javascript - Nipplejs方向事件触发间隔
- sql-server - SQL Server 何时提供行级安全性?
- python-3.x - 两列两个二维数组创建数据框?
- namespaces - unshare 命令不会创建新的 PID 命名空间
- java - 操作栏中的动态标题
- python - 如何在搅拌机大纲搜索栏中解析文本
- ruby-on-rails - Ruby on Rails 问题如何阻止 Total time duration 每天出现多次
- mysql - Mysql:选择 1 周前的交易数量
- zap - ZAP Spider 在 DVWA 上找不到表单请求