Refresh services when AppConfiguration values are refreshed in Azure Isolated Function

50 views Asked by At

I am following the steps listed here to refresh the App Config values when any config value is changed. However is there a way to refresh the services such as HTTPClient that are injected similar to the below where baseUrl is fetched from app config -

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");
});
1

There are 1 answers

0
kevalsing On

Yes you can do this by registering a callback on configuration refresh like this.

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

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