|
| 1 | +--- |
| 2 | +title: "Breaking change - WithCommand obsolete and new overload with CommandOptions" |
| 3 | +description: "Learn about the breaking change in .NET Aspire 9.2 where the WithCommand method overload with optional parameters is marked obsolete." |
| 4 | +ms.date: 3/25/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/dotnet/docs-aspire/issues/2888 |
| 7 | +--- |
| 8 | + |
| 9 | +# WithCommand obsolete and new overload with CommandOptions |
| 10 | + |
| 11 | +The <xref:Aspire.Hosting.ResourceBuilderExtensions.WithCommand*> method overload that accepted multiple optional parameters is now marked obsolete. A new overload that accepts an instance of `CommandOptions` has been introduced. This change requires updates to existing code to use the new overload. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET Aspire 9.2 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +The `WithCommand` method overload that accepted multiple optional parameters was available and not marked as obsolete. |
| 20 | + |
| 21 | +```csharp |
| 22 | +var builder = DistributedApplication.CreateBuilder(args); |
| 23 | + |
| 24 | +builder.AddProject<Projects.AspireApp_ApiService>("apiservice") |
| 25 | + .WithCommand( |
| 26 | + name: "command-name", |
| 27 | + displayName: "Command display name", |
| 28 | + executeCommand: async (ExecuteCommandContext context) => |
| 29 | + { |
| 30 | + // Command execution logic here |
| 31 | + await Task.CompletedTask; |
| 32 | + return CommandResults.Success(); |
| 33 | + }, |
| 34 | + updateState: (UpdateCommandStateContext context) => |
| 35 | + { |
| 36 | + // State update logic here |
| 37 | + return ResourceCommandState.Enabled; |
| 38 | + }, |
| 39 | + displayDescription: "Command Description", |
| 40 | + parameter: new[] { "", "" }, |
| 41 | + confirmationMessage: "Are you sure?", |
| 42 | + iconName: "Icons", |
| 43 | + iconVariant: "Red", |
| 44 | + isHighlighted: false); |
| 45 | +``` |
| 46 | + |
| 47 | +## New behavior |
| 48 | + |
| 49 | +The existing <xref:Aspire.Hosting.ResourceBuilderExtensions.WithCommand*> method overload is now marked obsolete. A new overload that accepts an instance of `CommandOptions` should be used instead. |
| 50 | + |
| 51 | +```csharp |
| 52 | +var builder = DistributedApplication.CreateBuilder(args); |
| 53 | + |
| 54 | +builder.AddProject<Projects.AspireApp_ApiService>("apiservice") |
| 55 | + .WithCommand( |
| 56 | + name: "command-name", |
| 57 | + displayName: "Command display name", |
| 58 | + executeCommand: async (ExecuteCommandContext context) => |
| 59 | + { |
| 60 | + // Command execution logic here |
| 61 | + await Task.CompletedTask; |
| 62 | + return CommandResults.Success(); |
| 63 | + }, |
| 64 | + commandOptions: new CommandOptions |
| 65 | + { |
| 66 | + UpdateState = (UpdateCommandStateContext context) => |
| 67 | + { |
| 68 | + // State update logic here |
| 69 | + return ResourceCommandState.Enabled; |
| 70 | + }, |
| 71 | + Description = "Command Description", |
| 72 | + Parameter = new[] { "", "" }, |
| 73 | + ConfirmationMessage = "Are you sure?", |
| 74 | + IconName = "Icons", |
| 75 | + IconVariant = "Red", |
| 76 | + IsHighlighted = false |
| 77 | + }); |
| 78 | +``` |
| 79 | + |
| 80 | +The only required parameters are the `name`, `displayName`, and `executeCommand`. The rest of the parameters are now encapsulated within the `CommandOptions` object, which provides a cleaner and more maintainable API. |
| 81 | + |
| 82 | +## Type of breaking change |
| 83 | + |
| 84 | +This is a [source incompatible](../categories.md#source-compatibility) change. |
| 85 | + |
| 86 | +## Reason for change |
| 87 | + |
| 88 | +This change was made following an API review to improve clarity and maintainability by consolidating optional parameters into a single `CommandOptions` object. |
| 89 | + |
| 90 | +## Recommended action |
| 91 | + |
| 92 | +Update your code to use the new `WithCommand` overload that accepts an instance of `CommandOptions`. Replace calls to the obsolete overload with the new overload. |
| 93 | + |
| 94 | +## Affected APIs |
| 95 | + |
| 96 | +- <xref:Aspire.Hosting.ResourceBuilderExtensions.WithCommand*> |
0 commit comments