I have a .NET project with ocelot:
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
});
builder.Services.AddControllers();
var app = builder.Build();
await app.UseOcelot();
app.Run();
and i want to use ocelot as an entry point for all microservices; I want to split routes from different microservices into different files like:
folder ocelot-config
with:
-a---- 3/26/2025 2:36 PM 336 doctors.json
-a---- 3/26/2025 2:35 PM 672 reviews.json
and i also have in the src foldere:
ocelot.json
The problem is when I try to add all files like:
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
.AddJsonFile("./ocelot-config/reviews.json", optional: false, reloadOnChange: true)
.AddJsonFile("./ocelot-config/doctors.json", optional: false, reloadOnChange: true);
only the last one works. The last configuration overwrites previous ones.
How to separate different config files for different Microservices with Routes and then register them all?
I have a .NET project with ocelot:
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
});
builder.Services.AddControllers();
var app = builder.Build();
await app.UseOcelot();
app.Run();
and i want to use ocelot as an entry point for all microservices; I want to split routes from different microservices into different files like:
folder ocelot-config
with:
-a---- 3/26/2025 2:36 PM 336 doctors.json
-a---- 3/26/2025 2:35 PM 672 reviews.json
and i also have in the src foldere:
ocelot.json
The problem is when I try to add all files like:
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
.AddJsonFile("./ocelot-config/reviews.json", optional: false, reloadOnChange: true)
.AddJsonFile("./ocelot-config/doctors.json", optional: false, reloadOnChange: true);
only the last one works. The last configuration overwrites previous ones.
How to separate different config files for different Microservices with Routes and then register them all?
Share Improve this question edited Mar 27 at 9:27 Ruikai Feng 12.3k1 gold badge7 silver badges16 bronze badges asked Mar 26 at 13:53 user29547398user29547398 51 bronze badge1 Answer
Reset to default 0The value in IConfigration share the same key would be overrided,you may see this document
A workaround for you is write the array in formart of object and append the index yourself
For example:
user.json:
{
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7174"
},
"Routes:0":
{
"UpstreamPathTemplate": "/gateway/user",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamPathTemplate": "/api/user",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7019
}
]
}
}
product.json:
{
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7174"
},
"Routes:1":
{
"UpstreamPathTemplate": "/gateway/product",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamPathTemplate": "/api/product",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7142
}
]
}
}
It works on myside: