microsoft/aspire · error · InvalidOperationException

The Azure Service Bus resource is already configured to run…

Error message

The Azure Service Bus resource is already configured to run as an emulator.

What it means

RunAsEmulator configures an Azure Service Bus resource to use the local emulator instead of real provisioning; it throws InvalidOperationException if builder.Resource.IsEmulator is already true, since the resource can only be switched to emulator mode once. This prevents duplicate annotations and conflicting container configuration.

Solutions

  1. Remove the duplicate RunAsEmulator call so it is invoked exactly once per resource.
  2. If multiple helpers may configure the emulator, check builder.Resource.IsEmulator before calling RunAsEmulator.
  3. Consolidate emulator configuration into a single extension method in your AppHost.

Example fix

// before
var sb = builder.AddAzureServiceBus("sb").RunAsEmulator().RunAsEmulator();
// after
var sb = builder.AddAzureServiceBus("sb");
if (!sb.Resource.IsEmulator) { sb.RunAsEmulator(); }
Defensive patterns

Strategy: validation

Validate before calling

if (!builder.Resource.IsEmulator)
{
    builder.RunAsEmulator();
}

Prevention

When it happens

Trigger: Calling RunAsEmulator twice on the same IResourceBuilder<AzureServiceBusResource>, or calling it after the resource was already marked as an emulator (e.g. a shared helper also called RunAsEmulator).

Common situations: Copy-pasted AppHost code adding RunAsEmulator in two places; composing extension helpers that each call RunAsEmulator; conditional code paths that both end in emulator configuration.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/6182022f78d33791. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs:402

    ///    .RunAsEmulator()
    ///    .AddQueue("queue");
    ///
    /// builder.AddProject&lt;Projects.InventoryService&gt;()
    ///        .WithReference(serviceBus);
    ///
    /// builder.Build().Run();
    /// </code>
    /// </example>
    /// </remarks>
    /// <ats-remarks />
    [AspireExport(RunSyncOnBackgroundThread = true)]
    public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IResourceBuilder<AzureServiceBusResource> builder, Action<IResourceBuilder<AzureServiceBusEmulatorResource>>? configureContainer = null)
    {
        ArgumentNullException.ThrowIfNull(builder);

        if (builder.Resource.IsEmulator)
        {
            throw new InvalidOperationException("The Azure Service Bus resource is already configured to run as an emulator.");
        }

        if (builder.ApplicationBuilder.ExecutionContext.IsPublishMode)
        {
            return builder;
        }

        // Mark this resource as an emulator for consistent resource identification and tooling support
        builder.WithAnnotation(new EmulatorResourceAnnotation());

        // Add emulator container

        // The password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols
        var passwordParameter = ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder.ApplicationBuilder, $"{builder.Resource.Name}-sql-pwd", minLower: 1, minUpper: 1, minNumeric: 1);

        builder
            .WithEndpoint(name: "emulator", targetPort: 5672)
            .WithHttpEndpoint(name: EmulatorHealthEndpointName, targetPort: 5300)

View on GitHub (pinned to 25830f84bd)