{"record":{"id":"871b1ee746466c75","repo":"microsoft/aspire","slug":"service-resources-do-not-consume-any-services","errorCode":null,"errorMessage":"Service resources do not consume any services","messagePattern":"Service resources do not consume any services","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/Dcp/AppResource.cs","lineNumber":93,"sourceCode":"        ModelResource = modelResource;\n    }\n\n    public virtual List<ServiceWithModelResource> ServicesProduced { get; } = [];\n    public virtual List<ServiceWithModelResource> ServicesConsumed { get; } = [];\n}\n\ninternal sealed class ServiceWithModelResource : RenderedModelResource<Service>\n{\n    public Service Service => DcpResource;\n    public EndpointAnnotation EndpointAnnotation { get; }\n\n    public override List<ServiceWithModelResource> ServicesProduced\n    {\n        get { throw new InvalidOperationException(\"Service resources do not produce any services\"); }\n    }\n    public override List<ServiceWithModelResource> ServicesConsumed\n    {\n        get { throw new InvalidOperationException(\"Service resources do not consume any services\"); }\n    }\n\n    public ServiceWithModelResource(IResource modelResource, Service service, EndpointAnnotation sba) : base(modelResource, service)\n    {\n        EndpointAnnotation = sba;\n    }\n}\n\ninternal interface IResourceReference\n{\n    IResource ModelResource { get; }\n    string DcpResourceName { get; }\n}\n","sourceCodeStart":75,"sourceCodeEnd":107,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/Dcp/AppResource.cs#L75-L107","documentation":"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.","triggerScenarios":"Reading ServicesConsumed on a ServiceWithModelResource — typically during generic traversal of app resources to build service consumption graphs.","commonSituations":"Code that computes consumed/produced service sets uniformly over all AppResource subtypes; refactoring that changed resource filtering and now includes Service resources.","solutions":["Type-check before accessing ServicesConsumed and skip ServiceWithModelResource instances","Derive consumption relationships from EndpointAnnotation/ServiceBinding data on this subtype instead","Add a guard helper on AppResource that returns empty lists for service-only resources"],"exampleFix":"// before\nvar consumed = appResource.ServicesConsumed; // throws for ServiceWithModelResource\n// after\nvar consumed = appResource switch\n{\n    ServiceWithModelResource => [],\n    _ => appResource.ServicesConsumed\n};","handlingStrategy":"type-guard","validationCode":"if (appResource is ServiceWithModelResource)\n{\n    // Service resources consume no services.\n    return [];\n}","typeGuard":"bool HasConsumedServices(AppResource r) => r is not ServiceWithModelResource;","tryCatchPattern":"try\n{\n    var consumed = appResource.ServicesConsumed;\n}\ncatch (InvalidOperationException) when (appResource is ServiceWithModelResource)\n{\n    var consumed = [];\n}","preventionTips":["Filter to non-Service resource types before reading ServicesConsumed","Model service consumption from EndpointAnnotation/ServiceBinding data instead","Centralize the subtype check in one helper to avoid scattered guards","Test resource-graph walks against apps containing Service resources"],"tags":["dcp","services","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}