0

I have a old project in .NET Framework-4. I am to implement HttpClient, So I installed:

<packages>
  <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
  <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net40" />
  <package id="System.Net.Http" version="2.0.20710.0" targetFramework="net40" />
</packages>

Then I have this code:

private readonly HttpClient _client = new HttpClient();
public async Task<HttpResponseMessage> PostAsync(string url, IDictionary<string, string> headerInfo, string payload)
{
    try
    {
        _client.BaseAddress = new Uri(url, UriKind.RelativeOrAbsolute);
        HttpContent content = new StringContent(payload, Encoding.UTF8);
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        return await _client.PostAsync(url, content);
    }
    catch (Exception ex)
    {
        Logger.WriteLog(ex.ToString());
    }
    return null;
}


public async Task<HttpResponseMessage> PostOrPutRequest(string uri, HttpMethod methodType, Dictionary<string, string> headers = null, object model = null)
{
    HttpResponseMessage response = null;
    if (headers == null)
        headers = new Dictionary<string, string>();

    if (model != null)
    {
        string request = "";
        if (model.GetType() != typeof(string))
        {
            request = JsonConvert.SerializeObject(model);
        }
        else
        {
            request = model.ToString();
        }

        using (var httpClient = new HttpClient())
        {
            var requestMessage = new HttpRequestMessage(methodType, uri);
            try
            {
                headers.Add("TimeStamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                if (headers != null)
                    foreach (var s in headers)
                    {
                        requestMessage.Headers.TryAddWithoutValidation(s.Key, s.Value);
                    }
                requestMessage.Content = new StringContent(request);
                requestMessage.Content.Headers.ContentType.MediaType = "application/json";

                Logger.WriteLog(@"Sending request {requestMessage}....");
              var  response =  await httpClient.PostAsync(requestMessage);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex.ToString());
            }
        }
    }
    return response;
}

But I got this error:

Error   CS1061  'Task<HttpResponseMessage>' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'Task<HttpResponseMessage>' could be found (are you missing a using directive or an assembly reference?)

Then this line of code is highlighted:

await _client.PostAsync(url, content);

and

await httpClient.PostAsync(requestMessage)

How do I get this sorted out?

It's an old project that users are already using that needs few modifications.

Bami
  • 383
  • 1
  • 4
  • 10
  • 1
    Does this help? https://stackoverflow.com/questions/11853812/task-does-not-contain-a-definition-for-getawaiter?rq=2 – DavidG May 11 '23 at 11:48

0 Answers0