question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

asp.net core ElasticClient DI scope

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
gmarzcommented, Jul 13, 2016

+1 to what @Mpdreamz said.

To elaborate a bit further, your current implementation of

services.AddSingleton<IProcessDataApiKeyRepository, ProcessDataApiKeyRepository>();

will cause each repository to have their own ConnectionSettings caches, which is definitely better than AddScoped which will create a new ConnectionSettings 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.

3reactions
Mpdreamzcommented, Jul 13, 2016

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 into ProcessDataApiKeyRepository

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found