asp.net 在Azure隔离函数中刷新AppConfiguration值时刷新服务

bzzcjhmw  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(92)

我按照here列出的步骤,在任何配置值更改时刷新应用程序配置值。但是,是否有一种方法可以刷新服务,如HTTPClient,这些服务类似于下面从应用程序配置中获取baseUrl的方式注入-

builder.Services.AddHttpClient("GitHub", httpClient =>
{
    httpClient.BaseAddress = new Uri(appConfig.BaseUrl!);

    // using Microsoft.Net.Http.Headers;
    // The GitHub API requires two headers.
    httpClient.DefaultRequestHeaders.Add(
        HeaderNames.Accept, "application/vnd.github.v3+json");
    httpClient.DefaultRequestHeaders.Add(
        HeaderNames.UserAgent, "HttpRequestsSample");
});

字符串

nuypyhwy

nuypyhwy1#

是的,你可以这样做,在配置刷新时注册一个回调。

refreshOptions.NotifyOnRefresh(async () =>
            {
            // Update the properties of the injected HTTPClient service 
        var httpClient = services.GetRequiredService<IHttpClientFactory>().CreateClient("GitHub");                                

httpClient.BaseAddress = new Uri(appConfig.BaseUrl!);
            });

字符串

相关问题