{"record":{"id":"6182022f78d33791","repo":"microsoft/aspire","slug":"the-azure-service-bus-resource-is-already-configured-to-run","errorCode":null,"errorMessage":"The Azure Service Bus resource is already configured to run as an emulator.","messagePattern":"The Azure Service Bus resource is already configured to run as an emulator\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs","lineNumber":402,"sourceCode":"    ///    .RunAsEmulator()\n    ///    .AddQueue(\"queue\");\n    ///\n    /// builder.AddProject&lt;Projects.InventoryService&gt;()\n    ///        .WithReference(serviceBus);\n    ///\n    /// builder.Build().Run();\n    /// </code>\n    /// </example>\n    /// </remarks>\n    /// <ats-remarks />\n    [AspireExport(RunSyncOnBackgroundThread = true)]\n    public static IResourceBuilder<AzureServiceBusResource> RunAsEmulator(this IResourceBuilder<AzureServiceBusResource> builder, Action<IResourceBuilder<AzureServiceBusEmulatorResource>>? configureContainer = null)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n\n        if (builder.Resource.IsEmulator)\n        {\n            throw new InvalidOperationException(\"The Azure Service Bus 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        // Add emulator container\n\n        // 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\n        var passwordParameter = ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder.ApplicationBuilder, $\"{builder.Resource.Name}-sql-pwd\", minLower: 1, minUpper: 1, minNumeric: 1);\n\n        builder\n            .WithEndpoint(name: \"emulator\", targetPort: 5672)\n            .WithHttpEndpoint(name: EmulatorHealthEndpointName, targetPort: 5300)","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusExtensions.cs#L384-L420","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Remove the duplicate RunAsEmulator call so it is invoked exactly once per resource.","If multiple helpers may configure the emulator, check builder.Resource.IsEmulator before calling RunAsEmulator.","Consolidate emulator configuration into a single extension method in your AppHost."],"exampleFix":"// before\nvar sb = builder.AddAzureServiceBus(\"sb\").RunAsEmulator().RunAsEmulator();\n// after\nvar sb = builder.AddAzureServiceBus(\"sb\");\nif (!sb.Resource.IsEmulator) { sb.RunAsEmulator(); }","handlingStrategy":"validation","validationCode":"if (!builder.Resource.IsEmulator)\n{\n    builder.RunAsEmulator();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call RunAsEmulator exactly once per resource; centralize it in one extension/helper.","Check builder.Resource.IsEmulator before configuring emulator mode.","Search your AppHost for duplicate RunAsEmulator calls when composing helpers."],"tags":["azure","servicebus","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"}