dotnet/yarp · error · InvalidOperationException

The IReverseProxyFeature Destinations collection was not set

Error message

The IReverseProxyFeature Destinations collection was not set.

What it means

Thrown by HttpSysDelegatorMiddleware.Invoke when IReverseProxyFeature.AvailableDestinations is null. The middleware expects an earlier pipeline stage (load balancing / proxy feature population) to have set the available destinations collection. A null value indicates the proxy feature was not initialized correctly for this request.

Source

Thrown at src/ReverseProxy/Delegation/HttpSysDelegatorMiddleware.cs:43

        IRandomFactory randomFactory)
    {
        ArgumentNullException.ThrowIfNull(next);
        ArgumentNullException.ThrowIfNull(logger);
        ArgumentNullException.ThrowIfNull(delegator);
        ArgumentNullException.ThrowIfNull(randomFactory);
        _next = next;
        _logger = logger;
        _delegator = delegator;
        _randomFactory = randomFactory;
    }

    public Task Invoke(HttpContext context)
    {
        ArgumentNullException.ThrowIfNull(context);

        var reverseProxyFeature = context.GetReverseProxyFeature();
        var destinations = reverseProxyFeature.AvailableDestinations
            ?? throw new InvalidOperationException($"The {nameof(IReverseProxyFeature)} Destinations collection was not set.");
        var cluster = reverseProxyFeature.Cluster
            ?? throw new InvalidOperationException($"The {nameof(IReverseProxyFeature)} Cluster was not set.");

        if (destinations.Any())
        {
            // This logic mimics behavior in ForwarderMiddleware, except we save the chosen destination back
            // to the proxy feature to ensure a delegation destination doesn't slip past this middleware.
            var destination = destinations[0];
            if (destinations.Count > 1)
            {
                var random = _randomFactory.CreateRandomInstance();
                Log.MultipleDestinationsAvailable(_logger, reverseProxyFeature.Cluster.Config.ClusterId);
                destination = destinations[random.Next(destinations.Count)];
                reverseProxyFeature.AvailableDestinations = destination;
            }

            if (destination.ShouldUseHttpSysDelegation())
            {

View on GitHub (pinned to bd11867bee)

Solutions

  1. Ensure UseHttpSysDelegation is placed after load balancing and the proxy feature-populating middleware.
  2. Do not mutate or null-out IReverseProxyFeature.AvailableDestinations in custom middleware.
  3. Verify the route matched has a cluster with destinations configured.

Example fix

// before — wrong order
app.MapReverseProxy(p =>
{
    p.UseHttpSysDelegation(); // runs before destinations set
    p.UseLoadBalancing();
});
// after
app.MapReverseProxy(p =>
{
    p.UseLoadBalancing();
    p.UseHttpSysDelegation();
});
Defensive patterns

Strategy: validation

Validate before calling

var feature = context.GetReverseProxyFeature();
if (feature.AvailableDestinations is null)
{
    context.Response.StatusCode = 503;
    return;
}

Type guard

static bool HasDestinations(HttpContext ctx) =>
    ctx.GetReverseProxyFeature().AvailableDestinations is not null;

Try / catch

try { await _next(context); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Destinations collection was not set"))
{ context.Response.StatusCode = 503; }

Prevention

When it happens

Trigger: The delegation middleware runs before the proxy has populated AvailableDestinations on the IReverseProxyFeature, or the feature itself was not attached to the context. Typically a middleware-ordering or pipeline-construction error.

Common situations: Placing UseHttpSysDelegation before UseLoadBalancing or before the proxy feature is set. Custom middleware clearing the feature. Misconfigured MapReverseProxy pipeline ordering.

Related errors


AI-assisted analysis of dotnet/yarp@bd11867bee (2026-08-13). Data as JSON: /api/errors/cc03db5958e66685. Report an issue: GitHub.