Enablement: IsEnabled vs IsResourceEnabled
Enablement: IsEnabled vs IsResourceEnabled
Section titled “Enablement: IsEnabled vs IsResourceEnabled”ResourceKit supports flexible runtime enablement through two related members.
The two toggles
Section titled “The two toggles”IsEnabled— the current enablement flag, usually sourced from generated options. This is the persisted/configured toggle.IsResourceEnabled(builder)— a runtime decision hook. Its default implementation returnsIsEnabled, and you override it when enablement should react to runtime conditions.
How they interact
Section titled “How they interact”At runtime, Build evaluates enablement before resource construction:
- If
IsEnabledistrue, ResourceKit callsIsResourceEnabled(builder)and assigns the result back toIsEnabled. - If the resulting
IsEnabledisfalse, bothBuildResource(...)andConfigureResource()are skipped for that resource.
Two important consequences:
- The hook is only invoked when
IsEnabledis alreadytrue. A kit disabled viaIsEnabled=false(for example from options) is never re-enabled by the hook. ResourceBuildercannot be accessed while the resource is disabled — doing so throws anInvalidOperationException(the property guards itself).
When to override IsResourceEnabled
Section titled “When to override IsResourceEnabled”Use the hook when enablement depends on runtime state rather than only static options. Common examples are environment-specific availability, publish mode, or dynamic configuration checks.
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;}Publish-only resources are a typical pattern. The example hosts run Key Vault and a publish marker parameter only when publishing:
protected override bool IsResourceEnabled(IDistributedApplicationBuilder builder) => builder.ExecutionContext.IsPublishMode;Disabling a resource from options
Section titled “Disabling a resource from options”Because IsEnabled is bound from generated options, a resource can be disabled without code changes
by setting the options section in configuration:
{ "ShopHostKit": { "Redis": { "IsEnabled": false } }}See Configuration and Options for the generated options shape.