Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot merge Ocelot config files

Tags:

ocelot

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.

like image 399
Ensar Ishakoglu Avatar asked Apr 12 '21 10:04

Ensar Ishakoglu


2 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)
like image 104
MatthijsFontys Avatar answered Dec 19 '22 14:12

MatthijsFontys


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


like image 37
Maicon Heck Avatar answered Dec 19 '22 15:12

Maicon Heck