Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dmitry-pavlov opened this issue May 24, 2020 · 19 comments

Comments

@dmitry-pavlov
Copy link

dmitry-pavlov commented May 24, 2020

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

@dmitry-pavlov
Copy link
Author

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

@dmitry-pavlov
Copy link
Author

dmitry-pavlov commented May 24, 2020

Might be related to #1961 (comment)

@maxberghammer
Copy link

Same Problem here :(

@RicoSuter
Copy link
Owner

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

@RicoSuter
Copy link
Owner

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

@SeppPenner
Copy link

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

@statler
Copy link

statler commented Nov 4, 2020

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

@RicoSuter
Copy link
Owner

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?).

@statler
Copy link

statler commented Nov 5, 2020

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.

@vgb1993
Copy link

vgb1993 commented Dec 1, 2020

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?

@LUISURBM
Copy link

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.

@dgancho
Copy link

dgancho commented Jan 18, 2021

Any update here? I have a similar exception in Startup of a FunctionApp on .NET Core 3.1

public class Startup : FunctionsStartup
{
   public override void Configure(IFunctionsHostBuilder builder)
   {
       builder.Services.AddMvcCore().AddJsonOptions(o =>
       {
           o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
           o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
       });
   }
}

A host error has occurred during startup operation '9eae7ea7-d20b-4c8f-a5bc-f6d4ada2d40e'.
Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.1.10.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

@RicoSuter
Copy link
Owner

RicoSuter commented Jan 18, 2021

In AspNetCoreOpenApiDocumentGenerator.cs we have

image

I wonder why this exception bubbles up?
Should fallback to System.Text.Json...

@jeremyVignelles
Copy link
Collaborator

jeremyVignelles commented Jan 18, 2021

Sorry for the last post. After a bit of thinking, I think that you get an IOption<MvcJsonOptions> at line 70 but the type inside the generic is probably not full resolved until you call .Value on it, which could explain why it throws outside the catch.

EDIT: This method could be rewritten to remove the usage of the dynamic, by writing returns instead of storing the value inside options

@RicoSuter
Copy link
Owner

RicoSuter commented Jan 18, 2021

Oh yes, maybe we need a trycatch around that as well so that the fallback works as expected... maybe this would already fix it:

image

@dmitry-pavlov
Copy link
Author

Any chance to get rid of adding Microsoft.AspNetCore.Mvc.NewtonsoftJson package when selected System.Text.Json as JSON Library @RicoSuter ?

image

@RicoSuter
Copy link
Owner

@dmitry-pavlov is this still a problem with the v14 previews?

@dmitry-pavlov
Copy link
Author

Yeah, now it's gone in v14 preview. Cool 👍 Thanks @RicoSuter !

@dmitry-pavlov
Copy link
Author

Just a note how to handle JSON serialization options with System.Text.Json and base class:

  • remove Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package reference from you project
  • use SystemTextJson and base class - add this to your nswag.json - e.g. BaseClient
    image
  • add BaseClient.cs like this
    image
  • so your generated clients will be calling UpdateJsonSerializerSettings method from the base class
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants