0

I would like to use one httpclient to many method in class. Below is the simplified code:

public class test{

private readonly HttpClient _httpClient;

public Test(){
_httpClient = new HttpClient();
}

public void method1(){
using (_httpClient){
//...
}
}

public void method2(){
using (_httpClient){
//...
}
}

public void method3(){
using (_httpClient){
//...
}
}
}

Then it calls the method data:

public async static void TestHttpClient()
        {
            Test test1 = new Test();
            test1.Method1();
            test1.Method2();
            test1.Method3();
        }

Method 1 is working. When calling the second one I get the message: "You cannot access a deleted object."

Thanks for helps.

Regards

5
  • Why do you want to reuse the same client? Is creating multiple clients a performance problem for you? Commented Feb 10, 2022 at 15:27
  • 3
    You shouldn't keep creating and disposing HttpClient - Look at this aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong Commented Feb 10, 2022 at 15:28
  • 1
    @RubbleFord That article is outdated given the introduction of IHttpClientFactory. Commented Feb 10, 2022 at 15:30
  • Your using statement is causing your error. I wouldn't use a static instance. Commented Feb 10, 2022 at 15:30
  • HttpClientFactory is the way to go these days. The HttpClient life cycle is very complicated; the factory manages all those details Commented Feb 10, 2022 at 16:01

3 Answers 3

3

using calls the Dispose() method after the scope - which destroys the object - keep the instance of your HttpClient within the instance of your object

public class test : IDisposable
{
    private readonly HttpClient _httpClient;
    public test()
    {
        _httpClient = new HttpClient();
    }

    public void Dispose()
    {
        _httpClient.Dispose();
    }

    public void method1()
    {
        //...
    }
}

then you can dispose your object instead of the HttpClient

using(test myObject = new test()) 
{
    myObject.method1();
    myObject.method2();
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want Test to create and reuse a disposable resource (e.g. HttpClient, then Test should implement IDisposable and should dispose of the resource in its Dispose method. That means that the class using Test should use a using block:

public async static void TestHttpClient()
{
    using (Test test1 = new Test())
    {   
        test1.Method1();
        test1.Method2();
        test1.Method3();
    }
}

Comments

0

One way to do it is to create a private variable and a get accessor

    private HttpClient _httpClient;
    
    private HttpClient MyClient 
    {
        get {
            if (_httpClient == null)
            {
                _httpClient = new HttpClient
                {
                    BaseAddress = new Uri($"https://your.url/")
                };
                //Other client logic goes here
            }                
            return _httpClient;
        }
    }

Then from your method, you just reference the accessor

public async Task method1()
{
    await MyClient.Post() //post logic here
   //...
}

You don't need to dispose HttpClient, MS recommends leaving the object in place, unless you know you need to forcibly close the connection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.