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.

make ElasticClient IDisposable

See original GitHub issue

Currently the ElasticClient is created, it is not possible to get rid of it. I would love to be able to do something like:

using (var settings) { settings.GlobalHeaders(new NameValueCollection { { "Authorization", $"Bearer {token}" } }; var client = new ElasticClient(settings); var results = client.DoSomething(); // return results; }

The problem with the ElasticClient is very static, it would be helpful in a MVC API Controller, we can switch “security context” of the connection.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dliulabscommented, Jan 31, 2022

Hi @stevejgordon,

Okay, I tweak a little of my code and was lucky to have found the right syntax. This one works:

using Nest;
using Elasticsearch.Net;
using Elastic.Transport;

var blank_settings = new ConnectionSettings (new Uri (ELASTIC_URI))
    .PrettyJson(true);
var blank_client = new ElasticClient(blank_settings);

var r = await blank_client.Cat.IndicesAsync(
    c => c
    .AllIndices()
    .RequestConfiguration(r => r.BasicAuthentication(ELASTIC_USER, ELASTIC_PASS))
);

So if this per-call configuration does not keep anything in the ElasticClient. You are right, we can use a Singleton ElasticClient and just supply needed credentials at per-request basis.

Thank you for all your help.

0reactions
stevejgordoncommented, Jan 31, 2022

@dliulabs The code in my comment above should work on your API call. It’s not r => r.Authentication but r => r.BasicAuthentication("username", "password")

Read more comments on GitHub >

github_iconTop Results From Across the Web

ElasticClient Dispose - nest
The ElasticClient holds a single IConnection responsible for doing async and sync http calls. The IConnection does not reuse httprequests ...
Read more >
Lifetimes | Elasticsearch .NET Clients [7.17]
If you are using an IOC/Dependency Injection container, it's always useful to know the best practices around the lifetime of your objects.
Read more >
Step one - A simple Elastic Search Repository using NEST
By creating a fixture-class that inherits from IDisposable, you can put your setup in the constructor and your teardown in the Dispose-method.
Read more >
HttpClient Creation and Disposal Internals ... - Steve Gordon
HttpClient is an example where the often blanket rule of making sure we always dispose of IDisposable objects breaks down.
Read more >
C# IDisposable Example vs IAsyncDisposable
This tutorial will cover how to implement IDisposable in C#. Then, we will compare our C# IDisposable example with its async alternative. Let's ......
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