10

I have just migrated my project from .net core 2.2 to 3.0 preview 7. I am using Swashbuckle.AspNetCore (v4.0.1) in it. This is my startup class.

public class Startup
    {
public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<ApplicationDbContext>(options =>
                                                            options.UseSqlServer(
                                                            Configuration["ConnectionStrings:ConnectionStringAzureSQL"]));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);


            services.AddApplicationInsightsTelemetry();

            services.AddLocalization(options => options.ResourcesPath = @"Resources");

            services.AddMemoryCache();

            services.Configure<RequestLocalizationOptions>(options =>
            {
                var supportedCultures = new List<CultureInfo>
                    {
                        new CultureInfo("en-US")
                    };

                options.DefaultRequestCulture = new RequestCulture("en-US");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
            });

            services.AddSwaggerGen(c =>
            {

                c.SwaggerDoc("v1", new Info { Title = "GetAJobToday", Description = "GetAJobTodayAPIs" });

                var xmlPath = AppDomain.CurrentDomain.BaseDirectory + @"PlatformAPI.xml";
                c.IncludeXmlComments(xmlPath);

                c.AddSecurityDefinition("Bearer",
                new ApiKeyScheme
                {
                    In = "header",
                    Description = "Please enter token into the field",
                    Name = "Authorization",
                    Type = "apiKey"
                });

                c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
                {
                    {"Bearer", new string[] { }},
                });
            });

            services.AddSingleton<ITelemetryInitializer, HttpContextItemsTelemetryInitializer>();


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseRouting();

            app.UseMiddleware<ApiLoggingMiddleware>();

            app.UseHttpsRedirection();

            app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapControllerRoute("default", "{controller=Auth}/{action=RequestVerificationCode}/{id?}");
            });

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "GetAJobToday");
            });
        }
    }

But it is throwing this exception when I run it:

AggregateException: 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.)

Muhammad Fahad Nadeem
  • 1,120
  • 1
  • 9
  • 15

2 Answers2

11

Upgrading Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Filters to v 5.0.0-rc2 solved the problem.

Muhammad Fahad Nadeem
  • 1,120
  • 1
  • 9
  • 15
  • As the first thing to follow up, when upgrading a project to a new version, is which external packages is available for the newer version, and asking a question about it feels very off. A [proper research](https://stackoverflow.com/help/how-to-ask) should have included that. – Asons Sep 03 '19 at 11:10
  • @LGSon - I'm grateful that Muhammed Fahad Nadeem wrote this question, it saved me a heap of time. For me, reading this question was the research :-) – Andrew Shepherd Sep 30 '19 at 21:45
  • @AndrewShepherd Are you saying that before you upgrade your main framework version, you never check so your components will work with that upgrade? – Asons Oct 01 '19 at 05:07
  • 2
    @LGSon - You're projecting your situation to everyone else on Stack Overflow. In my case, I was trying to learn something new, and was working with an example from an "ASP.NET Core 2.0" book. When the error appeared, I didn't know if I was doing something wrong or if it was some other issue. Yes, I could have worked it out eventually, but I was saved the time thanks to this question, and could focus on my goal which was to understand swashbuckle. – Andrew Shepherd Oct 01 '19 at 05:52
1

Upgrading Swashbuckle.AspNetCore to the latest version v 5.0.0-rc4 solved the problem.

enter image description here

DeC
  • 2,226
  • 22
  • 42
Bibin Gangadharan
  • 1,393
  • 12
  • 13