Skip to content

Attributes reference

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 ships two public attributes in the Purview.Aspire.ResourceKit namespace. Both are generated into the consuming compilation by the bundled source generator, so you do not reference a separate attribute package.

Marks the single host kit class per compilation. Exactly one host kit may exist per compilation (SG0004).

[HostKit]
sealed partial class ShopHostKit;

Optional named arguments:

ArgumentTypeDefaultPurpose
Namestring?nullControls generated naming.
ExtensionMethodNamestring?Derived from host kit nameOverrides the generated builder extension name (for example AddAspireResourceKit()).
GenerateOptionsbooltrueEnables/disables generated host options.

ResourceDefinitionAttribute ([ResourceDefinition])

Section titled “ResourceDefinitionAttribute ([ResourceDefinition])”

Marks a resource kit class that participates in generation.

[ResourceDefinition<ProjectResource>("api", PropertyName = "API")]
partial class APIResourceKit;

Options:

ArgumentTypeDefaultPurpose
Namestring?Derived from class name (suffix trimmed)Logical Aspire resource name.
PropertyNamestring?Derived from class nameGenerated host property name. Must be a valid C# identifier (SG0008).
AspireResourceTypegeneric type argumentnullThe Aspire IResource type (generic style only).

ResourceDefinition vs ResourceDefinition<TResource>

Section titled “ResourceDefinition vs ResourceDefinition<TResource>”

ResourceKit supports two declaration styles with different base-type behavior.

Use [ResourceDefinition<TResource>] when you want the resource type declared directly on the attribute.

[ResourceDefinition<ProjectResource>("api")]
partial class APIResourceKit
{
protected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder)
=> builder.AddProject<Projects.Example_Service>(Name);
}
  • Do not declare an explicit base type on the class (SG0015).
  • The generator supplies the host-specific base in generated partial code.

The type argument may be:

  • an Aspire resource type implementing IResource (used as-is), or
  • an IProjectMetadata type (the type accepted by AddProject<TProject>()), which is mapped to ProjectResource for the generated base class.

Anything else is rejected (SG0016).

Use [ResourceDefinition] when you prefer (or need) to specify the resource type through an explicit base type.

[ResourceDefinition("api")]
partial class APIResourceKit : ResourceKitBase<ProjectResource>
{
protected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder)
=> builder.AddProject<Projects.Example_Service>(Name);
}
  • You must declare an explicit valid base type (SG0014).
  • The base must derive from the generated ResourceKitBase<TResource> or the runtime ResourceKitBase<THostKit, TResource> (SG0006).
SituationDiagnosticSeverity
Both styles on one classSG0013Error
Generic style with explicit baseSG0015Error
Non-generic style without explicit baseSG0014Error
Explicit base not derived from a valid Resource Kit baseSG0006Error
Resource type cannot be inferred/foundSG0016Error

Attributed classes must not declare constructors with parameters or executable constructor bodies (SG0012). The generator supplies the constructor wiring in generated partial code; the primary constructor of a resource kit receives the host kit and options. In the manual (non-generated) pattern you declare it yourself — see Examples.