4

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

DropAndTrap
  • 1,538
  • 16
  • 24

3 Answers3

7

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();

//......
Xinran Shen
  • 8,416
  • 2
  • 3
  • 12
6

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.

  1. Add a new JSON file in your project named ocelot.json and add your configuration for ocelot inside.
  2. The file 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.

1. Adding Ocelot configuration without environment check


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.

2. Add Ocelot configuration for the current environment

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
1

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();
Leo0554
  • 21
  • 2
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney May 11 '22 at 00:27