Issues with swagger after migrating to .NET Core 3.0

10,285

There is a discussion on the aspnet/AspNetCore github repo discussing this: Restore MvcJsonOptions to ASP.NET Core 3.0 as a type forward?

It suggests the issue was resolved.

I'd suggest ensuring you have the latest Swashbuckle.AspnetCore package installed: https://www.nuget.org/packages/Swashbuckle.AspNetCore/

In the offical asp.net core docs, it states:

Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc2

https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.0&tabs=visual-studio

So I'd suggest you need to install a pre-release version, at the time of writing only 4.X is stable and 5.0.0-rc3 is the latest.

Share:
10,285
Admin
Author by

Admin

Updated on July 02, 2022

Comments

  • Admin
    Admin almost 2 years

    After migrating to .NET Core 3.0. I'm having issues configuring swagger.

    Following is my configuration.

      public void ConfigureServices(IServiceCollection services)
      {       
    
           services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0).AddNewtonsoftJson();
    
    
           services.AddSwaggerGen(setup =>
           {
                 setup.SwaggerDoc(
                    "v1", 
                     new Info 
                     {  
                         Title = "Docker", 
                         Version = "v1" 
                     });
            });
      }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
           Configuration.SwaggerOptions swaggerOptions = new Configuration.SwaggerOptions();
    
           _configuration.GetSection("SwaggerOptions").Bind(swaggerOptions);
    
           app.UseSwagger(options =>
           {
                options.RouteTemplate = swaggerOptions.JsonRoot;
           });
    
           app.UseSwaggerUI(options =>
           {
               options.SwaggerEndpoint(swaggerOptions.UiEndpoint, swaggerOptions.ApiDescription);
           });
    }
    

    Following is the exception

    TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json

    Is there a solution for this ?

  • Newteq Developer
    Newteq Developer over 4 years
    Moving from 4 to the new 5-RC worked perfectly to resolve this for me
  • inliner49er
    inliner49er over 4 years
    Getting "The type or namespace name 'Info' could not be found" error. Tried installing RC2, 3, and 4. Ideas?
  • Newteq Developer
    Newteq Developer over 4 years
    @inliner49er The Info object has changed to OpenApiInfo and the namespace for OpenApiInfo is Microsoft.OpenApi.Models