Why the swagger doesn't open in .NET 6?
Asked Answered
B

1

10

Well, I was doing a DDD project, specifically using redis, but I don't think that has anything to do with it.

The problem is, the swagger doesn't appear to me, it fails, but when I make requests in postman it works normally.

Thats the error:

 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
  An unhandled exception has occurred while executing the request.
  System.TypeLoadException: Could not load type 'Microsoft.AspNetCore.Http.Metadata.ITagsMetadata' from assembly 'Microsoft.AspNetCore.Http.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
     at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorOptions.DefaultTagsSelector(ApiDescription apiDescription)
     at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorOptions.DefaultSortKeySelector(ApiDescription apiDescription) in Swashbuckle.AspNetCore.SwaggerGen.dll:token 0x600012d+0x0
     at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count) in System.Linq.dll:token 0x600040b+0x10
     at System.Linq.EnumerableSorter`1.ComputeMap(TElement[] elements, Int32 count) in System.Linq.dll:token 0x6000401+0x0
     at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count) in System.Linq.dll:token 0x6000402+0x0
     at System.Linq.OrderedEnumerable`1.GetEnumerator()+MoveNext() in System.Linq.dll:token 0x6000391+0x3d
     at System.Linq.Lookup`2.Create(IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer) in System.Linq.dll:token 0x6000366+0x2b
     at System.Linq.GroupedEnumerable`2.GetEnumerator() in System.Linq.dll:token 0x600035f+0x0
     at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository) in Swashbuckle.AspNetCore.SwaggerGen.dll:token 0x60000f8+0x3a
     at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath) in Swashbuckle.AspNetCore.SwaggerGen.dll:token 0x60000f6+0xe6
     at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) in Swashbuckle.AspNetCore.Swagger.dll:token 0x6000009+0xe2
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) in Microsoft.AspNetCore.Diagnostics.dll:token 0x60000aa+0x82

Startup file (ConfigureServices):

public void ConfigureServices(IServiceCollection services)
{
        services.AddRedisContext(Configuration);
        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "Basket.Api", Version = "v1" 
        });
});

My method in extension method class:

public static IServiceCollection AddRedisContext(this IServiceCollection services, IConfiguration config)
{
        services.AddStackExchangeRedisCache(options =>
        {                
            options.Configuration = config["CacheSettings:ConnectionString"];
        });

        services.AddScoped<IBasketRepository, BasketRepository>();
        services.AddScoped<IBasketService, BasketService>();

        return services;
 }

The Package reference:

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>    
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Basket.Infra.Data\Basket.Infra.Data.csproj" />
  </ItemGroup>

</Project>

I also commented the lines referring to redis and it still gave the same error. So it's almost certainly something involving the swagger.

Babylonia answered 6/11, 2021 at 5:50 Comment(9)
Can you please post minimal reproducible example somewhere?Errolerroll
Ok, following the divide and conquer strategy, I commented the extension method part of redis and it kept giving an error, I commented the swagger part and stopped the error, but also obviously the swagger didn't open.Babylonia
Could you show the list of PackageReference in your csproj?Pankhurst
I edited the question with PackageReference.Babylonia
I got something, but I believe it's not really a solution, but it's at least working. I simply downgraded to version 6.2.2 and it worked. But there's a project with the same version in my solution and it's working, it doesn't make sense to me lolBabylonia
Same issue here, upgraded swashbuckle to the latest version. Also updated to .NET 6.0 and getting Could not load type 'Microsoft.AspNetCore.Http.Metadata.ITagsMetadata' from assembly 'Microsoft.AspNetCore.Http.Abstractions, Version=6.0.0.0Horrid
I don't know if it helps you doing that, but if you downgrade you swashbuckle to the version 6.2.2 could works. Because for me it working good.Babylonia
I have the same situation, maybe the latest Swashbuckle version 6.2.3 is broken and not working with net6.0, scenarios I tested: net5.0 and Swashbuckle 6.2.3 = working; net6.0 and Swashbuckle 6.2.3 = NOT working; net6.0 and Swashbuckle 6.2.2 = working;Tramroad
I found this GitHub issue which is related to net6.0 and the "ITagsMetadata" part of the error message: github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2192Tramroad
T
3

In my case, it was the SDK not running the proper net6.0 version.

While the whole project was using new net6.0 NuGet packages, the local SDK on that one machine I was working on was still net5.0. After installing .net6.0, Swashbuckle 6.2.3 was working again, and all was as expected.

Tramroad answered 15/11, 2021 at 6:16 Comment(1)
At properties of my project the target framework is net6.0 and at the terminal I wrote dotnet --list-sdks and appeared the 6.0.300 version. Dou you know something that can I do to use the swagger?Lightship

© 2022 - 2024 — McMap. All rights reserved.