{"record":{"id":"38c025e9563bc51c","repo":"microsoft/semantic-kernel","slug":"azure-foundry-project-endpoint-is-not-set-38c025","errorCode":null,"errorMessage":"AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.","messagePattern":"AZURE_FOUNDRY_PROJECT_ENDPOINT is not set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentFrameworkMigration/AzureAIFoundry/Step03_DependencyInjection/Program.cs","lineNumber":14,"sourceCode":"﻿// Copyright (c) Microsoft. All rights reserved.\n\nusing Azure.AI.Agents.Persistent;\nusing Azure.AI.Projects;\nusing Azure.Identity;\nusing Microsoft.Agents.AI;\nusing Microsoft.Extensions.AI;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Agents.AzureAI;\n\n#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.\n\nvar azureEndpoint = Environment.GetEnvironmentVariable(\"AZURE_FOUNDRY_PROJECT_ENDPOINT\") ?? throw new InvalidOperationException(\"AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.\");\nvar deploymentName = System.Environment.GetEnvironmentVariable(\"AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME\") ?? \"gpt-4o\";\nvar userInput = \"Tell me a joke about a pirate.\";\n\nConsole.WriteLine($\"User Input: {userInput}\");\n\nawait SKAgentAsync();\nawait SKAgent_As_AFAgentAsync();\nawait AFAgentAsync();\n\nasync Task SKAgentAsync()\n{\n    Console.WriteLine(\"\\n=== SK Agent ===\\n\");\n\n    var serviceCollection = new ServiceCollection();\n    serviceCollection.AddSingleton((sp) => AzureAIAgent.CreateAgentsClient(azureEndpoint, new AzureCliCredential()));\n    serviceCollection.AddTransient<AzureAIAgent>((sp) =>\n    {\n        var azureAgentClient = sp.GetRequiredService<PersistentAgentsClient>();","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/AgentFrameworkMigration/AzureAIFoundry/Step03_DependencyInjection/Program.cs#L1-L32","documentation":"The Azure AI Foundry DependencyInjection migration sample throws InvalidOperationException when AZURE_FOUNDRY_PROJECT_ENDPOINT is missing. This sample shows how to register AzureAIAgent in a Microsoft.Extensions.DependencyInjection container, comparing SK and AF patterns. The Foundry endpoint is needed to construct the PersistentAgentsClient that the DI factory resolves.","triggerScenarios":"Running AzureAIFoundry/Step03_DependencyInjection without AZURE_FOUNDRY_PROJECT_ENDPOINT. The throw at line 14 fires before any service collection or DI container is built.","commonSituations":"Assuming DI samples use user secrets or appsettings.json (they read only environment variables); the Foundry endpoint was set for a different sample folder but not globally; running in a fresh terminal after setting the variable in a previous session that closed.","solutions":["Set AZURE_FOUNDRY_PROJECT_ENDPOINT to your Azure AI Foundry project URL","Optionally set AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME (defaults to gpt-4o)","Consider persisting the variable in your shell profile (.bashrc, .zshrc) or system environment","Verify the endpoint resolves to a valid Foundry project with a deployed model"],"exampleFix":"// before\nvar azureEndpoint = Environment.GetEnvironmentVariable(\"AZURE_FOUNDRY_PROJECT_ENDPOINT\") ?? throw new InvalidOperationException(\"AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.\");\n\n// after\nvar azureEndpoint = Environment.GetEnvironmentVariable(\"AZURE_FOUNDRY_PROJECT_ENDPOINT\");\nif (string.IsNullOrWhiteSpace(azureEndpoint))\n{\n    Console.Error.WriteLine(\"AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.\");\n    Console.Error.WriteLine(\"Example: https://<project>-resource.services.ai.azure.com/api/projects/<project>\");\n    return;\n}","handlingStrategy":"validation","validationCode":"var endpoint = Environment.GetEnvironmentVariable(\"AZURE_FOUNDRY_PROJECT_ENDPOINT\");\nif (string.IsNullOrWhiteSpace(endpoint))\n{\n    Console.Error.WriteLine(\"AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.\");\n    Console.Error.WriteLine(\"Required for Foundry DI samples. See README.md.\");\n    return;\n}","typeGuard":"static bool IsFoundryEndpointConfigured() =>\n    !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"AZURE_FOUNDRY_PROJECT_ENDPOINT\"));","tryCatchPattern":"try\n{\n    var endpoint = Environment.GetEnvironmentVariable(\"AZURE_FOUNDRY_PROJECT_ENDPOINT\")\n        ?? throw new InvalidOperationException(\"AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AZURE_FOUNDRY_PROJECT_ENDPOINT\"))\n{\n    Console.Error.WriteLine(ex.Message);\n    return;\n}","preventionTips":["DI samples read env vars directly — do not assume IConfiguration or user secrets are used","Set the Foundry endpoint in your shell profile for persistence across sessions","Add env vars to launchSettings.json for VS-based debugging","Validate all required env vars in a guard at the top of Program.cs before building the DI container"],"tags":["configuration","csharp","azure-foundry","semantic-kernel","environment-variables","dependency-injection","persistent-agents"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}