Reputation: 1630
The .Net 6 have removed the Start up Class and i am not able to find out how to configure Ocelot in new .Net 6 structure. I have found two methos
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddOcelot()// 1.ocelot.json goes where?
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot(); // 2.what is the use of this
Let me know Please
Upvotes: 6
Views: 12064
Reputation: 116
You probably already have solved this, so this is meant for all the other developers looking for a solution to this. Below are two ways of adding the Ocelot configuration.
JSON file
in your project named ocelot.json
and add your configuration for ocelot inside.ocelot.json
has to be registered in Program.cs
in order for Ocelot to load the configuration for your API Gateway.Below are two examples of how you can register your Ocelot configuration.
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("ocelot.json")
.Build();
builder.Services.AddOcelot(configuration);
var app = builder.Build();
await app.UseOcelot();
app.MapGet("/", () => "Hello World!");
app.Run();
As you can see we load the configuration from ocelot.json by using .ConfigurationBuilder()
. We then parse the configuration to the method for adding Ocelot to the service container before registering it's middleware.
I tend to have multiple environments for production, testing, local development, etc... instead of re-writing/updating the configuration loader with the specific configuration file for Ocelot, we can do it by checking what environment we are running.
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile($"ocelot.{builder.Environment.EnvironmentName}.json", true, true)
.Build();
builder.Services.AddOcelot(configuration);
var app = builder.Build();
await app.UseOcelot();
app.MapGet("/", () => "Hello World!");
app.Run();
In the code above we use IHostEnvironment
to get the current environment name. We can then use string interpolation to dynamically insert the environment name into the string of our ocelot configuration file.
For this to work, you would have to add a new configuration file for each environment like this:
ocelot.json
├─ ocelot.Development.json
├─ ocelot.Local.json
├─ ocelot.Test.json
Upvotes: 8
Reputation: 21
You need to declare direct from your program.cs you add your Ocelot json file in bulder.configuration, than in services add the Ocelot reference, and in the end start the intance app.Ocelot().wait();
Here is an example, hope it helps
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("ocelot.json");
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot();
var app = builder.Build();
//if (app.Environment.IsDevelopment())
//{
app.UseSwagger();
app.UseSwaggerUI();
//}
app.UseHttpsRedirection();
app.UseOcelot().Wait();
app.UseAuthorization();
app.MapControllers();
app.Run();
Upvotes: 1
Reputation: 9993
Add json file called ocelot.json
in your project.
Then do configure like this in Program.cs
:
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("ocelot.json")
.Build();
var builder = WebApplication.CreateBuilder(args);
//.....
builder.Services.AddOcelot(configuration);
var app = builder.Build();
//........
app.UseOcelot();
//......
Upvotes: 7