Project resources
Project resources
Section titled “Project resources”ResourceKit has first-class support for Aspire project resources (AddProject<T>()) with generator
and analyzer validation to catch wiring mistakes.
Declaring a project resource kit
Section titled “Declaring a project resource kit”Use the Aspire-generated project reference type directly in the attribute. The generator maps it to
ProjectResource for the generated base class:
using Aspire.Hosting;using Aspire.Hosting.ApplicationModel;using Purview.Aspire.ResourceKit;
[ResourceDefinition<Projects.Example_Service>("api")]partial class ApiResourceKit{ protected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder) => builder.AddProject<Projects.Example_Service>(Name);}Projects.Example_Service is the type generated by the Aspire SDK into the AppHost. It implements
IProjectMetadata (the type accepted by AddProject<TProject>()). When the type argument implements
IProjectMetadata, ResourceKit:
- validates the resource type (rather than rejecting it),
- maps it to
ProjectResourcefor the generated base class, - enables the two project-specific execution-only rules below.
The declared project must be added (SG0018)
Section titled “The declared project must be added (SG0018)”BuildResource must register the same project it declares via AddProject<T>().
// OKprotected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder) => builder.AddProject<Projects.Example_Service>(Name);// SG0018 (warning): the declared project is never registered at runtime.[ResourceDefinition<Projects.Example_Service>("api")]partial class ApiResourceKit{ protected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder) => builder.AddContainer("api", "my-image");}SG0018 is an execution-only warning: generation still proceeds so you can complete the override without losing the host kit output. At runtime the declared project is not registered, so the resource fails.
Explicit base must use ProjectResource (SG0019)
Section titled “Explicit base must use ProjectResource (SG0019)”When a project resource kit declares an explicit base class (non-generic attribute style), the base
must resolve to ProjectResource.
[ResourceDefinition("api")]partial class ApiResourceKit : ResourceKitBase<ProjectResource>{ protected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder) => builder.AddProject<Projects.Example_Service>(Name);}If the explicit base resolves to a different resource type, SG0019 is reported (execution-only warning) because that base cannot build the declared project.
Generic vs non-generic styles
Section titled “Generic vs non-generic styles”- Generic style (
[ResourceDefinition<Projects.Example_Service>(...)]) is recommended for project resources: the type is declared on the attribute and the base is supplied by the generator. - Non-generic style (
[ResourceDefinition(...)]+ explicit base) requires the base to useProjectResource(SG0019).
See Attributes Reference for the base-type rules shared by all resources.