asp.net core ElasticClient DI scope
See original GitHub issueI have this repository class
public sealed class ProcessDataApiKeyRepository : IProcessDataApiKeyRepository
{
private readonly ElasticClient client;
private static readonly string index = nameof(ProcessDataApiKey).ToLower();
private readonly AppSettings _appSetting;
public ProcessDataApiKeyRepository(IOptions<AppSettings> appSetting)
{
_appSetting = appSetting.Value;
var node = new Uri(_appSetting.DbConnection.Elastic.Url);
var settings = new ConnectionSettings(node);
settings.DefaultIndex(index);
client = new ElasticClient(settings);
}
public async Task<bool> SaveApiKeyAsync(ProcessDataApiKey apiKey)
{
var response = await client.IndexAsync(apiKey);
return response.Created;
}
and inject this class in asp.net core default Di container
services.AddSingleton<IProcessDataApiKeyRepository, ProcessDataApiKeyRepository>();
Reading the Nest Documentation, the best way to manage the Nest client lifetimes (https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/lifetimes.html) is through the singleton instance.
But, I don’t know if this way is the best, because I think that I should be just managing the Nest client class as singleton and then inject in any repository
I made some test using asp.net core scope lifetime
services.AddScoped<IProcessDataApiKeyRepository, ProcessDataApiKeyRepository>();
and the memory usage for each request was around 50 mg !!!
with the singleton scope the memory usage peer request was around 2 mg
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using the Client in a Function-as-a-Service Environment
This section illustrates the best practices for leveraging the Elasticsearch client in a Function-as-a-Service (FaaS) environment.
Read more >Elasticsearch in ASP.NET Core
To demonstrate how to use Elasticsearch in ASP.NET Core, we're going to create a simple web application with a text field input. When...
Read more >How to include scopes when logging exceptions in ASP. ...
This post describes how to work around an issue I ran into when logging exceptions that occur inside a scope block in ASP.NET...
Read more >ASP.NET Core Dependency Injection - Steve Gordon
In this post, I wanted to take a deeper look at the first concept from the Microsoft Dependency Injection (DI) container, the IServiceCollection ......
Read more >ASP.NET Core Scope Validation
If a scoped service is created in the root container, the service's lifetime is effectively promoted to singleton because it's only disposed by ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1 to what @Mpdreamz said.
To elaborate a bit further, your current implementation of
will cause each repository to have their own
ConnectionSettings
caches, which is definitely better thanAddScoped
which will create a newConnectionSetting
s every time, basically defeating the purpose caching (hence the 50ms vs 2ms difference you observed).But injecting a singleton instance of
ConnectionSettings
as Martijn suggested into each repository will cause all your repositories to share the same caches which will result in overall better performance.ConnectionSettings needs to be a singleton its what all the caches are leaning on.
ElasticClient may or may not be registered as a singleton, its thread safe so using a single instance is just fine.
I’d definitely make
IElasticClient
a constructor dependency that gets injected intoProcessDataApiKeyRepository