microsoft/aspire · error · InvalidOperationException

The Azure Event Hubs resource is already configured to run…

Error message

The Azure Event Hubs resource is already configured to run as an emulator.

What it means

RunAsEmulator can only be applied once per Event Hubs resource; calling it a second time on a resource already flagged as an emulator throws InvalidOperationException. This prevents double-wrapping the resource with conflicting emulator configuration.

Solutions

  1. Remove the duplicate RunAsEmulator call and keep a single invocation
  2. Check builder.Resource.IsEmulator before calling if the call may come from multiple code paths
  3. Consolidate emulator configuration into one shared extension/helper

Example fix

// before
var cosmos = builder.AddAzureEventHubs("eh").RunAsEmulator();
if (useDevMode)
{
    cosmos.RunAsEmulator(c => c.WithLifetime(ContainerLifetime.Persistent)); // throws
}
// after
var cosmos = builder.AddAzureEventHubs("eh");
if (useDevMode)
{
    cosmos.RunAsEmulator(c => c.WithLifetime(ContainerLifetime.Persistent));
}
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try { builder.RunAsEmulator(configureContainer); }
catch (InvalidOperationException) { /* already an emulator — skip */ }

Prevention

When it happens

Trigger: Calling RunAsEmulator twice on the same AzureEventHubsResource builder, e.g. once in shared setup code and again in app-specific configuration.

Common situations: Refactoring that moved emulator setup into a shared helper but left the original call in place; conditional code paths that both invoke RunAsEmulator; duplicating configuration across AppHost projects.

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/4bd5f97bdc620e96. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs:258

    ///
    /// var eventHub = builder.AddAzureEventHubs("eventhubns")
    ///    .RunAsEmulator()
    ///    .AddEventHub("hub");
    ///
    /// builder.AddProject<Projects.InventoryService>()
    ///        .WithReference(eventHub);
    ///
    /// builder.Build().Run();
    /// </code>
    /// </example>
    [AspireExport(RunSyncOnBackgroundThread = true)]
    public static IResourceBuilder<AzureEventHubsResource> RunAsEmulator(this IResourceBuilder<AzureEventHubsResource> builder, Action<IResourceBuilder<AzureEventHubsEmulatorResource>>? configureContainer = null)
    {
        ArgumentNullException.ThrowIfNull(builder);

        if (builder.Resource.IsEmulator)
        {
            throw new InvalidOperationException("The Azure Event Hubs 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());

        builder
            .WithEndpoint(name: "emulator", targetPort: 5672)
            .WithHttpEndpoint(name: EmulatorHealthEndpointName, targetPort: 5300)
            .WithEndpoint(EmulatorHealthEndpointName, e => e.ExcludeReferenceEndpoint = true)
            .WithHttpHealthCheck(endpointName: EmulatorHealthEndpointName, path: "/health")
            .WithAnnotation(new ContainerImageAnnotation
            {
                Registry = EventHubsEmulatorContainerImageTags.Registry,

View on GitHub (pinned to 25830f84bd)