Skip to content

Instantly share code, notes, and snippets.

@afscrome
Created September 12, 2025 17:22
Show Gist options
  • Save afscrome/176b289ba7ae1c0c83d540465e7a296e to your computer and use it in GitHub Desktop.
Save afscrome/176b289ba7ae1c0c83d540465e7a296e to your computer and use it in GitHub Desktop.
// This is an example of using the Aspire Interaction service to prompt users
// to install a missing dependencies if it's not present.
//
// Ideally you can get all your dependencies through `dotnet restore`, or other package mangers that run as part of your app host,
// but that may not be possible, particularly if third parties don't provide package manager friendly setup.
//
// `Helper.IsInstalled()` and `Helper.Install()` implementations are not included and will vary based on your dependency
public static T WithDependencyInstaller<T>(this T builder)
where T : IDistributedApplicationBuilder
{
if (!builder.ExecutionContext.IsRunMode
|| !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|| Environment.GetEnvironmentVariable("TF_BUILD") != null)
{
return builder;
}
builder.Eventing.Subscribe<BeforeStartEvent>((evt, ct) =>
{
_ = Task.Run(async () =>
{
if (Helper.IsInstalled())
{
return;
}
var interactionService = evt.Services.GetRequiredService<IInteractionService>();
if (!interactionService.IsAvailable)
{
var log = evt.Services.GetRequiredService<ILogger<LoggerMarker>>();
log.LogWarning("XYZ is not installed, please follow the instructions at https://myorg.com/internal-docs to install it.");
return;
}
var result = await interactionService.PromptNotificationAsync(
title: "Install XYZ",
message: "You are missing XYZ. Would you like to install it?",
options: new NotificationInteractionOptions
{
Intent = MessageIntent.Warning,
LinkUrl = "https://myorg.com/internal-docs",
PrimaryButtonText = "Install",
ShowDismiss = false
});
if (result.Canceled || result.Data == false)
{
return;
}
try
{
await Helper.Install(ct);
}
catch (Exception ex)
{
await interactionService.PromptMessageBoxAsync(
title: "Installation Failed",
message: $"""
Failed to install the XYr:
```plain
{ex}
```
""",
options: new MessageBoxInteractionOptions
{
EnableMessageMarkdown = true,
Intent = MessageIntent.Error,
});
}
});
return Task.CompletedTask;
});
return builder;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment