【问题标题】:Why the swagger doesn't open in .NET 6?为什么 .NET 6 中没有打开招摇?
【发布时间】:2021-12-20 00:15:58
【问题描述】:

嗯,我在做一个 DDD 项目,专门使用 redis,但我认为这与它没有任何关系。

问题是,我看不到招摇,它失败了,但是当我在邮递员中提出请求时,它可以正常工作。

这是错误:

 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

启动文件(ConfigureServices):

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

我在扩展方法类中的方法:

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;
 }

包参考:

<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>

我还评论了引用 redis 的行,它仍然给出了同样的错误。所以几乎可以肯定这涉及到招摇。

【问题讨论】:

  • 你能在某处发帖minimal reproducible example吗?
  • 我得到了一些东西,但我相信这不是一个真正的解决方案,但它至少可以工作。我只是降级到版本 6.2.2 并且它工作。但是我的解决方案中有一个具有相同版本的项目并且它正在运行,这对我来说没有意义lol
  • 我不知道它是否能帮助你做到这一点,但如果你降级你的 swashbuckle 到版本 6.2.2 可以工作。因为对我来说效果很好。
  • 我也有同样的情况,可能是最新的Swashbuckle 6.2.3版本坏了,不能和net6.0一起工作,我测试的场景:net5.0和Swashbuckle 6.2.3=工作; net6.0 和 Swashbuckle 6.2.3 = 不工作; net6.0 和 Swashbuckle 6.2.2 = 工作;
  • 我发现了这个与net6.0有关的GitHub问题和错误消息的“ITagsMetadata”部分:github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2192

标签: c# swagger swashbuckle.aspnetcore .net-6.0


【解决方案1】:

在我的情况下,是 SDK 没有运行正确的 net6.0 版本。

虽然整个项目都使用新的 net6.0 NuGet 包,但我正在使用的那台机器上的本地 SDK 仍然是 net5.0。安装 .net6.0 后,Swashbuckle 6.2.3 再次运行,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2021-08-08
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多