.NET Core 3.0中添加大张旗鼓的异常

我正在尝试将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'.

PS 我遵循了这个guide,唯一的区别是,当他使用OpenApiInfo对象时,我没有可用的重载,因此正在使用{{ 1}}。

shenghuat 回答:.NET Core 3.0中添加大张旗鼓的异常

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

结果是我忘记将services.AddControllers();添加到我的Startup::ConfigureServices方法中。

我只添加了剃刀页面。

,

我刚刚在docs.microsoft.com上经历了Get started with Swashbuckle and ASP.NET Core,并且没有问题。

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

,

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

如果使用最新的Swashbuckle.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

本文链接:https://www.f2er.com/3160502.html

大家都在问