0

当我们将 NSwag 与 ASP.Net Core Service Fabric 服务的默认设置一起使用时,该服务更改了HttpSys侦听器 url(添加了路径后缀),生成的 swagger.json 和 UI 找不到/无法访问。

设置 HttpSys Listener 的 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;
        });
    }
4

1 回答 1

0

我们通过以下方式在 NSwag 设置中提供 swagger Route 和 Swagger UI Port 解决了这个问题:

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 路径更改。

于 2018-11-07T11:06:28.387 回答