asp.net core重新加载应用配置intro

我把配置放在了数据库或者是redis里,配置需要修改的时候我要直接修改数据库,然后调用一个接口去重新加载应用配置,于是就尝试写一个运行时重新加载配置的接口。

configuration 重新加载实现

重新加载配置的接口其实很简单,经过看 configuration 的源码可以知道,如果想要重新加载应用配置,需要一个 iconfigurationroot 对象,而 iconfigurationroot 其实可以直接拿注入服务中的 iconfiguration 对象,服务中的 iconfiguration 对象也是实现了 iconfigurationroot 接口的实例。后面我们一起看源码就更清晰了。

来看实现重新加载配置的代码

using microsoft.aspnetcore.mvc;
using microsoft.extensions.configuration;

namespace testwebapplication.controllers
{
  [route("api/[controller]")]
  public class configurationscontroller : controller
  {
    private readonly iconfigurationroot _configuration;

    public configurationscontroller(iconfiguration configuration)
    {
      _configuration = configuration as iconfigurationroot;
    }

    [httpget]
    public iactionresult get()
    {
      return ok(new
      {
        rootuser = _configuration.getappsetting("rootuser") // 这里 getappsetting 是一个自定义扩展方法,获取appsettings 节点下的配置信息
      });
    }

    [httpput]
    public iactionresult put()
    {
      _configuration.reload();
      return ok();
    }
  }
}

是不是很简单,下面我们来尝试一下,你可以参考这个示例项目

因为默认的项目配置会监听 appsettings.json 文件是否修改,如果已修改就会重新reload,这里我新加一个文件,这里设置 reloadonchangefalse,示例代码如下:

public static iwebhostbuilder createwebhostbuilder(string[] args) =>
      webhost.createdefaultbuilder(args)
        .configureappconfiguration(configbuilder =>
        {
          configbuilder.addjsonfile("abc.json", optional: true, reloadonchange: false);
        })
        .usestartup<startup>();

abc.json 的文件内容如下:

{
 "appsettings": {
  "testnumber": 12,
  "rootuser": "weihanli"
 }
}

dotnet run 启动网站,然后在浏览器中访问

然后我们修改 abc.json 文件

{
 "appsettings": {
  "testnumber": 12,
  "rootuser": "weihanli 123"
 }
}

修改保存之后刷新刚才的页面,可以看到还是刚才的内容,证明并没有重新加载配置,接下来尝试我们的重新加载配置方法

使用 postman 或 fiddler 或其他你喜欢的工具发一个 put 请求到 http://localhost:5000/api/configurations,这里我使用 postman 调用 put 接口重新加载配置

返回 200 即接口调用成功,重新刷新刚才的页面就可以看到页面上的数据已经发生变化,这也就证明了我们重新加载配置的接口生效了。

源码解析

来看 configurationbuilder 在 build 的时候做了什么,configurationbuilder 源码

可以看到这里最后返回的是一个 iconfigurationroot 对象,再来看 iconfigurationroot 源码

可以看到 iconfigurationroot 定义了一个 reload 的方法,这个方法会从下面的 providers 中重新加载配置,看到这里我们就知道可以通过 iconfigurationreload 方法来重新加载应用程序的配置了,然后我们来看 webhost.createdefaultbuilder(args).build() 做了什么
https://github.com/aspnet/aspnetcore/blob/master/src/defaultbuilder/src/webhost.cs

这里我们可以看到为什么 appsettings.json 文件会自动 reload 配置,可以看到最后返回了一个 webhostbuilder 对象

看 asp.net core webhostbuilder 对象的 build 方法 https://github.com/aspnet/aspnetcore/blob/master/src/hosting/hosting/src/webhostbuilder.cs

buildcommonservices 可以看到这样一段代码 https://github.com/aspnet/aspnetcore/blob/master/src/hosting/hosting/src/webhostbuilder.cs

上面我们已经知道 configurationbuilder build 之后返回的是一个 iconfigurationroot 对象,而这里注入是一个 iconfiguration 对象(iconfigurationroot 实现 iconfiguration 接口),所以我们就可以从依赖注入中获取 iconfiguration 对象直接当作 iconfigurationroot 来使用,这也就是为什么我们会直接获取一个 iconfiguration 对象直接 as iconfigurationroot

memo

到此就暂时结束了,希望你能有所收获~

以上所述是www.887551.com给大家介绍的asp.net core重新加载应用配置详解整合,希望对大家有所帮助