【问题标题】:Swagger not working (rather exception is thrown) after migrating project from .net core 2.2 to 3.0 preview-7将项目从 .net core 2.2 迁移到 3.0 preview-7 后,Swagger 无法正常工作(引发异常)
【发布时间】:2019-12-15 01:22:16
【问题描述】:

我刚刚将我的项目从 .net core 2.2 迁移到 3.0 预览版 7。我在其中使用 Swashbuckle.AspNetCore (v4.0.1)。这是我的创业课。

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

但是当我运行它时它会抛出这个异常:

AggregateException:无法构造某些服务(验证服务描述符时出错“ServiceType: Swashbuckle.AspNetCore.Swagger.ISwaggerProvider Lifetime: Transient ImplementationType: Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator”:无法比较中的两个元素数组。)(验证服务描述符“ServiceType: Swashbuckle.AspNetCore.SwaggerGen.ISchemaRegistryFactory Lifetime: Transient ImplementationType: Swashbuckle.AspNetCore.SwaggerGen.SchemaRegistryFactory”时出错:无法比较数组中的两个元素。)

【问题讨论】:

  • 我认为您需要 Swashbuckle 5.0.0-rc2 才能与 .NET Core 3.0 一起使用。

标签: exception swagger asp.net-core-webapi swashbuckle


【解决方案1】:

将 Swashbuckle.AspNetCore 和 Swashbuckle.AspNetCore.Filters 升级到 v 5.0.0-rc2 解决了这个问题。

【讨论】:

  • 作为跟进的第一件事,在将项目升级到新版本时,新版本可以使用哪些外部包,并且询问有关它的问题感觉很不正常。 proper research 应该包含它。
  • @LGSon - 我很感激 Muhammed Fahad Nadeem 写了这个问题,它为我节省了大量时间。对我来说,阅读这个问题就是研究:-)
  • @AndrewShepherd 你是说在你升级你的主框架版本之前,你从不检查你的组件是否可以在升级中工作?
  • @LGSon - 您正在向 Stack Overflow 上的其他人展示您的情况。就我而言,我试图学习一些新东西,并且正在使用“ASP.NET Core 2.0”书中的一个示例。当错误出现时,我不知道是我做错了什么还是其他问题。是的,我最终可以解决这个问题,但是由于这个问题,我节省了时间,并且可以专注于我的目标,即理解 swashbuckle。
【解决方案2】:

将 Swashbuckle.AspNetCore 升级到最新版本 v 5.0.0-rc4 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多