Exception when adding swagger with .NET Core 3.0
Asked Answered
B

6

14

I am trying to integrate swagger into a ASP NET Core 3.0 project and it throws exception right in the ConfigureServices method:

I am using Swashbuckle.AspNetCore 4.0.1.

I have also checked this issue and also this

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",
                });

            });
        }

Exception

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 I have followed this guide and the only difference is that while he uses a OpenApiInfo object i do not have that overload available and thus am using a Info.

Briefs answered 5/11, 2019 at 10:18 Comment(0)
C
7

I just went through Get started with Swashbuckle and ASP.NET Core at learn.microsoft.com and had no problem.

The docs explain you need to use the latest preview version of Swashbuckle.AspNetCore, 5.0.0-rc4

Clute answered 5/11, 2019 at 11:57 Comment(0)
B
62

I experienced this issue, using .Net Core 3.1.2 and Swagger 5.4.1.

Turns out I forgot to add services.AddControllers(); to my Startup::ConfigureServices method.

I was only adding Razor Pages.

Bubble answered 8/5, 2020 at 1:20 Comment(3)
This was also true for me in .Net Core 5.0.0-preview.4Housecoat
In Core 3.1 and Swagger 5.5.1 I had to add services.AddControllers(); and also endpoints.MapControllers();.Weir
The ASP.NET Core documentation seems to indicate that AddControllers(); and maybe AddEndpointsApiExplorer(); should be enough: learn.microsoft.com/en-us/aspnet/core/tutorials/…Juliettejulina
C
7

I just went through Get started with Swashbuckle and ASP.NET Core at learn.microsoft.com and had no problem.

The docs explain you need to use the latest preview version of Swashbuckle.AspNetCore, 5.0.0-rc4

Clute answered 5/11, 2019 at 11:57 Comment(0)
L
4

I had this same error with Minimamal API.

I added builder.Services.AddEndpointsApiExplorer(); to Program.cs and it was fine.

this was in dotnet 7

Lantern answered 13/5, 2023 at 20:27 Comment(2)
also worked in dotnet 6Conlen
This worked for me as well, Thank you.Gunsel
A
1

This is usually due to usage of wrong swagger package and as a result the Info api is using Swashbuckle.AspNetCore.Swagger.Info which caused the problem.

If you use the latest Swashbuckle.AspNetCore, 5.0.0-rc4m package then it will take OpenApiInfo from new Microsoft.OpenApi.Models and then it will work.

Happy Coding!

Applause answered 4/12, 2019 at 9:58 Comment(0)
T
1

The logs wouldn't tell me the answer I had to open up the swagger generated JSON File

http://localhost:{PORT}/swagger/v1/swagger.json
Teratoid answered 24/3, 2023 at 21:54 Comment(0)
Z
0

I tried this and it worked fine:

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

Btw I'm using .Net Core 2.1

Zirconium answered 10/4, 2020 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.