'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method

后端 未结 6 2165
梦如初夏
梦如初夏 2020-12-02 16:27

I made a console app to consume a Web API I just made. The console app code does not compile. It gives me the compilation error:

\'System.Net.Http.HttpConten         


        
相关标签:
6条回答
  • 2020-12-02 16:45

    After a long struggle, I found the solution.

    Solution: Add a reference to System.Net.Http.Formatting.dll. This assembly is also available in the C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies folder.

    The method ReadAsAsync is an extension method declared in the class HttpContentExtensions, which is in the namespace System.Net.Http in the library System.Net.Http.Formatting.

    Reflector came to rescue!

    0 讨论(0)
  • 2020-12-02 16:48

    USE This Assembly Referance in your Project

    Add a reference to System.Net.Http.Formatting.dll
    
    0 讨论(0)
  • 2020-12-02 16:54

    Make sure that you have installed the correct NuGet package in your console application:

    <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" />
    

    and that you are targeting at least .NET 4.0.

    This being said, your GetAllFoos function is defined to return an IEnumerable<Prospect> whereas in your ReadAsAsync method you are passing IEnumerable<Foo> which obviously are not compatible types.

    Install-Package Microsoft.AspNet.WebApi.Client

    0 讨论(0)
  • 2020-12-02 16:58

    Adding a reference to System.Net.Http.Formatting.dll may cause DLL mismatch issues. Right now, System.Net.Http.Formatting.dll appears to reference version 4.5.0.0 of Newtonsoft.Json.DLL, whereas the latest version is 6.0.0.0. That means you'll need to also add a binding redirect to avoid a .NET Assembly exception if you reference the latest Newtonsoft NuGet package or DLL:

    <dependentAssembly>
       <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
     </dependentAssembly> 
    

    So an alternative solution to adding a reference to System.Net.Http.Formatting.dll is to read the response as a string and then desearalize yourself with JsonConvert.DeserializeObject(responseAsString). The full method would be:

    public async Task<T> GetHttpResponseContentAsType(string baseUrl, string subUrl)
    {
         using (var client = new HttpClient())
         {
             client.BaseAddress = new Uri(baseUrl);
             client.DefaultRequestHeaders.Accept.Clear();
             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
             HttpResponseMessage response = await client.GetAsync(subUrl);
             response.EnsureSuccessStatusCode();
             var responseAsString = await response.Content.ReadAsStringAsync();
             var responseAsConcreteType = JsonConvert.DeserializeObject<T>(responseAsString);
             return responseAsConcreteType;
          }
    }
    
    0 讨论(0)
  • 2020-12-02 16:59

    or if you have VS 2012 you can goto the package manager console and type Install-Package Microsoft.AspNet.WebApi.Client

    This would download the latest version of the package

    0 讨论(0)
  • 2020-12-02 17:06
    • if you unable to find assembly reference from when (Right click on reference ->add required assembly)

    try this Package manager console
    Install-Package System.Net.Http.Formatting.Extension -Version 5.2.3 and then add by using add reference .

    0 讨论(0)
提交回复
热议问题