Cannot merge Ocelot config files

Viewed 809

As per the documentation i tried to merge my config files so they are a bit more readable. The generated ocelot.json file however is not like expected. My folder structure is like follows:

Folder structure

Below is a text representation of this:

.
└── Ocelot route configs
    ├── ocelot.pokemon.json
    ├── ocelot.tweet.json
    └── ocelot.weather.json

The ocelot.pokemon.json file looks like following (the others are similar to this):

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/v2/pokemon",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "pokeapi.co",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/api/pokemon",
      "UpstreamHttpMethod": [ "GET" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "MyTestKey",
        "AllowedScopes": []
      }
    },
    {
      "DownstreamPathTemplate": "/api/v2/pokemon/ditto",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "pokeapi.co",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/api/pokemon/ditto",
      "UpstreamHttpMethod": [ "GET" ]
    }
  ]
}

The generated ocelot.json file looks like this:

{
  "Routes": [
  ],
  "DynamicRoutes": [
  ],
  "Aggregates": [
  ],
  "GlobalConfiguration": {
    "RequestIdKey": null,
    "ServiceDiscoveryProvider": {
      "Scheme": null,
      "Host": null,
      "Port": 0,
      "Type": null,
      "Token": null,
      "ConfigurationKey": null,
      "PollingInterval": 0,
      "Namespace": null
    },
    "RateLimitOptions": {
      "ClientIdHeader": "ClientId",
      "QuotaExceededMessage": null,
      "RateLimitCounterPrefix": "ocelot",
      "DisableRateLimitHeaders": false,
      "HttpStatusCode": 429
    },
    "QoSOptions": {
      "ExceptionsAllowedBeforeBreaking": 0,
      "DurationOfBreak": 0,
      "TimeoutValue": 0
    },
    "BaseUrl": null,
    "LoadBalancerOptions": {
      "Type": null,
      "Key": null,
      "Expiry": 0
    },
    "DownstreamScheme": null,
    "HttpHandlerOptions": {
      "AllowAutoRedirect": false,
      "UseCookieContainer": false,
      "UseTracing": false,
      "UseProxy": true,
      "MaxConnectionsPerServer": 2147483647
    },
    "DownstreamHttpVersion": null
  }
}

As you can see, the routes I defined were not added. I tried looking on the internet for this specific issue but couldn't find anything. I don't know what I'm doing wrong, help will be appreciated.

3 Answers

Since your different route configuration files are located in a folder you should make sure the correct overload of the AddOcelot method is called. In this case the method should be called with the folder name containing the route files.

For example:

config.AddOcelot("Ocelot route configs", hostingContext.HostingEnvironment)

UPDATE: .NET Core 3+ with Ocelot 17.0.0

As the method AddOcelot needs an IWebHostEnvironment, and this is not available in HostBuilderContext:

enter image description here

You need to get it via WebHostBuilderContext:

enter image description here


I created two different directories Development and Production and with the below code, I'm able to read ocelot configuration according to development environment to generate the final ocelot.json that will be use by ocelot middleware.

enter image description here

Related