Getting started with Purview.Aspire.ResourceKit
Getting started with Purview.Aspire.ResourceKit
Section titled “Getting started with Purview.Aspire.ResourceKit”This guide walks through a minimal host + resource setup using source generation.
1) Add the package
Section titled “1) Add the package”Install the Purview.Aspire.ResourceKit package in your AppHost project.
2) Define a host kit
Section titled “2) Define a host kit”Create a partial class and annotate it with [HostKit].
using Purview.Aspire.ResourceKit;
[HostKit]partial class ShopHostKit;3) Define one or more resources
Section titled “3) Define one or more resources”Create a partial resource class per resource and annotate it with [ResourceDefinition].
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);}Choose an attribute style
Section titled “Choose an attribute style”You have two valid styles:
- Generic style:
[ResourceDefinition<TResource>(...)]- Preferred for most cases.
- Do not declare an explicit base type.
- Non-generic style:
[ResourceDefinition(...)]- Requires an explicit compatible base type that provides the resource type.
Example non-generic style:
[ResourceDefinition("api")]partial class ApiResourceKit : ResourceKitBase<ProjectResource>{ protected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder) => builder.AddProject<Projects.Example_Service>(Name);}Avoid mixing both styles on the same class.
3.1) Extend generated typed options (HostKit + ResourceKit)
Section titled “3.1) Extend generated typed options (HostKit + ResourceKit)”ResourceKit generates typed options as nested sealed partial classes, so you can extend them with your own settings.
Extend resource options
Section titled “Extend resource options”Add a nested partial options class inside your resource kit:
[ResourceDefinition<ProjectResource>("api")]sealed partial class ApiResourceKit{ protected override IResourceBuilder<ProjectResource> BuildResource(IDistributedApplicationBuilder builder) => builder.AddProject<Projects.Example_Service>(Name);
partial class ApiResourceKitOptions { public string PublishEnvironmentVariableName { get; set; } = "PUBLISH_MARKER"; }}Then consume it from the generated Options property inside the resource kit.
Extend host options
Section titled “Extend host options”Add a nested partial options class inside your host kit:
[HostKit]partial class ShopHostKit{ public sealed partial class ShopHostKitOptions { public bool EnablePreviewResources { get; set; } }}Then consume it via HostKit.Options from your resource kits.
Tip: keep custom option members
publicwithget; set;so configuration binding can populate them.
4) Register generated wiring in AppHost
Section titled “4) Register generated wiring in AppHost”Call the generated extension method from your AppHost entry point.
var builder = DistributedApplication.CreateBuilder(args);builder.AddAspireResourceKit();The method name can be customized with
HostKitAttribute.ExtensionMethodName.
5) Understand Build vs Configure (before adding dependencies)
Section titled “5) Understand Build vs Configure (before adding dependencies)”ResourceKit has two lifecycle pairs:
Build/BuildResource(...)builds the resource itself.Configure/ConfigureResource()wires resources together after build.
Think of it as:
- Build phase: create each resource.
- Configure phase: connect the created resources.
How enablement is decided
Section titled “How enablement is decided”Before BuildResource(...) runs, ResourceKit checks whether the resource should participate:
IsEnabledis the current toggle value.IsResourceEnabled(builder)is called duringBuildto compute the effective runtime state.- If disabled, both build and configure are skipped for that resource.
Use IsResourceEnabled(builder) when enablement should react to runtime conditions.
protected override bool IsResourceEnabled(IDistributedApplicationBuilder builder){ // Example: allow config + environment based behavior. var isEnabledInConfig = IsEnabled; var isProd = builder.Configuration["ASPNETCORE_ENVIRONMENT"] == "Production";
return isEnabledInConfig && isProd;}6) Understand the runtime order
Section titled “6) Understand the runtime order”ResourceKit executes resources in a predictable sequence:
- Instantiate resources from options
- Build enabled resources
- Configure enabled resources
This makes dependency flow explicit and easier to reason about.
7) Expand incrementally
Section titled “7) Expand incrementally”A common progression:
- Start with one resource kit class
- Add more resource kit classes as AppHost grows
- Use generated options to toggle resources for local/dev/test scenarios
For configuration details, continue with Configuration and options.