I know that httpclient should be reused, but i'm curious if the same rules apply to the other objects used in this method specifically the JsonSerializer object when deserializing.
public async Task<T> PostAsync<T>(string actionName, object postData)
{
try
{
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(actionName));
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Content = new StringContent(postData.ToString(), Encoding.UTF8, "application/json");
var newClient = _httpClientFactory.CreateClient();
newClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _tokenModel.Token);
var result = await newClient.PostAsync(request.RequestUri, request.Content);
using (var streamReader = BuildNonClosingStreamReader(await result.Content.ReadAsStreamAsync()))
using (var jsonReader = new JsonTextReader(streamReader))
{
JsonSerializer s = new JsonSerializer();
return s.Deserialize<T>(jsonReader);
}
}
catch (Exception)
{
throw;
}
}