{"record":{"id":"b671e42c762508ad","repo":"microsoft/aspire","slug":"cannot-create-a-microsoft-foundry-project-connection-to-an","errorCode":null,"errorMessage":"Cannot create a Microsoft Foundry project connection to an emulator Cosmos DB instance.","messagePattern":"Cannot create a Microsoft Foundry project connection to an emulator Cosmos DB instance\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/Project/ConnectionBuilderExtensions.cs","lineNumber":98,"sourceCode":"            infrastructure.Add(new ProvisioningOutput(\"name\", typeof(string)) { Value = connection.Name });\n            infrastructure.Add(new ProvisioningOutput(\"id\", typeof(string)) { Value = connection.Id });\n        }\n        var connectionResource = new AzureCognitiveServicesProjectConnectionResource(name, configureInfrastructure, builder.Resource);\n        return builder.ApplicationBuilder.AddResource(connectionResource);\n    }\n\n    /// <summary>\n    /// Adds CosmosDB to a project as a connection\n    /// </summary>\n    [AspireExportIgnore(Reason = \"Raw AzureCosmosDBResource parameters are not ATS-compatible. Use the resource-builder overload instead.\")]\n    public static IResourceBuilder<AzureCognitiveServicesProjectConnectionResource> AddConnection(\n        this IResourceBuilder<AzureCognitiveServicesProjectResource> builder,\n        AzureCosmosDBResource db)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        if (db.IsEmulator())\n        {\n            throw new InvalidOperationException(\"Cannot create a Microsoft Foundry project connection to an emulator Cosmos DB instance.\");\n        }\n        return builder.AddConnection($\"connection-{Guid.NewGuid():N}\", (infra) => new AadAuthTypeConnectionProperties()\n        {\n            Category = CognitiveServicesConnectionCategory.CosmosDB,\n            Target = db.ConnectionStringOutput.AsProvisioningParameter(infra),\n            IsSharedToAll = true,\n            Metadata =\n            {\n                { \"ApiType\", \"Azure\" },\n                { \"ResourceId\", db.Id.AsProvisioningParameter(infra) }\n            }\n        });\n    }\n\n    /// <summary>\n    /// Adds CosmosDB to a project as a connection\n    /// </summary>\n    [AspireExport(\"addCosmosConnection\")]","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/Project/ConnectionBuilderExtensions.cs#L80-L116","documentation":"Microsoft Foundry project connections point at real Azure resources; the Azure Cosmos DB emulator is a local-only endpoint that Foundry cannot reach. AddConnection for Cosmos DB throws this InvalidOperationException when the supplied AzureCosmosDBResource is running as the emulator (RunAsEmulator).","triggerScenarios":"Calling foundryProject.AddConnection(cosmosDb) where cosmosDb is an AzureCosmosDBResource builder on which RunAsEmulator() was called (or the resource otherwise reports IsEmulator()).","commonSituations":"Running the app host locally with RunAsEmulator on Cosmos DB while also wiring a Foundry project connection; sharing one app model between local dev and deployed scenarios without conditioning the connection.","solutions":["Remove RunAsEmulator() from the Cosmos DB resource so a real Azure Cosmos DB account is provisioned.","Only add the Foundry connection when not in emulator mode, e.g. guard on execution context / publish mode.","Point the Foundry project at a separately declared real Cosmos DB resource instead of the emulated one."],"exampleFix":"// before\nvar cosmos = builder.AddAzureCosmosDB(\"cosmos\").RunAsEmulator();\nproject.AddConnection(cosmos); // throws\n\n// after\nvar cosmos = builder.AddAzureCosmosDB(\"cosmos\"); // no RunAsEmulator\nproject.AddConnection(cosmos);","handlingStrategy":"validation","validationCode":"if (!cosmos.IsEmulator())\n    project.AddConnection(cosmos);","typeGuard":null,"tryCatchPattern":"try { project.AddConnection(cosmos); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"emulator Cosmos DB\")) { /* skip or provision real Cosmos DB */ }","preventionTips":["Guard all Foundry connection registrations with IsEmulator() checks.","Keep emulated resources for local app consumption and separate real resources for Foundry connections.","Enable Foundry connections only in publish/deploy mode."],"tags":["azure-ai-foundry","cosmos-db","emulator","connection"],"backgroundTag":"unsupported-operation","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"}