Compartilhar via


IDistributedApplicationBuilder.AddResource<T>(T) Method

Definition

Adds a resource of type T to the distributed application.

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

Type Parameters

T

The type of resource to add.

Parameters

resource
T

The resource to add.

Returns

A builder for configuring the added resource.

Exceptions

Thrown when a resource with the same name already exists.

Examples

This example shows the implementation of the AddContainer(IDistributedApplicationBuilder, String, String) method which makes use of the AddResource<T>(T) method to add a container resource to the application. In .NET Aspire the pattern for defining new resources is to include a method that extends IDistributedApplicationBuilder and and then constructs a resource derived from IResource and adds it to the application model using the AddResource<T>(T) method. Other extension methods (such as WithImage<T>(IResourceBuilder<T>, String, String) in this case) can be chained to configure the resource as desired.

public static IResourceBuilder<ContainerResource> AddContainer(this IDistributedApplicationBuilder builder, [ResourceName] string name, string image, string tag)
{
    var container = new ContainerResource(name);
    return builder.AddResource(container)
                  .WithImage(image, tag);
}

Remarks

The AddResource<T>(T) method is not typically used directly by developers building Aspire-based applications. It is typically used by developers building extensions to Aspire and is called from within an extension method to add a custom resource to the application model.

Applies to