microsoft/aspire · error · InvalidOperationException

Service resources do not consume any services

Error message

Service resources do not consume any services

What it means

Mirroring ServicesProduced, the ServicesConsumed getter on ServiceWithModelResource always throws because a DCP Service resource consumes no services. The property exists only to satisfy the AppResource base contract; accessing it on this subtype is a programming error.

Solutions

  1. Type-check before accessing ServicesConsumed and skip ServiceWithModelResource instances
  2. Derive consumption relationships from EndpointAnnotation/ServiceBinding data on this subtype instead
  3. Add a guard helper on AppResource that returns empty lists for service-only resources

Example fix

// before
var consumed = appResource.ServicesConsumed; // throws for ServiceWithModelResource
// after
var consumed = appResource switch
{
    ServiceWithModelResource => [],
    _ => appResource.ServicesConsumed
};
Defensive patterns

Strategy: type-guard

Validate before calling

if (appResource is ServiceWithModelResource)
{
    // Service resources consume no services.
    return [];
}

Type guard

bool HasConsumedServices(AppResource r) => r is not ServiceWithModelResource;

Try / catch

try
{
    var consumed = appResource.ServicesConsumed;
}
catch (InvalidOperationException) when (appResource is ServiceWithModelResource)
{
    var consumed = [];
}

Prevention

When it happens

Trigger: Reading ServicesConsumed on a ServiceWithModelResource — typically during generic traversal of app resources to build service consumption graphs.

Common situations: Code that computes consumed/produced service sets uniformly over all AppResource subtypes; refactoring that changed resource filtering and now includes Service resources.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/871b1ee746466c75. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting/Dcp/AppResource.cs:93

        ModelResource = modelResource;
    }

    public virtual List<ServiceWithModelResource> ServicesProduced { get; } = [];
    public virtual List<ServiceWithModelResource> ServicesConsumed { get; } = [];
}

internal sealed class ServiceWithModelResource : RenderedModelResource<Service>
{
    public Service Service => DcpResource;
    public EndpointAnnotation EndpointAnnotation { get; }

    public override List<ServiceWithModelResource> ServicesProduced
    {
        get { throw new InvalidOperationException("Service resources do not produce any services"); }
    }
    public override List<ServiceWithModelResource> ServicesConsumed
    {
        get { throw new InvalidOperationException("Service resources do not consume any services"); }
    }

    public ServiceWithModelResource(IResource modelResource, Service service, EndpointAnnotation sba) : base(modelResource, service)
    {
        EndpointAnnotation = sba;
    }
}

internal interface IResourceReference
{
    IResource ModelResource { get; }
    string DcpResourceName { get; }
}

View on GitHub (pinned to 25830f84bd)