【问题标题】:TypeLoadException, Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatterTypeLoadException,Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter
【发布时间】:2019-10-19 19:32:49
【问题描述】:

考虑我的项目文件:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="3.0.0-preview4-19123-01" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

我的Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    [..]

    services.
        AddMvc().
        AddNewtonsoftJson(); // Here is the problem
}

每当我尝试加载 NewonsoftJson 格式化程序时,我都会在运行我的 API 时看到此视图:

TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter' from assembly 'Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

我应该如何解释这一点,为什么格式化程序不在预期的位置?它甚至在寻找正确的格式化程序吗?

【问题讨论】:

  • 您是否尝试删除 obj 和 bin 文件夹并重新构建?
  • 是的,很多次。
  • 删除 Microsoft.AspNetCore.Mvc.Core 并将其他软件包升级到 3.0.0-preview5-*
  • 如果我删除这个包我根本无法添加 JsonFormatters,扩展方法 AddNewtonsoftJson 位于这个包中。我的解决方案中没有包的精确预览匹配。我已经尝试了最新的所有方法,结果相同。
  • 你用的是什么sdk? dotnet --version。我只是使用 dotnet new mvc 和 sdk 3.0.100-preview5-011568 创建了一个新的 mvc 项目,它编写了代码:services.AddControllersWithViews() .AddNewtonsoftJson(); 并添加 在 csproj 中。

标签: .net asp.net-web-api asp.net-core json.net asp.net-core-webapi


【解决方案1】:

我也遇到了这个错误,但是使用

services.AddMvcCore().AddNewtonsoftJson();

目前在 dotnet core 3.0 preview 5 中工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多