In .NET Core 2.2, inside the _Startup_ constructor we have the following configuration:
var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath);
if (env.IsLocal())
{
builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
} else
{
builder.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
}
builder.AddEnvironmentVariables();
// Loads corresponding folder -> e.g. Products/Local/*.json
var gateway = System.Environment.GetEnvironmentVariable("ASPNETCORE_GATEWAY");
builder.AddOcelot($"/{gateway}/{env.EnvironmentName}/", env);
Configuration = builder.Build();
Environment = env;
In ConfigureServices we use the following line to add Ocelot as a middleware:
services.AddOcelot(Configuration);
As you can see, we specify a certain path for merging ocelot files for different environments.
After upgrading to .NET Core 3.1 the only difference is in _Configure_ method:
app.UseRouting();
app.UseCors("CorsPolicy"); // to remove
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseOcelot().Wait();
The above snippets should merge all ocelot.*.json files into ocelot.json and _work_ as intended. At least with 2.2 that was the case.
In 3.1, the generated ocelot.json is empty! So every request leads to => 404
One thing I noticed is in this line:
builder.AddOcelot(string, env);
It used to get the ContentRootPath and add the rest {gateway}/{env.name}.
Instead, it goes to "C:/" now. I fixed this by adding the ContentRootPath in front, but still the generated routing config is empty.
Produced ocelot.json:
{"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":"http://localhost:56512","LoadBalancerOptions":{"Type":null,"Key":null,"Expiry":0},"DownstreamScheme":null,"HttpHandlerOptions":{"AllowAutoRedirect":false,"UseCookieContainer":false,"UseTracing":false,"UseProxy":true,"MaxConnectionsPerServer":2147483647},"DownstreamHttpVersion":null}}
Update:
If I downgrade Ocelot to latest 15 version (v15.0.7), the configuration (ocelot.json) is back again, with _.NET Core 3.1_.
So narrowing down the problem, the empty configuration only occurs in: _v16.0.0 & v16.0.1_
Thank you for sharing, i spent a lot of time resolving this bug
I've also found the 16 versions have an issue where it seems to break JWT authentication. Running 15.0.7 everything was fine. If I upgrade to 16.0.x and rename "ReRoutes" to "Routes" in the appsettings.json, the basic routes seem ok but when I inspect the Identity in the context it's not translating claims correctly. I don't have a simple case to post yet, but it seems like there's a step missing for the config upgrade or something is very broken.
Most helpful comment
Update:
If I downgrade Ocelot to latest 15 version (v15.0.7), the configuration (ocelot.json) is back again, with _.NET Core 3.1_.
So narrowing down the problem, the empty configuration only occurs in: _v16.0.0 & v16.0.1_