Udostępnij za pośrednictwem


IDistributedApplicationBuilder.CreateResourceBuilder<T>(T) Method

Definition

Creates a new resource builder based on an existing resource.

public Aspire.Hosting.ApplicationModel.IResourceBuilder<T> CreateResourceBuilder<T> (T resource) where T : Aspire.Hosting.ApplicationModel.IResource;
abstract member CreateResourceBuilder : 'T -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'T (requires 'T :> Aspire.Hosting.ApplicationModel.IResource)> (requires 'T :> Aspire.Hosting.ApplicationModel.IResource)
Public Function CreateResourceBuilder(Of T As IResource) (resource As T) As IResourceBuilder(Of T)

Type Parameters

T

Type of resource.

Parameters

resource
T

An existing resource.

Returns

A resource builder.

Examples

The following example shows the implementation of the AddConnectionString(IDistributedApplicationBuilder, String, String) extension method.

The AddConnectionString(IDistributedApplicationBuilder, String, String) method creates a new ParameterResource in the application model. The return type of AddConnectionString(IDistributedApplicationBuilder, String, String) is IResourceBuilder<T>. The ParameterResource type does not implement the IResourceWithConnectionString.

To work around this issue the AddConnectionString(IDistributedApplicationBuilder, String, String) method wraps the parameter resource in a "surrogate" class which proxies access to the ParameterResource fields but implements IResourceWithConnectionString. The CreateResourceBuilder<T>(T) method assists by allowing the creation of a IResourceBuilder<T> without adding another resource to the application model.

public static IResourceBuilder<IResourceWithConnectionString> AddConnectionString(this IDistributedApplicationBuilder builder, [ResourceName] string name, string? environmentVariableName = null)
{
    var parameterBuilder = builder.AddParameter(name, _ =>
    {
        return builder.Configuration.GetConnectionString(name) ?? throw new DistributedApplicationException($"Connection string parameter resource could not be used because connection string '{name}' is missing.");
    },
    secret: true,
    connectionString: true);

    var surrogate = new ResourceWithConnectionStringSurrogate(parameterBuilder.Resource, environmentVariableName);
    return builder.CreateResourceBuilder(surrogate);
}

Remarks

The CreateResourceBuilder<T>(T) method is used to create an IResourceBuilder<T> for a specific resource where the original resource builder cannot be referenced. This does not create a new resource, but instead returns a resource builder for an existing resource.

This method is typically used when building extensions to .NET Aspire where the original resource builder cannot be referenced directly. Using the CreateResourceBuilder<T>(T) method allows for easier mutation of resources within the application model.

Calling extension methods on the IResourceBuilder<T> typically results in modifications to the Annotations collection. Not all changes to annotations will be effective depending on what stage of the lifecycle the app host is in. See IDistributedApplicationLifecycleHook for more details.

Applies to