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