Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i configure ocelot programmatically? #2214

Open
cizu64 opened this issue Nov 22, 2024 · 0 comments
Open

How can i configure ocelot programmatically? #2214

cizu64 opened this issue Nov 22, 2024 · 0 comments

Comments

@cizu64
Copy link

cizu64 commented Nov 22, 2024

I get this error message when trying to configure ocelot programmatically

Ocelot.Responder.Middleware.ResponderMiddleware[0]
      requestId: 0HN8B1C7OOUES:00000001, previousRequestId: No PreviousRequestId, message: 'Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /banks, verb: GET. errors found in ResponderMiddleware. Setting error response for request path:/banks, request method: GET'

This is my code

using 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 = "google.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.cs

builder.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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant