admin 管理员组文章数量: 1086019
I'm implementing an Ocelot API Gateway in .NET 8 with Swagger integration, but my downstream services aren't appearing in the Swagger UI. Here's my setup:
Service 1 is running on port 5001, and it has a working Swagger at
http://localhost:5001/swagger/v1/swagger.json
Gateway (port 5004) is configured with correct SwaggerKey matching between routes and endpoints. I've included my
ocelot.json
file below
While the gateway's Swagger UI loads, it only shows the gateway's own endpoints. The service isn't listed, despite the downstream Swagger JSON being directly accessible
Environment:.NET 8, Ocelot 23.4.3, MMLib.SwaggerForOcelot: 8.3.2
ocelot.json
in gateway
{
"Routes": [
{
"UpstreamPathTemplate": "/api/Service1/{everything}",
"UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"DownstreamPathTemplate": "/api/Service1/{everything}",
"SwaggerKey": "Service1"
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5004"
},
"SwaggerEndPoints": [
{
"Key": "Service1",
"Config": [
{
"Name": "IService1API",
"Version": "v1",
"Url": "http://localhost:5001/swagger/v1/swagger.json"
}
]
}
]
}
Program.cs
in gateway:
using Microsoft.OpenApi.Models;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddSwaggerForOcelot(builder.Configuration);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerForOcelotUI(options =>
{
options.PathToSwaggerGenerator = "/swagger/docs";
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
await app.UseOcelot();
app.Run();
I'm implementing an Ocelot API Gateway in .NET 8 with Swagger integration, but my downstream services aren't appearing in the Swagger UI. Here's my setup:
Service 1 is running on port 5001, and it has a working Swagger at
http://localhost:5001/swagger/v1/swagger.json
Gateway (port 5004) is configured with correct SwaggerKey matching between routes and endpoints. I've included my
ocelot.json
file below
While the gateway's Swagger UI loads, it only shows the gateway's own endpoints. The service isn't listed, despite the downstream Swagger JSON being directly accessible
Environment:.NET 8, Ocelot 23.4.3, MMLib.SwaggerForOcelot: 8.3.2
ocelot.json
in gateway
{
"Routes": [
{
"UpstreamPathTemplate": "/api/Service1/{everything}",
"UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}
],
"DownstreamPathTemplate": "/api/Service1/{everything}",
"SwaggerKey": "Service1"
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5004"
},
"SwaggerEndPoints": [
{
"Key": "Service1",
"Config": [
{
"Name": "IService1API",
"Version": "v1",
"Url": "http://localhost:5001/swagger/v1/swagger.json"
}
]
}
]
}
Program.cs
in gateway:
using Microsoft.OpenApi.Models;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddSwaggerForOcelot(builder.Configuration);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwaggerForOcelotUI(options =>
{
options.PathToSwaggerGenerator = "/swagger/docs";
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
await app.UseOcelot();
app.Run();
Share
Improve this question
edited Mar 28 at 6:50
Jason Pan
22.4k2 gold badges22 silver badges45 bronze badges
asked Mar 27 at 21:57
Amisha SubediAmisha Subedi
12 bronze badges
1 Answer
Reset to default 0You need to comment this line.
app.UseSwaggerUI();
Your configuration should be like below.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
//app.UseSwaggerUI();
app.UseSwaggerForOcelotUI(options =>
{
options.PathToSwaggerGenerator = "/swagger/docs";
});
}
Test Result
版权声明:本文标题:asp.net core webapi - Ocelot API Gateway in .NET 8 not showing downstream services in Swagger UI - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744066144a2527632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论