Generated output
From your [HostKit] and [ResourceDefinition] declarations, the generator emits all of the
boilerplate that wires your resource kits together. The generated code is marked with
[CompilerGenerated] and [GeneratedCode("HostKitGenerator", ...)] and is excluded from code coverage.
What gets generated
Section titled “What gets generated”- a host resource base class (the concrete host kit deriving from
HostKitBase<THostKit>), - resource properties on the host (one per resource definition),
- host + per-resource options (when
GenerateOptionsis enabled), - a typed
ResourceKitBase<TResource>base per host kit, - each resource kit’s
sealed partialskeleton with a constructor andOptionsproperty, - an AppHost extension method to build/configure/register the host kit.
Host kit members
Section titled “Host kit members”For every [ResourceDefinition], the host kit gets a lazily initialized property. Accessing a
property before Build throws, and setting it twice throws:
public Purview.Aspire.ResourceKit.Example.AppHost.AppModels.Resources.ExampleAPIKit ExampleAPI{ get { return field ?? throw new global::System.InvalidOperationException( "The 'ExampleAPI' resource has not been initialized. Call Build first."); } private set { ... }}Child properties
Section titled “Child properties”A kit that declares Parent = typeof(TParentKit) gains a typed Parent property, and its parent gains a
typed child property with an assembly-internal setter:
internal global::Example.PostgresKit Postgres { get; private set; }internal global::Example.PostgresDatabaseKit Database { get; internal set; }
internal global::Example.PostgresKit Parent { get; }The generated host constructs the parent first, then assigns the child through the parent:
Postgres.Database = new(this, Postgres, Options.Postgres.Database);AddResource(Postgres.Database);Root kits are unchanged. See Parent and child resource kits.
Generated host Build/Configure
Section titled “Generated host Build/Configure”Build instantiates roots first and then each child with its parent instance, registers them in
parent-before-child order, invokes the optional onBuilt callback, then calls base.Build(builder)
(which validates the ownership graph before creating anything):
public override void Build(global::Aspire.Hosting.IDistributedApplicationBuilder builder){ // Creating ExampleAPIKit Resource Kit. ExampleAPI = new(this, Options.ExampleAPI);
// Register the discovered app resources with the base class. AddResource(ExampleAPI);
// Now the additional post-build func builder onBuilt?.Invoke(this, builder);
base.Build(builder);}
public override void Configure(){ base.Configure(); onConfigured?.Invoke(this);}Generated resource kit skeleton
Section titled “Generated resource kit skeleton”Each resource kit derives from the generated ResourceKitBase<TResource> and gains a constructor that
accepts the host kit and its options:
internal sealed partial class ExampleAPIKit : global::Purview.Aspire.ResourceKit.ResourceKitBase<ProjectResource>{ public ExampleAPIKit(ExampleHostKit hostKit, ExampleAPIKitOptions options) : base(hostKit, (options ?? throw new global::System.ArgumentNullException(nameof(options))).Name) { Options = options; IsEnabled = options.IsEnabled; }
public ExampleAPIKitOptions Options { get; }
protected override global::Purview.Aspire.ResourceKit.ResourceKitMode Mode => Options.Mode;}Your partial class supplies BuildResource(...) and optionally ConfigureResource() and
IsResourceEnabled(builder). Mode is generated (sourced from the options, or from the attribute when
options are disabled), so publish-only and local-development-only resources are configuration rather
than code. See Enablement.
Generated options
Section titled “Generated options”Host options are nested under the host kit and expose a SectionName constant plus one nested options
object per resource:
public sealed partial class ExampleHostKitOptions{ public const string SectionName = "ExampleHostKit";
public global::Purview.Aspire.ResourceKit.ResourceKitMode Mode { get; set; } = global::Purview.Aspire.ResourceKit.ResourceKitMode.All;
public ExampleAPIKitOptions ExampleAPI { get; init; } = new();}Only roots are host-level options. A child kit’s options are a property on its parent’s options, mirroring the ownership hierarchy:
public sealed partial class PostgresKitOptions{ public bool IsEnabled { get; set; } = true;
public PostgresDatabaseKit.PostgresDatabaseKitOptions Database { get; init; } = new();}Each resource options type carries Name (defaulting to the logical resource name), IsEnabled, and
Mode. Mode defaults to the value declared on the attribute (or All when nothing is declared):
public sealed partial class ExampleAPIKitOptions{ [global::System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = false)] public string Name { get; set; } = "api";
public bool IsEnabled { get; set; } = true;
public global::Purview.Aspire.ResourceKit.ResourceKitMode Mode { get; set; } = global::Purview.Aspire.ResourceKit.ResourceKitMode.Publish;}The generated host kit and every generated resource kit override Mode from those options, so
Build gates on the configured mode before IsResourceEnabled(builder) and BuildResource(...) run.
Name is decorated with [Required(AllowEmptyStrings = false)]; the generated extension method calls
ValidateOnStart().
Generated extension method
Section titled “Generated extension method”The extension method binds host options from configuration, creates the host kit, and runs the full lifecycle:
public static global::Aspire.Hosting.IDistributedApplicationBuilder AddAspireResourceKit( this global::Aspire.Hosting.IDistributedApplicationBuilder builder, global::System.Action<ExampleHostKit, global::Aspire.Hosting.IDistributedApplicationBuilder>? onBuilt = null, global::System.Action<ExampleHostKit>? onConfigured = null, global::System.Action<global::Microsoft.Extensions.Options.OptionsBuilder<ExampleHostKitOptions>>? configureOptions = null){ var optionsBuilder = builder.Services .AddOptions<ExampleHostKitOptions>() .BindConfiguration(ExampleHostKitOptions.SectionName);
configureOptions?.Invoke(optionsBuilder); optionsBuilder.ValidateOnStart();
var hostKitOptions = builder.Configuration .GetSection(ExampleHostKitOptions.SectionName) .Get<ExampleHostKitOptions>() ?? new();
ExampleHostKit hostKit = new(onBuilt, onConfigured, hostKitOptions); hostKit.Build(builder); hostKit.Configure(); builder.Services.AddSingleton(hostKit); return builder;}The extension method name defaults to Add<HostKitName>() (for example AddAspireResourceKit() for
ExampleHostKit) and can be overridden with HostKitAttribute.ExtensionMethodName.