简体   繁体   English

如何使用 .NETCore 在 2 个不同项目之间覆盖 appsettings.json 文件中的值

[英]how to override value in appsettings.json file between 2 different projects using .NETCore

I have a C# class library project and I am using appsettings.json to read values.我有一个 C# class 库项目,我正在使用appsettings.json读取值。
I have one MS Test (unit test) project and I am maintaining another appsettings.json file我有一个 MS 测试(单元测试)项目,我正在维护另一个appsettings.json文件

appsettings.json file in C# Class library project: appsettings.json 文件在 C# Class 库项目中:

{
  "name" : "jack"
}

appsettings.json file in MS Test project: MS 测试项目中的 appsettings.json 文件:

{
  "name" : "Mike"
}

My requirement is when I run the unit test the value "name" in the appsettings.json file in the C# class library project needs to be replaced with appsettings.json file in the MS Test project. My requirement is when I run the unit test the value "name" in the appsettings.json file in the C# class library project needs to be replaced with appsettings.json file in the MS Test project.

You should be using dependency injection to access configuration settings in your lib's classes;您应该使用依赖注入来访问 lib 类中的配置设置; simply manually construct an instance of the configuration class with the test values you desire and pass it to the lib classes' constructors.只需使用您想要的测试值手动构造配置 class 的实例,并将其传递给 lib 类的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为不同的操作系统覆盖 appsettings.json? - How to override appsettings.json for different OS? 从不同的项目访问appsettings.json - Accessing appsettings.json from different projects 如何使用appsettings.json覆盖serilog设置 - How to override serilog settings using appsettings.json 在发布 netCore6 上更改 appsettings.json 值 - Change an appsettings.json value on publish netCore6 使用 ASPNETCORE_ENVIRONMENT 覆盖 Azure 上的 appsettings.json - Override appsettings.json on Azure using ASPNETCORE_ENVIRONMENT 如何在项目appsettings.json中配置多个QnA模型? - How to configure multiple QnA models inside projects appsettings.json? 如何将.MinimumLevel.Override 移动到 appsettings.json? - How do I move .MinimumLevel.Override to appsettings.json? C#获取appsettings.json值并将其存储到app.config .netcore 2.0中 - C# grab appsettings.json value and store into app.config .netcore 2.0 如何将 appsettings.json 中的 json 设置解析为 Z240AA2CEC4B29C56F3BEE520A8DCEE7 中的 class - How to parse json settings in appsettings.json into a class in c# .netcore 使用 appsettings.json 配置 Serilog 以将某些日志过滤到不同的文件 - Configure Serilog to filter certain logs to a different file using appsettings.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM