1

I am getting an error on AuthTokenRS value = await response.Content.ReadAsAsync(); The error message is:

'HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method 'ReadAsAsync' and no extension method 'ReadAsAsync' accepting a first argument of type 'HttpContent' could be found(are you missing a using directive or an assembly reference?).

Here is my code snippet:

public async Task<HttpResponse<AuthTokenRS>> AuthorizeAsync(string credentials)
{
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri(this.config.Environment);
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));

        var args = new Dictionary<string, string>();
        args.Add("grant_type", "client_credentials");
        var content = new FormUrlEncodedContent(args);
        Logger.DebugFormat("POST {0}\n{1}", AuthorizationEndpoint, await content.ReadAsStringAsync());

        var response = await client.PostAsync(AuthorizationEndpoint, content);
        string requestUri = response.RequestMessage.RequestUri.ToString();
        if (response.IsSuccessStatusCode)
        {
            //AuthTokenRS value = await response.Content.ReadAsAsync<AuthTokenRS>();
            AuthTokenRS value = await response.Content.ReadAsAsync<AuthTokenRS>();

            return HttpResponse<AuthTokenRS>.Success(response.StatusCode, value, requestUri);
        }
        else
        {
            return HttpResponse<AuthTokenRS>.Fail(response.StatusCode, await response.Content.ReadAsStringAsync(), requestUri);
        }
    }
}
Filnor
  • 1,290
  • 2
  • 23
  • 28
Waqi
  • 13
  • 1
  • 5
  • Have you seen this solution? https://stackoverflow.com/questions/14520762/system-net-http-httpcontent-does-not-contain-a-definition-for-readasasync-an – ObiEff Oct 11 '17 at 10:38
  • https://stackoverflow.com/questions/10399324/where-is-httpcontent-readasasync – qxg Oct 11 '17 at 10:40
  • Thank you everyone for your feedback, but this is still not working, I have located the file and added ref into the application seems no joy. Any other help please :) ? – Waqi Oct 16 '17 at 11:37
  • I know that seniors here marked this one as duplicate but all the references have been referred are old and doesn't work. So if anyone is still facing the same problem please install following nuget..... so for 2015 or 5.2.3, install following nuget package https://www.nuget.org/packages/System.Net.Http.Formatting.Extension/ – Waqi Oct 17 '17 at 11:40

0 Answers0