-
Hi, I want to return the responding server in a load balanced configuration back to the caller to see from which destination the response was generated. This is only used within a dev environment. I configured a single route with a single cluster and wrote this code, which works for this simple setup: services.AddReverseProxy()
.LoadFromConfig(this.configuration.GetSection("ReverseProxy"))
.AddTransforms(context =>
{
context.AddResponseTransform(ctx =>
{
/* Compares ctx.ProxyResponse.RequestMessage.RequestUri with the Cluster destination adresses to find the
responding one and return its key */
var responseServer = DetectResponseServer(ctx, context.Cluster);
if (!string.IsNullOrEmpty(responseServer))
{
ctx.HttpContext.Response.Headers.Add("yarp-response-server", responseServer);
}
return default;
});
}); The problem with this code is, that the context variable from Is there a way of doing something like this? Looking at the load balancer middleware, the decision is not stored and the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The YARP pipeline will add an var responseServer = ctx.HttpContext.Features.Get<IReverseProxyFeature>()?.ProxiedDestination;
if (responseServer is not null)
{
ctx.HttpContext.Response.Headers.Add("yarp-response-server", responseServer.DestinationId);
} |
Beta Was this translation helpful? Give feedback.
The YARP pipeline will add an
IReverseProxyFeature
to theHttpContext
. The feature also stores the destination that was chosen for proxying by health checks and load balancing.