asp.net-coreasp.net-core-webapiocelot

Ocelot route in ASP.NET Core 6.0


I'm using ocelot to create a simple API gateway in my ASP.NET Core 6.0.

Here is my program.cs code :

using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.SetBasePath(builder.Environment.ContentRootPath)    
    .AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables();
builder.Services.AddOcelot(builder.Configuration);

var app = builder.Build();

app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
app.UseOcelot().Wait();

app.Run();

And here is my ocelot.json content :

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/getDoc/{id}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7202
        }
      ],
      "UpstreamPathTemplate": "/api/dd/getDoc/{id}",
      "UpstreamHttpMethod": [ "Get" ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:7300"
  }
}

My problem is that, api gateway works in address (and port) which defined in my vs project settings and does not works for my baseUrl which define in ocelot.json file. So, this address working correctly :

https://localhost:7006/api/dd/getDoc/{id}

And this address does not work:

https://localhost:7300/api/dd/getDoc/{id}

Where is my problem and how to solve that?

Thanks in advance


Solution

  • I've read the document:https://ocelot.readthedocs.io/en/latest/features/routing.html

    "DownstreamHostAndPorts is a collection that defines the host and port of any downstream services that you wish to forward requests to"

    It seems that your DownstreamHostAndPorts is not samewith the port your defined in project settings