【问题标题】:Exception when adding swagger with .NET Core 3.0使用 .NET Core 3.0 添加 swagger 时出现异常
【发布时间】:2019-11-05 10:18:50
【问题描述】:

我正在尝试将 swagger 集成到 ASP NET Core 3.0 项目中,它会在 ConfigureServices 方法中引发异常:

我正在使用Swashbuckle.AspNetCore 4.0.1。

我也检查了这个issuethis

public void ConfigureServices(IServiceCollection services)
        {


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
                {
                    Version = "v1",
                    Title = " API",
                    Description="API for the  Server",
                });

            });
        }

例外

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我已经关注了这个guide,唯一的区别是,虽然他使用了OpenApiInfo 对象,但我没有可用的重载,因此我使用的是Info

【问题讨论】:

    标签: swagger swashbuckle asp.net-core-3.0


    【解决方案1】:

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

    原来我忘了将services.AddControllers(); 添加到我的Startup::ConfigureServices 方法中。

    我只是添加 Razor 页面。

    【讨论】:

    • 我在 .Net Core 5.0.0-preview.4 中也是如此
    • 在 Core 3.1 和 Swagger 5.5.1 中,我必须添加 services.AddControllers();endpoints.MapControllers();
    【解决方案2】:

    我刚刚在 docs.microsoft.com 上通过 Get started with Swashbuckle and ASP.NET Core 并没有任何问题。

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

    【讨论】:

      【解决方案3】:

      这通常是由于使用了错误的 swagger 包,因此 Info api 使用了 Swashbuckle.AspNetCore.Swagger.Info 导致了问题。

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

      编码愉快!

      【讨论】:

        【解决方案4】:

        我试过了,效果很好:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-30
          • 2021-05-15
          • 2020-02-06
          相关资源
          最近更新 更多