Skip to content

Project resources

Preview Aspire ResourceKit Reviewed 2026-09-16 purview-dev/aspire-resourcekit apphost aspire cloud-native code-generation csharp developer-tools distributed-systems dotnet dotnet-aspire integration-testing resource-management roslyn source-generators testing

ResourceKit has first-class support for Aspire project resources (AddProject<T>()) with generator and analyzer validation to catch wiring mistakes.

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 ProjectResource for 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>().

// OK
protected 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 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 use ProjectResource (SG0019).

See Attributes Reference for the base-type rules shared by all resources.