Ocelot + dotnet 8 + hc + serilog

Matheus Xavier
C# Programming
Published in
2 min readJan 3, 2024

--

Ocelot has complete documentation including a getting started example, but when migrating my project to dotnet 8 I came across some problems:

1. The documentation example still:
— Use namespace
— Declare the Program class
— Implement the Main method
2. We may have difficulty implementing Serilog following the documentation
3. The example was not created using `WebApplication.CreateBuilder`

So, I decided to bring a small example addressing the points I mentioned above and giving a complete example with:

Ocelot + dotnet 8 + Serilog

I have already written a series of two articles demonstrating how we can build an API Gateway using Ocelot, because of this I will not go into details of how we should configure it, the idea is simply to show what our Program.cs class will look like.

Program class

Without any kind of mystery, this is our Program class:

PS1: This is just an example, so in your application you must add the Authentication configuration for your scenario, the same goes for CORS, Health Checks and Serilog, but I believe that with this example this will make things easier.

PS2: By default in dotnet 8 it is not necessary to add the following points:

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(_ => { });

This is because these middlewares are already added automatically when we configure authentication and our API endpoints, but when we call

await app.UseOcelot();

it adds a final middleware to our application, meaning that these middlewares are not added, which is why it was necessary to add them.

You can check the entire implementation on github.

Conclusion

This is an extremely simple article, I went through the process of migrating some projects to dotnet 8 and ended up bumping into some points in the Ocelot configuration and because of that I decided to bring an example more complete than the documentation to try to help other people in the same situation.

Thank you for reading and feel free to contact me if you have any questions.

--

--