Search code examples
apihostgatewayocelot

API Gateway using OCELOT -- ERROR - Unable to start Ocelot


I'm new to API gateway and follow the below link to start with.

https://www.c-sharpcorner.com/article/building-api-gateway-using-ocelot-in-asp-net-core/

When I try to run the application it throws an error in Startup.cs file in the following line of code.

Startup.cs

public Startup(IHostingEnvironment env){
    var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
    builder.SetBasePath(env.ContentRootPath)
    .AddJsonFile("configuration.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables();
    Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

public void ConfigureServices(IServiceCollection services){
    Action<ConfigurationBuilderCachePart> settings = (x) =>{
        x.WithMicrosoftLogging(log =>{
            log.AddConsole(LogLevel.Debug);
        }).WithDictionaryHandle();
    };
    //services.AddOcelot(Configuration, settings);
    services.AddOcelot(Configuration);
}

public async void Configure(IApplicationBuilder app, IHostingEnvironment env){
    await app.UseOcelot();  // Error in this line number
}
}

Error:

Unable to start Ocelot, errors are: When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!,When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!,When not using service discovery DownstreamHostAndPorts must be set and not empty or Ocelot cannot find your service!

configuration.json

{</br>
    "ReRoutes": [</br>
        {</br>
            "DownstreamPathTemplate": "/api/customers",
            "DownstreamScheme": "http",
            "DownstreamHost": "localhost",
            "DownstreamPort": 9001,
            "UpstreamPathTemplate": "/customers",
            "UpstreamHttpMethod": [ "Get" ]</br>
        },</br>
        {</br>
            "DownstreamPathTemplate": "/api/customers/{id}",
            "DownstreamScheme": "http",
            "DownstreamHost": "localhost",
            "DownstreamPort": 9001,
            "UpstreamPathTemplate": "/customers/{id}",
            "UpstreamHttpMethod": [ "Get" ]</br>
        },</br>
        {</br>
            "DownstreamPathTemplate": "/api/products",
            "DownstreamScheme": "http",
            "DownstreamPort": 9002,
            "DownstreamHost": "localhost",
            "UpstreamPathTemplate": "/api/products",
            "UpstreamHttpMethod": [ "Get" ]</br>
        }</br>
    ],</br>
    "GlobalConfiguration": {
        "RequestIdKey": "OcRequestId",
        "AdministrationPath": "/administration"
    }</br>
}</br></br>

Solution

  • I have used Ocelot 8.0.1 and there was a bug on that version. Upgraded to 8.0.2, now its working fine.