ResourceBuilderExtensions.WithHttpCommand Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
WithHttpCommand<TResource>(IResourceBuilder<TResource>, String, String, Func<EndpointReference>, String, HttpCommandOptions) |
Adds a command to the resource that when invoked sends an HTTP request to the specified endpoint and path. |
WithHttpCommand<TResource>(IResourceBuilder<TResource>, String, String, String, String, HttpCommandOptions) |
Adds a command to the resource that when invoked sends an HTTP request to the specified endpoint and path. |
WithHttpCommand<TResource>(IResourceBuilder<TResource>, String, String, Func<EndpointReference>, String, HttpCommandOptions)
- Source:
- ResourceBuilderExtensions.cs
Adds a command to the resource that when invoked sends an HTTP request to the specified endpoint and path.
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<TResource> WithHttpCommand<TResource>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<TResource> builder, string path, string displayName, Func<Aspire.Hosting.ApplicationModel.EndpointReference>? endpointSelector, string? commandName = default, Aspire.Hosting.ApplicationModel.HttpCommandOptions? commandOptions = default) where TResource : Aspire.Hosting.ApplicationModel.IResourceWithEndpoints;
static member WithHttpCommand : Aspire.Hosting.ApplicationModel.IResourceBuilder<'Resource (requires 'Resource :> Aspire.Hosting.ApplicationModel.IResourceWithEndpoints)> * string * string * Func<Aspire.Hosting.ApplicationModel.EndpointReference> * string * Aspire.Hosting.ApplicationModel.HttpCommandOptions -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'Resource (requires 'Resource :> Aspire.Hosting.ApplicationModel.IResourceWithEndpoints)> (requires 'Resource :> Aspire.Hosting.ApplicationModel.IResourceWithEndpoints)
<Extension()>
Public Function WithHttpCommand(Of TResource As IResourceWithEndpoints) (builder As IResourceBuilder(Of TResource), path As String, displayName As String, endpointSelector As Func(Of EndpointReference), Optional commandName As String = Nothing, Optional commandOptions As HttpCommandOptions = Nothing) As IResourceBuilder(Of TResource)
Type Parameters
- TResource
The type of the resource.
Parameters
- builder
- IResourceBuilder<TResource>
The resource builder.
- path
- String
The path to send the request to when the command is invoked.
- displayName
- String
The display name visible in UI.
- endpointSelector
- Func<EndpointReference>
A callback that selects the HTTP endpoint to send the request to when the command is invoked.
- commandName
- String
The name of command. The name uniquely identifies the command.
- commandOptions
- HttpCommandOptions
Optional configuration for the command.
Returns
The IResourceBuilder<T>.
Exceptions
Examples
Adds commands to a project resource that when invoked sends an HTTP POST request to an endpoint on a separate load generator resource, to generate load against the resource the command was executed against.
var loadGenerator = builder.AddProject>LoadGenerator>("load");
var loadGeneratorEndpoint = loadGenerator.GetEndpoint("https");
var customerService = builder.AddProject>CustomerService>("customer-service")
.WithHttpCommand("/stress?resource=customer-service&requests=1000", "Apply load (1000)", endpointSelector: () => loadGeneratorEndpoint)
.WithHttpCommand("/stress?resource=customer-service&requests=5000", "Apply load (5000)", endpointSelector: () => loadGeneratorEndpoint);
loadGenerator.WithReference(customerService);
Remarks
The command will be added to the resource represented by builder
.
If no EndpointSelector is specified, the first HTTP endpoint found on the resource will be used. HTTP endpoints with an https
scheme are preferred over those with an http
scheme. If no HTTP endpoint is found on the resource, an exception will be thrown.
The supplied EndpointSelector may return an endpoint from a different resource to that which the command is being added to.
The command will not be enabled until the endpoint is allocated and the resource the endpoint is associated with is healthy.
If Method is not specified, POST
will be used.
Specifying a HttpClientName will use that named HttpClient when sending the request. This allows you to configure the HttpClient instance with a specific handler or other options using AddHttpClient(IServiceCollection, String). If no HttpClientName is specified, the default HttpClient will be used.
The PrepareRequest callback will be invoked to configure the request before it is sent. This can be used to add headers or a request payload before the request is sent.
The GetCommandResult callback will be invoked after the response is received to determine the result of the command invocation. If this callback is not specified, the command will be considered succesful if the response status code is in the 2xx range.
Applies to
WithHttpCommand<TResource>(IResourceBuilder<TResource>, String, String, String, String, HttpCommandOptions)
- Source:
- ResourceBuilderExtensions.cs
Adds a command to the resource that when invoked sends an HTTP request to the specified endpoint and path.
public static Aspire.Hosting.ApplicationModel.IResourceBuilder<TResource> WithHttpCommand<TResource>(this Aspire.Hosting.ApplicationModel.IResourceBuilder<TResource> builder, string path, string displayName, string? endpointName = default, string? commandName = default, Aspire.Hosting.ApplicationModel.HttpCommandOptions? commandOptions = default) where TResource : Aspire.Hosting.ApplicationModel.IResourceWithEndpoints;
static member WithHttpCommand : Aspire.Hosting.ApplicationModel.IResourceBuilder<'Resource (requires 'Resource :> Aspire.Hosting.ApplicationModel.IResourceWithEndpoints)> * string * string * string * string * Aspire.Hosting.ApplicationModel.HttpCommandOptions -> Aspire.Hosting.ApplicationModel.IResourceBuilder<'Resource (requires 'Resource :> Aspire.Hosting.ApplicationModel.IResourceWithEndpoints)> (requires 'Resource :> Aspire.Hosting.ApplicationModel.IResourceWithEndpoints)
<Extension()>
Public Function WithHttpCommand(Of TResource As IResourceWithEndpoints) (builder As IResourceBuilder(Of TResource), path As String, displayName As String, Optional endpointName As String = Nothing, Optional commandName As String = Nothing, Optional commandOptions As HttpCommandOptions = Nothing) As IResourceBuilder(Of TResource)
Type Parameters
- TResource
The type of the resource.
Parameters
- builder
- IResourceBuilder<TResource>
The resource builder.
- path
- String
The path to send the request to when the command is invoked.
- displayName
- String
The display name visible in UI.
- endpointName
- String
The name of the HTTP endpoint on this resource to send the request to when the command is invoked.
- commandName
- String
Optional name of the command. The name uniquely identifies the command. If a name isn't specified then it's inferred using the command's endpoint and HTTP method.
- commandOptions
- HttpCommandOptions
Optional configuration for the command.
Returns
The IResourceBuilder<T>.
Examples
Adds a command to the project resource that when invoked sends an HTTP POST request to the path /clear-cache
.
var apiService = builder.AddProject>MyApiService>("api")
.WithHttpCommand("/clear-cache", "Clear cache");
Adds a command to the project resource that when invoked sends an HTTP GET request to the path /reset-db
on endpoint named admin
.
The request's headers are configured to include an X-Admin-Key
header for verification.
var adminKey = builder.AddParameter("admin-key");
var apiService = builder.AddProject>MyApiService>("api")
.WithHttpsEndpoint("admin")
.WithEnvironment("ADMIN_KEY", adminKey)
.WithHttpCommand("/reset-db", "Reset database",
endpointName: "admin",
commandOptions: new ()
{
Method = HttpMethod.Get,
ConfirmationMessage = "Are you sure you want to reset the database?",
PrepareRequest: request =>
{
request.Headers.Add("X-Admin-Key", adminKey);
return Task.CompletedTask;
}
});
Remarks
The command will be added to the resource represented by builder
.
If endpointName
is specified, the request will be sent to the endpoint with that name on the resource represented by builder
. If an endpoint with that name is not found, or the endpoint with that name is not an HTTP endpoint, an exception will be thrown.
If no endpointName
is specified, the first HTTP endpoint found on the resource will be used. HTTP endpoints with an https
scheme are preferred over those with an http
scheme. If no HTTP endpoint is found on the resource, an exception will be thrown.
The command will not be enabled until the endpoint is allocated and the resource the endpoint is associated with is healthy.
If Method is not specified, POST
will be used.
Specifying HttpClientName will use that named HttpClient when sending the request. This allows you to configure the HttpClient instance with a specific handler or other options using AddHttpClient(IServiceCollection, String). If HttpClientName is not specified, the default HttpClient will be used.
The PrepareRequest callback will be invoked to configure the request before it is sent. This can be used to add headers or a request payload before the request is sent.
The GetCommandResult callback will be invoked after the response is received to determine the result of the command invocation. If this callback is not specified, the command will be considered succesful if the response status code is in the 2xx range.