web-dev-qa-db-fra.com

Swagger ne fonctionne pas (plutôt une exception est levée) après la migration du projet de .net core 2.2 vers 3.0 preview-7

Je viens de migrer mon projet de .net core 2.2 vers l'aperçu 3.0 7. J'utilise Swashbuckle.AspNetCore (v4.0.1) dedans. Ceci est ma classe de démarrage.

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

Mais il lève cette exception lorsque je l'exécute:

AggregateException: certains services ne peuvent pas être construits (Erreur lors de la validation du descripteur de service 'ServiceType: Swashbuckle.AspNetCore.Swagger.ISwaggerProvider Durée de vie: Transient ImplementationType: Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator': Impossible de comparer deux éléments dans le tableau. ) (Erreur lors de la validation du descripteur de service "ServiceType: Swashbuckle.AspNetCore.SwaggerGen.ISchemaRegistryFactory Lifetime: Transient ImplementationType: Swashbuckle.AspNetCore.SwaggerGen.SchemaRegistryFactory": impossible de comparer deux éléments dans le tableau.)

10

La mise à niveau de Swashbuckle.AspNetCore et Swashbuckle.AspNetCore.Filters vers la version 5.0.0-rc2 a résolu le problème.

11

La mise à niveau de Swashbuckle.AspNetCore vers la dernière version v 5.0.0-rc4 a résolu le problème.

enter image description here

1
Bibin Gangadharan