Skip to content

Instantly share code, notes, and snippets.

@ealsur
Last active September 21, 2020 17:14
Show Gist options
  • Save ealsur/d435fe063469fe09235e6d9e9eb81315 to your computer and use it in GitHub Desktop.
Save ealsur/d435fe063469fe09235e6d9e9eb81315 to your computer and use it in GitHub Desktop.
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddSingleton<CosmosClient>(serviceProvider =>
{
IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
{
HttpClientFactory = httpClientFactory.CreateClient,
ConnectionMode = ConnectionMode.Gateway
};
return new CosmosClient("<connection-string>", cosmosClientOptions);
});
//... other service registration
}
@ealsur
Copy link
Author

ealsur commented Sep 21, 2020

@ylr-research

My code example has services.AddHttpClient();, how are you registering the IHttpClientFactory?

You are just creating a Transient instance of HttpClient, which that by itself is incorrect, you should avoid creating new HttpClients all the time.

@dotnetspark
Copy link

@ealsur I fixed those lines already an the error is still the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment