-
I get this error message when trying to configure ocelot programmatically
This is my codeusing System.Security.Claims;
using Microsoft.AspNetCore.Mvc.Filters;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Repository;
public class Middleware : IMiddleware
{
private readonly IFileConfigurationRepository _fileConfigurationRepository;
public Middleware(IFileConfigurationRepository fileConfigurationRepository)
{
_fileConfigurationRepository = fileConfigurationRepository;
}
private List<FileRoute> Configuration(string downStreamPathTemplate)
{
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var fileConfig = new FileConfiguration
{
Routes = new List<FileRoute>
{
new FileRoute
{
DownstreamPathTemplate = "/api/bank/banks",
DownstreamScheme = "https",
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort { Host = "https://gateway.mybankapp123.com", Port = 443 }
},
DownstreamHttpMethod = "GET",
UpstreamPathTemplate = "/banks",
UpstreamHttpMethod = new List<string> { "GET" }
}
},
GlobalConfiguration = new FileGlobalConfiguration
{
BaseUrl = "https://localhost:7174"
}
};
await _fileConfigurationRepository.Set(fileConfig);
await next.Invoke(context);
}
} This is my program.csbuilder.Services.AddOcelot();
builder.Services.AddScoped<Middleware>();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
//await app.UseOcelot().ContinueWith(a => app.UseMiddleware<Middleware>());
await app.UseOcelot();
app.UseMiddleware<Middleware>();
app.Run(); |
Beta Was this translation helpful? Give feedback.
Answered by
raman-m
Nov 25, 2024
Replies: 2 comments 7 replies
-
😄 🐯Yes, you can! And everybody can! Yahoo! |
Beta Was this translation helpful? Give feedback.
3 replies
-
OK Let's start professional discussion...
The proposed middleware solution is based on the This service within the DI-container is solely responsible for file operations. This is the correct direction; however, the configuration logic in Ocelot is rather complex. First, you must conduct some research on the Ocelot code by examining all references to IFileConfigurationRepository! The appropriate solution should be grounded in the functionality of FileConfigurationController :Ocelot/src/Ocelot/Configuration/FileConfigurationController.cs Lines 38 to 55 in a79ae8a To change the configuration dynamically after the Ocelot application starts, it is necessary to utilize both the IFileConfigurationRepository and IFileConfigurationSetter services.
|
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The second,
Ocelot has no any fluent syntax to build configuration on the fly as it allowed by YARP for example.
But injecting
FileConfiguration
object is possible during app start in the ConfigurationBuilderExtensions class methods:Ocelot/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs
Lines 165 to 170 in a79ae8a