{"record":{"id":"a0a83c4d7ccfe85f","repo":"microsoft/aspire","slug":"azure-ai-search-tool-tool-resource-name-already-has-a","errorCode":null,"errorMessage":"Azure AI Search tool '{tool.Resource.Name}' already has a backing resource configured.","messagePattern":"Azure AI Search tool '(.+?)' already has a backing resource configured\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/PromptAgent/PromptAgentBuilderExtensions.cs","lineNumber":338,"sourceCode":"    /// Links an Azure AI Search tool to a backing <see cref=\"AzureSearchResource\"/>,\n    /// creating the necessary Foundry project connection and role assignments.\n    /// </summary>\n    /// <ats-summary>Links an Azure AI Search tool to a backing search resource.</ats-summary>\n    /// <param name=\"tool\">The Azure AI Search tool resource builder.</param>\n    /// <param name=\"search\">The Azure AI Search resource to use for grounding.</param>\n    /// <returns>A reference to the <see cref=\"IResourceBuilder{T}\"/> for chaining.</returns>\n    /// <ats-returns>The resource builder.</ats-returns>\n    [AspireExport]\n    public static IResourceBuilder<AzureAISearchToolResource> WithReference(\n        this IResourceBuilder<AzureAISearchToolResource> tool,\n        IResourceBuilder<AzureSearchResource> search)\n    {\n        ArgumentNullException.ThrowIfNull(tool);\n        ArgumentNullException.ThrowIfNull(search);\n\n        if (tool.Resource.Connection is not null)\n        {\n            throw new InvalidOperationException(\n                $\"Azure AI Search tool '{tool.Resource.Name}' already has a backing resource configured.\");\n        }\n\n        // Find the project builder to create the connection\n        var projectBuilder = tool.ApplicationBuilder.CreateResourceBuilder(tool.Resource.Project);\n\n        // AddConnection(IResourceBuilder<AzureSearchResource>) already handles role assignments\n        var connection = projectBuilder.AddConnection(search);\n\n        tool.Resource.Connection = connection.Resource;\n        tool.Resource.SearchResource = search.Resource;\n        return tool;\n    }\n\n    /// <summary>\n    /// Adds a Bing Grounding tool to a Microsoft Foundry project, enabling agents to\n    /// ground their responses using Bing Search results.\n    /// </summary>","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/PromptAgent/PromptAgentBuilderExtensions.cs#L320-L356","documentation":"An Azure AI Search tool in a Foundry prompt agent can have exactly one backing resource/connection. WithReference throws when the tool's Connection is already set, preventing a second WithReference call from silently overwriting the backing search resource.","triggerScenarios":"Calling WithReference(search) twice on the same Azure AI Search tool builder, or calling it after another overload already assigned tool.Resource.Connection.","commonSituations":"Merging sample code that wires the same tool twice; conditional wiring that can run more than once; copying WithReference lines from another tool builder onto an already-configured tool.","solutions":["Remove the duplicate WithReference call so the search tool is backed by exactly one Azure AI Search resource.","If you intend to change the backing resource, recreate the tool with the desired resource instead of re-wiring.","Guard conditional wiring so it executes only once per tool."],"exampleFix":"// before\nsearchTool.WithReference(searchA);\nsearchTool.WithReference(searchB);\n// after\nsearchTool.WithReference(searchB);","handlingStrategy":"validation","validationCode":"if (searchToolBuilder.Resource.Connection is not null) { /* already configured; skip or throw */ }","typeGuard":"bool isUnconfigured(FoundryToolResource t) => t.Connection is null;","tryCatchPattern":null,"preventionTips":["Configure each tool exactly once in a single place.","Never duplicate WithReference lines when merging samples.","Make helper wiring methods idempotent."],"tags":["azure","foundry","ai-search","duplicate-configuration","tool"],"backgroundTag":"conflicting-config-options","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}