8

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 Answers3

7

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)
  • 1
    This was indeed the answer, thank you very much :) – Ensar Ishakoglu Apr 12 '21 at 12:07
  • How can I get json files from diffrent folders?like this scrutcture XFolder->ocelot.x.json ,YFolder-> ocelot.y.json – captainblack Aug 26 '21 at 12:42
  • Hey @captainblack, as per the documentation of Ocelot, it should just work fine as long as the files are prefixed with "ocelot." and end with ".json". So your files should be used during configuration, if it doesn't work maybe you should create another Stackoverflow post where people can help you on your specific problem – Ensar Ishakoglu Sep 07 '21 at 08:45
3

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


Maicon Heck
  • 2,017
  • 1
  • 20
  • 27
0

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69