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:

  1. Service 1 is running on port 5001, and it has a working Swagger at http://localhost:5001/swagger/v1/swagger.json

  2. 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:

  1. Service 1 is running on port 5001, and it has a working Swagger at http://localhost:5001/swagger/v1/swagger.json

  2. 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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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

本文标签: aspnet core webapiOcelot API Gateway in NET 8 not showing downstream services in Swagger UIStack Overflow