gpt4 book ai didi

exception - 将项目从.net Core 2.2迁移到3.0 Preview-7后,Swagger无法正常工作(引发异常)

转载 作者:行者123 更新时间:2023-12-03 15:39:17 25 4
gpt4 key购买 nike

我刚刚将项目从.net core 2.2迁移到3.0 Preview7。我在其中使用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时发生错误:临时实现类型:Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator”:无法比较数组中的两个元素。 )(验证服务描述符'ServiceType:Swashbuckle.AspNetCore.SwaggerGen.ISchemaRegistryFactory时发生的错误:临时实现类型:Swashbuckle.AspNetCore.SwaggerGen.SchemaRegistryFactory':无法比较数组中的两个元素。)

最佳答案

将Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Filters升级到v 5.0.0-rc2可解决此问题。

关于exception - 将项目从.net Core 2.2迁移到3.0 Preview-7后,Swagger无法正常工作(引发异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57393829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com