简体   繁体   English

如何覆盖 appsettings.json 或 appsettings。<envrionment> .json 文件以编程方式使用 .net 核心 web api 和 C#?</envrionment>

[英]How to overrite appsettings.json or appsettings.<envrionment>.json file(s) programtically using .net core web api with C#?

Current problem with a scenario: Our application code was deployed in dev environment with dev api service (Third party api service: DEV).一个场景的当前问题:我们的应用程序代码部署在具有 dev api 服务(第三方 api 服务:DEV)的开发环境中。 Now, due to maintainence this third party service is down in dev environment and our testing is blocked.现在,由于维护,该第三方服务在开发环境中已关闭,我们的测试被阻止。 So architect decided point to QA and redeploy the code into DEV environment.因此架构师决定指向 QA 并将代码重新部署到 DEV 环境中。

Until we push the changes into DEV team has to wait for testing.在我们将更改推送到 DEV 团队之前必须等待测试。

To avoid this situation, what we are looking here is,为了避免这种情况,我们在这里寻找的是,

Read these file(s): appsettings.json or appsetting.<environment>.json and loaded in UI with editable way.读取这些文件: appsettings.jsonappsetting.<environment>.json并以可编辑的方式加载到 UI 中。 When there is a change required like above scenario, we change and update our appsettings.json or appsetting.<environment>.json files.当需要像上述情况那样进行更改时,我们更改并更新我们的appsettings.jsonappsetting.<environment>.json文件。

This way we save our time to redploy this file with new changes.这样我们就可以节省时间来重新部署这个文件并进行新的更改。

But our question here is, all our data from appsettings.json or appsetting.<environment>.json these file(s) loaded during startup.但我们的问题是,我们所有来自appsettings.jsonappsetting.<environment>.json这些文件的数据都是在启动期间加载的。 Later, we update and loaded the file in environment but how these values are going to read it through any of the service in code?稍后,我们在环境中更新并加载文件,但这些值将如何通过代码中的任何服务读取它?

Hope you are getting my scenario and expecting your valuable suggestions希望你能理解我的场景并期待你的宝贵建议

If I understood your request, you will need to use a design pattern, such as wrapper .如果我理解您的要求,您将需要使用设计模式,例如wrapper

Create a singleton service that holds all settings in properties.创建一个 singleton 服务,它包含属性中的所有设置。 Do not use IConfiguration directly anywhere in the services, instead switch to the wrapper.不要在服务的任何地方直接使用IConfiguration ,而是切换到包装器。 Thanks to this you can control the source of settings, and overwrite the settings by overwriting properties in the wrapper.由于这一点,您可以控制设置的来源,并通过覆盖包装器中的属性来覆盖设置。 You don't have to modify source files and reload the entire application, thanks to this solution.得益于此解决方案,您不必修改源文件并重新加载整个应用程序。

Also remember about proper caching and encapsulation - if you're caching the values somewhere, make sure you have that in mind.还要记住适当的缓存和封装 - 如果您在某处缓存值,请确保您牢记这一点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM