Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys/girls i am having a bit of problem. i have a method that performs a get request. this method works. so i re-factored the method, so that certain parts can be used for other methods.

//Original Method
public async Task<T> GetByIdAsync(T returnType, T1 controllerName, T2 Id)
{
  var url = baseUrl + controllerName + "/" + Id;
  var result = returnType;
  using (HttpClient client = new HttpClient())
  {
     client.BaseAddress = new Uri(url);
     client.DefaultRequestHeaders.Accept.Clear();
     client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

      var response = await client.GetAsync(url);
      if (response.IsSuccessStatusCode)
      {
         var data = response.Content.ReadAsStringAsync();
          result = JsonConvert.DeserializeObject(data.Result);
       }
    }
}


//refactored method        
public async Task<T> GetByIdAsync<T, T1, T2>(T returnType, T1 controllerName, T2 Id)
{
    var url = baseUrl + controllerName + "/" + Id;
    var result = returnType;
    var client = GetClient(url);

    var response = await client.GetAsync(url);
    if (response.IsSuccessStatusCode)
    {
        var data = response.Content.ReadAsStringAsync();
        result = JsonConvert.DeserializeObject<T>(data.Result);
    }
    return result;
}

public static HttpClient GetClient(string url)
{
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        return client;
    }
}


After i re-factored the method, it no longer awaits. what is wrong ? .

What I have tried:

the original code works, i am trying to understand why the re-factored code doesn't work
Posted
Updated 15-May-17 6:03am
v2

1 solution

"using" disposes (destroys) an object when it leaves the using context, so your HttpClient is not usable outside of the GetClient method. Remove the "using"

C#
public static HttpClient GetClient(string url)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
 
    return client;
}
 
Share this answer
 
Comments
rudolph098 15-May-17 12:16pm    
Thanks, it now works. But are there any possible side effects that may arise from removing the using statement in the GetClient(url) method ?
F-ES Sitecore 15-May-17 12:21pm    
None, it's just a short-hand way of writing this

HttpClient client;
try
{
client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

return client;
}
catch
{
throw;
}
finally
{
client.Dispose();
}

The important bit is the call to Dispose in "finally", that's why you use "using" and that's exactly the line you don't need.
rudolph098 15-May-17 12:25pm    
Thanks for the explanation
Richard Deeming 16-May-17 10:51am    
Confusingly, even though it implements IDisposable, the HttpClient should almost never be wrapped in a using statement:
You're using HttpClient wrong and it is destabilizing your software : ASP.NET Monsters[^]
rudolph098 16-May-17 11:55am    
Wow!!!!, thanks for this insightful article.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900