c# - 即时更改 appsettings.json 并将其输入 DI
问题描述
我正在为我的项目编写集成测试,我有以下场景。我有一个基类,所有测试类都从该基类继承。我的基类的构造函数如下所示:
public KrampHubServiceIntegrationTestBase()
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.Build();
_apiSettings = config.GetSection("ApiSettings").Get<ApiSettings>();
var builder = new WebHostBuilder()
.UseStartup<Startup>()
.UseConfiguration(config);
var server = new TestServer(builder);
_client = server.CreateClient();
}
现在,当它调用.UseStartup()我想是它说进入我的另一个项目(Startup 是我正在测试的项目的一部分)并使用该 Startup(它注入不同的接口和实现的地方也有我的 appsettings.json 的值)。我需要做的是在启动开始工作之前修改 appsettings.json,即ApiSettings会话。我怎样才能做到这一点?提前致谢!
解决方案
推荐阅读
- nginx - 制作 CMS + Nginx 重定向
- python - Python 2.7 为什么不打开创建一个新文件
- python - 使用 Django 在 HTML 中显示 SQLite 条目
- javascript - Javascript 获取在 HTTP 请求中发送 HTML 和 URL 字符串
- javascript - 单击链接后,通过 JavaScript 重新加载页面继续一次
- python - 如何使用以下 DataFrame 创建字典
- c# - 32 位客户端和 64 位服务器之间的免注册 COM 互操作
- asp.net - 允许未经身份验证访问 ASP.NET web.config 中的某些文件类型,无论其位置如何
- python - 如果他们第一次弄错了,有没有办法不给分,测验,python
- python - 如何将 df 值与列表的元素进行比较,并用这些值制作 df?