{"record":{"id":"4bd5f97bdc620e96","repo":"microsoft/aspire","slug":"the-azure-event-hubs-resource-is-already-configured-to-run","errorCode":null,"errorMessage":"The Azure Event Hubs resource is already configured to run as an emulator.","messagePattern":"The Azure Event Hubs resource is already configured to run as an emulator\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs","lineNumber":258,"sourceCode":"    ///\n    /// var eventHub = builder.AddAzureEventHubs(\"eventhubns\")\n    ///    .RunAsEmulator()\n    ///    .AddEventHub(\"hub\");\n    ///\n    /// builder.AddProject&lt;Projects.InventoryService&gt;()\n    ///        .WithReference(eventHub);\n    ///\n    /// builder.Build().Run();\n    /// </code>\n    /// </example>\n    [AspireExport(RunSyncOnBackgroundThread = true)]\n    public static IResourceBuilder<AzureEventHubsResource> RunAsEmulator(this IResourceBuilder<AzureEventHubsResource> builder, Action<IResourceBuilder<AzureEventHubsEmulatorResource>>? configureContainer = null)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n\n        if (builder.Resource.IsEmulator)\n        {\n            throw new InvalidOperationException(\"The Azure Event Hubs resource is already configured to run as an emulator.\");\n        }\n\n        if (builder.ApplicationBuilder.ExecutionContext.IsPublishMode)\n        {\n            return builder;\n        }\n\n        // Mark this resource as an emulator for consistent resource identification and tooling support\n        builder.WithAnnotation(new EmulatorResourceAnnotation());\n\n        builder\n            .WithEndpoint(name: \"emulator\", targetPort: 5672)\n            .WithHttpEndpoint(name: EmulatorHealthEndpointName, targetPort: 5300)\n            .WithEndpoint(EmulatorHealthEndpointName, e => e.ExcludeReferenceEndpoint = true)\n            .WithHttpHealthCheck(endpointName: EmulatorHealthEndpointName, path: \"/health\")\n            .WithAnnotation(new ContainerImageAnnotation\n            {\n                Registry = EventHubsEmulatorContainerImageTags.Registry,","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs#L240-L276","documentation":"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.","triggerScenarios":"Calling RunAsEmulator twice on the same AzureEventHubsResource builder, e.g. once in shared setup code and again in app-specific configuration.","commonSituations":"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.","solutions":["Remove the duplicate RunAsEmulator call and keep a single invocation","Check builder.Resource.IsEmulator before calling if the call may come from multiple code paths","Consolidate emulator configuration into one shared extension/helper"],"exampleFix":"// before\nvar cosmos = builder.AddAzureEventHubs(\"eh\").RunAsEmulator();\nif (useDevMode)\n{\n    cosmos.RunAsEmulator(c => c.WithLifetime(ContainerLifetime.Persistent)); // throws\n}\n// after\nvar cosmos = builder.AddAzureEventHubs(\"eh\");\nif (useDevMode)\n{\n    cosmos.RunAsEmulator(c => c.WithLifetime(ContainerLifetime.Persistent));\n}","handlingStrategy":"validation","validationCode":"if (!builder.Resource.IsEmulator)\n{\n    builder.RunAsEmulator();\n}","typeGuard":null,"tryCatchPattern":"try { builder.RunAsEmulator(configureContainer); }\ncatch (InvalidOperationException) { /* already an emulator — skip */ }","preventionTips":["Centralize RunAsEmulator calls in one helper","Check IsEmulator before configuring emulator options","Avoid calling RunAsEmulator in multiple conditional branches"],"tags":["eventhubs","emulator","duplicate-configuration"],"backgroundTag":"invalid-state-transition","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}