Last active
August 14, 2020 23:35
-
-
Save ealsur/87103ca1ccc10b16bde2d44483a937ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Maintain a single instance of the SocketsHttpHandler for the lifetime of the application | |
SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler(); | |
socketsHttpHandler.PooledConnectionLifetime = TimeSpan.FromMinutes(10); // Customize this value based on desired DNS refresh timer | |
socketsHttpHandler.MaxConnectionsPerServer = 20; // Customize the maximum number of allowed connections | |
services.AddSingleton<SocketsHttpHandler>(socketsHttpHandler); | |
services.AddSingleton<CosmosClient>(serviceProvider => | |
{ | |
SocketsHttpHandler socketsHttpHandler = serviceProvider.GetRequiredService<SocketsHttpHandler>(); | |
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions() | |
{ | |
HttpClientFactory = () => new HttpClient(socketsHttpHandler, disposeHandler: false) | |
}; | |
return new CosmosClient("<connection-string>", cosmosClientOptions); | |
}); | |
services.AddSingleton<OtherService>(serviceProvider => | |
{ | |
SocketsHttpHandler socketsHttpHandler = serviceProvider.GetRequiredService<SocketsHttpHandler>(); | |
// another service that might also use HttpClient | |
HttpClient anotherHttpClient = new HttpClient(socketsHttpHandler, disposeHandler: false); | |
return new OtherService(anotherHttpClient); | |
} | |
//... other service registration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment