Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to 3.1 produces empty ocelot.json #1247

Closed
mkanakis opened this issue May 27, 2020 · 6 comments · Fixed by #1569
Closed

Upgrade to 3.1 produces empty ocelot.json #1247

mkanakis opened this issue May 27, 2020 · 6 comments · Fixed by #1569
Assignees
Labels
bug Identified as a potential bug merged Issue has been merged to dev and is waiting for the next release proposal Proposal for a new functionality in Ocelot

Comments

@mkanakis
Copy link

Expected Behavior

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.

Actual Behavior

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}}

Specifications

  • Version: Ocelot 16.0.1
  • Platform: .NET Core 3.1
@mkanakis
Copy link
Author

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

@Mato-01
Copy link

Mato-01 commented May 28, 2020

Thank you for sharing, i spent a lot of time resolving this bug

@amweiss
Copy link

amweiss commented May 29, 2020

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.

@ediblecode
Copy link

ediblecode commented Jun 15, 2021

It looks like this is caused by this method:

public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, string folder, IWebHostEnvironment env)

And specifically by this line:

File.WriteAllText(primaryConfigFile, json);

I followed the config from this answer for .NET Core 3.1. But did the following instead:

-config.AddOcelot("your folder", hostingContext.HostingEnvironment);
+config.AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true);

So avoiding the call to AddOcelot avoids the ocelot.json file being overwritten.

But according to the docs:

The way Ocelot merges the files is basically load them, loop over them, add any Routes, add any AggregateRoutes and if the file is called ocelot.global.json add the GlobalConfiguration aswell as any Routes or AggregateRoutes. Ocelot will then save the merged configuration to a file called ocelot.json and this will be used as the source of truth while ocelot is running.

So it looks like it's by design and not a bug!

@raman-m raman-m added bug Identified as a potential bug proposal Proposal for a new functionality in Ocelot labels Sep 21, 2023
@raman-m
Copy link
Member

raman-m commented Sep 21, 2023

@mkanakis Hi Marios!
Thanks for your interest in Ocelot!

I see you fought with Ocelot config files for different environments.
I believe linked PR 1569 will help you much.

raman-m added a commit that referenced this issue Sep 23, 2023
…ration construction (#1569)

* Added AddOcelot Configbuilder Overload

Added an AddOcelot overload to load FileConfiguration directly from the application, so that all the routes could be made configurable and could be load from anywhere.

* Code review changes

* Added unit tests for ConfigurationBuilder AddOcelot method overload

* code review

* CA1816 Change ConfigurationReloadTests.Dispose() to call GC.SuppressFinalize(object). This will prevent derived types that introduce a finalizer from needing to re-implement 'IDisposable' to call it.

* SYSLIB1045 Use 'GeneratedRegexAttribute' to generate the regular expression implementation at compile-time.

* SA1601 Partial elements should be documented

* Add developer's XML-docs

---------

Co-authored-by: raman-m <[email protected]>
@raman-m raman-m added the merged Issue has been merged to dev and is waiting for the next release label Sep 23, 2023
@raman-m
Copy link
Member

raman-m commented Sep 23, 2023

@mkanakis

In .NET Core 2.2, inside the Startup constructor we have the following configuration:

Specifications

  • Version: Ocelot 16.0.1
  • Platform: .NET Core 3.1

We don't support .NET Core 3.x since 16.0.2 release.
Current version is .NET 7 with Ocelot 19.0.x releases.

If the issue still persists, please open new issue/PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Identified as a potential bug merged Issue has been merged to dev and is waiting for the next release proposal Proposal for a new functionality in Ocelot
Projects
None yet
5 participants