I'm developing a library with C# and .NET Framework 4.0.
I have this method to PUT something on a ASP.NET Web Api 2 web service.
public async Task<string> PrepareAndStartv2(
string orderNumber,
string userName,
string systemName)
{
string uri =
string.Format(PrepareAndStartUri, orderNumber, userName, systemName);
string batchName = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(webApiHost);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
Task<HttpResponseMessage> response = client.PutAsync(uri, content);
HttpResponseMessage message = response.Result;
Task<string> task = message.Content.ReadAsStringAsync();
batchName = await task;
}
return batchName;
}
I added message.Content.ReadAsStringAsync()
to get a string returned by the method. And after that I've been forced to and Task<string>
, await
and async
everywhere.
Is there another option to read a string in a HttpContent
without using await
, Task<string>
and async
?
Maybe running a Task inside this PrepareAndStartv2
method that wait will I'm reading the string with ReadAsStringAsync()
.