-
I am in the middle of process to rewrite endpoints from .NET Framework -> .NET6, we are using YARP to proxy request to old API. Here is configuration of our Map Forwarder: app.MapForwarder("/{**catch-all}", app.Configuration["ProxyTo"]!).Add(static builder => ((RouteEndpointBuilder)builder).Order = int.MaxValue); Traffic is proxed through new API to old API, but if there is new endpoint (.NET6) it stays in new API. Now we want to create configuration to proxy specific endpoints to old API even if the new one exist. Just in case if the new endpoint has not been tested complete or cointins some bugs. How to do it? |
Beta Was this translation helpful? Give feedback.
Answered by
Tratcher
Sep 5, 2023
Replies: 1 comment
-
You can switch to a middleware approach instead: using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Yarp.ReverseProxy.Forwarder;
namespace CustomForwarding
{
public class CustomForwardingMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpForwarder _forwarder;
private readonly HttpMessageInvoker _client;
private readonly string _destination = "ULR Prefix To Forward To";
public CustomForwardingMiddleware(RequestDelegate next, IHttpForwarder forwarder)
{
_next = next;
_forwarder = forwarder;
// https://github.com/microsoft/reverse-proxy/blob/47f1e7c21d6a3a5771d61770100bd3a6f07e8568/docs/docfx/articles/direct-forwarding.md#the-http-client
_client = null; // TODO: Set up or inject client
}
public Task InvokeAsync(HttpContext context)
{
if (ShouldForwardRequest(context))
{
return _forwarder.SendAsync(context, _destination, _client);
}
// Call the next middleware in the pipeline instead.
return _next(context);
}
private bool ShouldForwardRequest(HttpContext context)
{
// https://github.com/microsoft/reverse-proxy/blob/47f1e7c21d6a3a5771d61770100bd3a6f07e8568/docs/docfx/articles/middleware.md?plain=1#L32
var endpoint = context.GetEndpoint();
if (endpoint == null) return true; // Forward anything that didn't match an endpoint.
// TODO: Which endpoints should be overriden?
return false;
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
karelz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can switch to a middleware approach instead: