asp.net core webapi - Ocelot API Gateway in .NET 8 not showing downstream services in Swagger UI - Stack Overflow

I'm implementing an Ocelot API Gateway in .NET 8 with Swagger integration, but my downstream servi

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744066142a4552688.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信