6

I currently have an instance variable of a HttpClient which is used in an instance method to download an image. The method is always called in a Task.Run(). This method can be called thousands of times in a minute and doesn't crash or produce any errors. But I just wanted to know what benefits, if any, there would be if I switched to a static HttpClient, apart from perhaps being more thread safe.

Here is some code for context:

HttpClient client = new HttpClient(); // Make this static?

// Always called in a Task.Run(() => DownloadImage(Obj));
public async void DownloadImage(Object obj)
{
    FormUrlEncodedContent formContent = GetFormContent(Obj);
    HttpResponseMessage Result = await client.PostAsync("URL", formContent).ConfigureAwait(false);
    byte[] Data = Result.Content.ReadAsByteArrayAsync().Result;
    StaticClass.Images[Obj.ID] = ImageSource.FromStream(() => new MemoryStream(Data));
    formContent.Dispose();
    Result.Dispose();
}
7
  • if DownloadImage is being called on the same instance of whatever class this code is in, then there is no difference Commented Oct 25, 2017 at 18:49
  • @Jonesopolis Yep, that's the case. Commented Oct 25, 2017 at 18:55
  • 1
    Does anyone know when it would be appropriate to make a static HttpClient that is often reused? This post recommends making it so: aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong Commented Oct 25, 2017 at 18:57
  • 1
    HttpClient is fully thread-safe and reentrant, so making it a static variable is actually the preferred way of interacting with it. Commented Oct 25, 2017 at 18:57
  • 1
    what is temp? And why isn't your DownloadImage async? Commented Oct 25, 2017 at 19:06

2 Answers 2

2

No

Since you are not using async version and you are calling ReadAsByteArrayAsync().Result you will most likely end up in deadlocks.

Recommended

Following is the most recommended way,

static HttpClient client = new HttpClient ();

public async Task DownloadImage(Object obj)
{
    using(FormUrlEncodedContent formContent = GetFormContent(Obj)) {
       using(HttpResponseMessage Result = await 
           client.PostAsync("URL", formContent)
           .ConfigureAwait(false)){
              byte[] Data = await Result.Content.ReadAsByteArrayAsync();
              StaticClass.Images[Obj.ID] = ImageSource.FromStream(
                   () => new MemoryStream(Data));
        }
    }

}

Also don't call Dispose, use using block, Dispose will not be executed if an exception is thrown, using block will properly Dispose even in case of exception.

As long as, all disposables are wrapped in using and async await are used properly, HttpClient will perform best with single static instance.

Sign up to request clarification or add additional context in comments.

Comments

0

HttpClient is intented by microsoft to be used as a static object. Even if the time to instatiate it every time is trivial, because other issues could arise. his link contains a simple implementation : https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client .

Since you will be using the client in more more cases than simple getting an image I would suggest a class with static methods (async if you may) for get/post/put/delete where you would inserting method names and objects dynamically.

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.