Can we dynamically turn yarp on and off?

52 Views Asked by At

I am in the process of updating our old ASP.NET code to .NET Core using the strangling pattern and YARP. So we have our new .NET Core application with YARP in front, and our old application in the back. The idea as I understand it is, as we implement endpoints in the new application, requests won't reach the old application.

The issue now is we want to test out the new application on only certain test users (based on a flag stored in a database). So for these test users, they get to see the new .NET Core application, along with the old application for non-upgraded routes. And for other users, they just always get forwarded to the old application.

I've tried the following setup in program.cs hoping that hasClaim will handle it, but it seems to be all or nothing. All requests are forwarded to the old application or not. It no longer seems to be able to check if a route is exists in .NET Core before falling back to the old application.

var transformer = HttpTransformer.Default; // or HttpTransformer.Default;
var requestConfig = new ForwarderRequestConfig { ActivityTimeout = TimeSpan.FromSeconds(100) };

// When using IHttpForwarder for direct forwarding you are responsible for routing, destination     discovery, load balancing, affinity, etc..
// For an alternate example that includes those features see BasicYarpSample.
var httpClient = new HttpMessageInvoker(new SocketsHttpHandler()
{
    UseProxy = false,
    AllowAutoRedirect = false,
    AutomaticDecompression = DecompressionMethods.None,
    UseCookies = false,
    ActivityHeadersPropagator = new ReverseProxyPropagator(DistributedContextPropagator.Current),
    ConnectTimeout = TimeSpan.FromSeconds(15),
}); 

app.Map("/{**catch-all}", async (HttpContext httpContext, IHttpForwarder forwarder) =>
{
    if (httpContext.User.HasClaim("testUser", "true"))
    {
        var error = await forwarder.SendAsync(httpContext, builder.Configuration["ProxyTo"], httpClient, requestConfig, transformer);

        if (error != ForwarderError.None)
        {
            var errorFeature = httpContext.GetForwarderErrorFeature();
            var exception = errorFeature.Exception;
        }
    }
});
0

There are 0 best solutions below