Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New article]: Create resource commands #2015

Open
JamesNK opened this issue Nov 7, 2024 · 1 comment
Open

[New article]: Create resource commands #2015

JamesNK opened this issue Nov 7, 2024 · 1 comment
Assignees
Labels
doc-idea Indicates issues that are suggestions for new topics [org][type][category] 📦 release-9.0 Used to track doc updates for release 9.0 of .NET Aspire.
Milestone

Comments

@JamesNK
Copy link
Member

JamesNK commented Nov 7, 2024

Proposed topic or title

Create resource commands

Location in table of contents.

Custom integrations

Reason for the article

Aspire 9 introduces an API to add custom resource commands. People will want to know how to implement there own.

Mentioned in release notes here:

### Resource commands
The app host supports adding custom commands to resources. This is useful when you want to run custom commands on a resource.
> [!IMPORTANT]
> These .NET Aspire dashboard commands are only available when running the dashboard locally. They are not available when running the dashboard in Azure Container Apps.
The following example uses an extension method to add some additional commands.
```csharp
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithClearCommand();
builder.AddProject<Projects.WebApplication1>("api")
.WithReference(cache)
.WaitFor(cache);
builder.Build().Run();
```
Custom commands can be added by calling the `WithCommand*` method and passing in the command to run:
```csharp
using Microsoft.Extensions.Diagnostics.HealthChecks;
using StackExchange.Redis;
public static class RedisCommandExtensions
{
public static IResourceBuilder<RedisResource> WithClearCommand(
this IResourceBuilder<RedisResource> builder)
{
builder.WithCommand(
"clear-cache",
"Clear Cache",
async context =>
{
var redisConnectionString = await builder.Resource.GetConnectionStringAsync() ??
throw new InvalidOperationException(
"Unable to get the Redis connection string.");
using var connection = ConnectionMultiplexer.Connect(redisConnectionString);
await connection.GetDatabase().ExecuteAsync("FLUSHALL");
return CommandResults.Success();
},
context =>
{
if (context.ResourceSnapshot.HealthStatus is HealthStatus.Healthy)
{
return ResourceCommandState.Enabled;
}
return ResourceCommandState.Disabled;
});
return builder;
}
}
```
These commands can be run from the dashboard:
:::image type="content" source="media/clear-cache-command.png" lightbox="media/clear-cache-command.png" alt-text="Clear cache command on dashboard":::

Article abstract

The article would talk about what resource commands are, show them in the dashbaord UI, and discuss examples of what custom commands can do.

It would then show code for an example custom command.

Relevant searches

No response

@JamesNK JamesNK added the doc-idea Indicates issues that are suggestions for new topics [org][type][category] label Nov 7, 2024
@dotnetrepoman dotnetrepoman bot added the ⌚ Not Triaged Not triaged label Nov 7, 2024
@JamesNK JamesNK added this to the 9.0 milestone Nov 7, 2024
@JamesNK JamesNK added 📦 release-9.0 Used to track doc updates for release 9.0 of .NET Aspire. and removed ⌚ Not Triaged Not triaged labels Nov 7, 2024
@afscrome
Copy link

afscrome commented Nov 7, 2024

Can you include docs on icon set, and where to find a list of icons from - e.g. DatabasePlugConnected. A list of icons can be found at https://github.com/microsoft/fluentui-system-icons/blob/main/icons_regular.md .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-idea Indicates issues that are suggestions for new topics [org][type][category] 📦 release-9.0 Used to track doc updates for release 9.0 of .NET Aspire.
Projects
None yet
Development

No branches or pull requests

3 participants