使用.NET Core 3.0添加swagger时出现异常

使用.NET Core 3.0添加swagger时出现异常,swagger,swashbuckle,asp.net-core-3.0,Swagger,Swashbuckle,Asp.net Core 3.0,我试图将swagger集成到ASP NET Core 3.0项目中,它在ConfigureServices方法中抛出异常: 我正在使用swashback.AspNetCore4.0.1 我也检查了这个,还有 例外情况 System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service des

我试图将swagger集成到
ASP NET Core 3.0
项目中,它在
ConfigureServices
方法中抛出异常:

我正在使用
swashback.AspNetCore
4.0.1

我也检查了这个,还有

例外情况

System.AggregateException
  HResult=0x80131500
  Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Swashbuckle.AspNetCore.Swagger.ISwaggerProvider Lifetime: Transient ImplementationType: Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator': Failed to compare two elements in the array.) (Error while validating the service descriptor 'ServiceType: Swashbuckle.AspNetCore.SwaggerGen.ISchemaRegistryFactory Lifetime: Transient ImplementationType: Swashbuckle.AspNetCore.SwaggerGen.SchemaRegistryFactory': Failed to compare two elements in the array.)
  Source=Microsoft.Extensions.DependencyInjection
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
   at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at SXS.Server.Program.Main(String[] args) in C:\Work\SXS\SXS\Core\Server\SXS.Server\Program.cs:line 32

Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Swashbuckle.AspNetCore.Swagger.ISwaggerProvider Lifetime: Transient ImplementationType: Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator': Failed to compare two elements in the array.

Inner Exception 2:
InvalidOperationException: Failed to compare two elements in the array.

Inner Exception 3:
TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
p.S我遵循了这一点,唯一的区别是,虽然他使用的是
openapinfo
对象,但我没有可用的重载,因此我使用的是
Info
我只是访问了docs.microsoft.com,没有问题


文档说明您需要使用Swashback.AspNetCore的最新预览版本,5.0.0-rc4

这通常是由于使用了错误的swagger软件包,因此信息api使用了Swashback.AspNetCore.swagger.Info,这导致了问题

如果您使用最新的Swashback.AspNetCore、5.0.0-rc4m软件包,那么它将从新的Microsoft.OpenApi.Models中获取OpenApiInfo,然后就可以工作了


快乐编码

我试过这个,效果很好:

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo
            {
                Version = "v1",
                Title = "My API",
                Description = "My API description for blah blah"
            });
        });

顺便说一句,我使用的是.Net Core 2.1

我在使用.Net Core 3.1.2和Swagger 5.4.1时遇到了这个问题

结果我忘了添加
services.AddControllers()到我的
启动::配置服务
方法


我只是在添加Razor页面。

在.Net Core 5.0.0-preview.4中,在Core 3.1和Swagger 5.5.1中,我也需要添加
services.AddControllers()
endpoints.MapControllers()
services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo
            {
                Version = "v1",
                Title = "My API",
                Description = "My API description for blah blah"
            });
        });