[英]How to set BaseUrl for Swagger in ASP.NET Core Web API using NSwag.AspNetCore (when hosted behind a reverse proxy)
[英]Swagger generated using NSwag not found for ASP.Net Core Service Fabric service when using httpsys with altered listener path
当我们将NSwag与ASP.Net Core Service Fabric服务的默认设置一起使用时,该服务更改了HttpSys侦听器url(添加了路径后缀),生成的swagger.json和UI找不到/无法访问。
设置HttpSys侦听器的url路径:
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new HttpSysCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
url += "/service1";
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting HttpSys on {url}");
return new WebHostBuilder()
.UseHttpSys()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))
};
}
并为ASP.Net Core服务设置应用程序NSwag:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Register the Swagger services
services.AddSwagger();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
// Register the Swagger generator and the Swagger UI middlewares
app.UseSwaggerUi3WithApiExplorer(settings =>
{
settings.GeneratorSettings.DefaultPropertyNameHandling =
PropertyNameHandling.CamelCase;
});
}
我们通过以下方式在NSwag设置中提供了醒目的Route和Swagger UI端口来解决此问题:
app.UseSwaggerUi3WithApiExplorer(settings =>
{
settings.GeneratorSettings.DefaultPropertyNameHandling =
PropertyNameHandling.CamelCase;
settings.SwaggerUiRoute = "/swagger";
settings.SwaggerRoute = "/api-specification.json";
});
Swagger UI在baseurl / service1 / swagger上可用,而swagger.json文件在baseurl / service1 / api-specification.json上可用
看来,如果我们不更改swaggerroute设置,则NSwag使用一些默认值,而忽略了为HttpSys侦听器所做的url路径更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.