Search code examples
c#azureazure-functionsstartupdotnet-httpclient

Why would SendAsync on my DelegatingHandler injected in Startup of my Function App never be called?


I have the following configuration for an Azure Function App using .Net 3.1:

public override void Configure(IFunctionsHostBuilder builder)
{
    Config = CdrConfigurationBuilder.BuildConfiguration<Startup>(approotpath: builder.GetContext().ApplicationRootPath);
    builder.Services.AddSingleton(Config);
    builder.Services.AddAzureAppConfiguration();
    builder.Services.AddSingleton(CdrConfigurationBuilder.ConfigurationRefresher);

    builder.Services.AddSingleton((c) => new AuthHeaderHandler(Config, apisection: string.Empty, securitysection: "CDR:Services:Security"));

    builder.Services.AddHttpClient<IHtmlDocumentService, HtmlDocumentService>()
        .AddHttpMessageHandler<AuthHeaderHandler>()
        .SetHandlerLifetime(TimeSpan.FromMinutes(5))
        .AddPolicyHandler(GetRetryPolicy());
    
    builder.Services.AddSingleton<IHtmlDocumentService, HtmlDocumentService>();

    builder.Services.AddLogging();
}

This compiles and runs but SendAsync() on my AuthHeaderHandler is never called.

What am I missing?


Solution

  • You are injecting IHtmlDocumentService twice.

    The second registration is overriding the first registration.

    Remove the code line builder.Services.AddSingleton<IHtmlDocumentService, HtmlDocumentService>();