我正在使用.NET Core 1.1 ASP.NET MVC项目,并且在尝试解析来自我的某个API的响应时遇到了以下错误。 根据我的阅读,我应该在我的项目中使用Web API客户端,我就是这样。
这让我发疯了。有什么问题?
处理请求时发生未处理的异常。
TypeLoadException:Method' SerializeToStreamAsync'在类型 ' System.Net.Http.ObjectContent'从装配 ' System.Net.Http.Formatting,Version = 5.2.3.0,Culture = neutral, 公钥= 31bf3856ad364e35'没有实施。 System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent 内容,类型类型,IEnumerable格式化程序, IFormatterLogger formatterLogger,CancellationToken cancellationToken)
public async Task<IEnumerable<Menu>> GetAll()
{
List<Menu> result;
var response = await http.GetAsync("/Menu/GetAll");
if (response.IsSuccessStatusCode)
result = await response.Content.ReadAsAsync<List<Menu>>();
else
throw new HttpRequestException(response.ReasonPhrase);
return result;
}
public class MenuRepository
{
private readonly MainContext context;
public MenuRepository(MainContext context)
{
this.context = context;
}
public IEnumerable<Menu> GetAll()
{
var menus = context.Menus
.Include(m => m.Vendor)
.AsNoTracking();
return menus;
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
<PackageReference Include="Microsoft.Identity.Client" Version="1.1.0-preview" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />
<PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" />
<PackageReference Include="system.xml.xmlserializer" Version="4.3.*" />
<PackageReference Include="WinInsider.System.Net.Http.Formatting" Version="1.0.5" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Alfred.Models\Alfred.Models.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Views\Menu\" />
</ItemGroup>
</Project>
答案 0 :(得分:0)
尝试使用以下语法
public async Task<IActionResult> GetAll() {
var response = await http.GetAsync("/Menu/GetAll");
if (response.IsSuccessStatusCode) {
var result = await response.Content.ReadAsAsync<List<Menu>>();
return Ok(result);
} else {
return StatusCode((int)response.StatusCode);
}
}