Nswag: Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' (Version=3.1.2.0) in NSwag.AspNetCore v 13.5.0

Created on 24 May 2020  路  11Comments  路  Source: RicoSuter/NSwag

I ma getting this error when I am opening in browser API Explorer

Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

  • NSwag.AspNetCore v 13.5.0
  • .NET Core 3.1

image

NSwag.SwaggerGeneration.AspNetCore bug

Most helpful comment

@maxberghammer @RicoSuter The main reason seems to be the missing services.AddControllers() as described here: https://github.com/RicoSuter/NSwag/issues/1961#issuecomment-515631411 (As @dmitry-pavlov already pointed out). I had the same problem today and after adding this one line, it worked :D

All 11 comments

at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.<>c__DisplayClass5_0.<GetJsonSerializerSettings>b__0() at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GetJsonSerializerSettings(IServiceProvider serviceProvider)

Same Problem here :(

Can you create a PR against a sample project in this repo to repro this?

I don't see that in my .net core 3.1 projects...

@maxberghammer @RicoSuter The main reason seems to be the missing services.AddControllers() as described here: https://github.com/RicoSuter/NSwag/issues/1961#issuecomment-515631411 (As @dmitry-pavlov already pointed out). I had the same problem today and after adding this one line, it worked :D

Is this expected to work now? I continue to get this exception, even on a simple project. The solution from #1961 has no effect. Refer attached.

I can continue from the exception, but parameter models are not resolved in controllers.

System.TypeLoadException: 'Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.1.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.'

DataSourceLoadOptionsProblem_withnewton.zip

It seems that we need to catch this error when Newtonsoft is not available and fall back to System.Text.Json (this should be there but probably it stops before it can fall back?).

Also, this error gets thrown in .Net Core 3 where the Microsoft.AspNetCore.Mvc.MvcJsonOptions library is not even available regardless of whether you include NewtonsoftJson and specify .AddNewtonsoftJson(). It seems like it is thrown by the core libraries though

I think the issue of the parameter models is a different issue to do with the modelbinders, and that this error is safely ignorable, but it would be great not to have it raised if it is irrelevant.

I've spent 2 days looking into this after migrating to .NET 5, so if it is irrelevant it would be nice to hide it so no other developers will be left wondering.

You need to enable the all CommonLanguageRuntimeExceptions check to see the exception. This option comes in handy some times when debugging. But this bug throws every time you run if you enable it. https://www.carlosjanderson.com/break-on-all-exceptions/

Startup

 public void ConfigureServices( IServiceCollection services )
        {
            services.AddControllers().AddNewtonsoftJson();
            services.AddOpenApiDocument();
        }
        public void Configure( IApplicationBuilder app, IWebHostEnvironment env )
        {
            app.UseOpenApi();
            app.UseSwaggerUi3();
            app.UseRouting();
            app.UseEndpoints( endpoints =>
            {
                endpoints.MapControllers();
            } );
        }

Project

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="NSwag.Annotations" Version="13.9.4" />
    <PackageReference Include="NSwag.AspNetCore" Version="13.9.4" />
    <PackageReference Include="NSwag.Generation.AspNetCore" Version="13.9.4" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Controllers\" />
  </ItemGroup>
</Project>

Exception

System.TypeLoadException: 'Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.'

   at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.<>c__DisplayClass5_0.<GetJsonSerializerSettings>b__0()
   at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GetJsonSerializerSettings(IServiceProvider serviceProvider)

Any thoughts?

System.TypeLoadException: 'Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.'
After upgraded .Net Core 3.1 to 5.0

And

    public void ConfigureServices( IServiceCollection services )
    {
        services.AddControllers();
    }

I am ignoring exception as it is not blocking my app, any help please is welcome.

Was this page helpful?
0 / 5 - 0 ratings