microsoft-identity-web icon indicating copy to clipboard operation
microsoft-identity-web copied to clipboard

Configure HttpClient of DownstreamApi in order to set custom timeout for Http requests

Open serhatataman opened this issue 1 year ago • 1 comments

I have multiple microservices that have cold-start about 20+ seconds. My requests through DownstreamApi return 502 Bad Gateway when sending a requests.

I would love to be able to configure HttpClient of DownstreamApi, so that I can set up a timeout value of 30 seconds.

serhatataman avatar Aug 14 '23 15:08 serhatataman

We achieve this by adding a "HttpClient" to the ServiceCollection with the Service name, used in "AddDownstreamApi"

by extension method:

public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder UseHttpClientTimeout(this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder, string serviceName, TimeSpan timeout) 
{ 
    builder.Services.AddHttpClient(serviceName, client =>
    {
      client.Timeout = timeout;
    });
    return builder;
 }

and then used like

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(configuration.GetSection("AzureAd"))
        .EnableTokenAcquisitionToCallDownstreamApi(initialScopes).
.AddDownstreamApi("MyService", configuration.GetSection("MyApi"))
.UseHttpClientTimeout("MyService", TimeSpan.FromMinutes(1))

AndreErb avatar Sep 27 '23 16:09 AndreErb