{"record":{"id":"7821e1c701f221dc","repo":"microsoft/semantic-kernel","slug":"azure-openai-endpoint-is-not-set-7821e1","errorCode":null,"errorMessage":"AZURE_OPENAI_ENDPOINT is not set.","messagePattern":"AZURE_OPENAI_ENDPOINT is not set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/AgentFrameworkMigration/AzureOpenAI/Step03_DependencyInjection/Program.cs","lineNumber":12,"sourceCode":"﻿// Copyright (c) Microsoft. All rights reserved.\n\nusing Azure.AI.OpenAI;\nusing Azure.Identity;\nusing Microsoft.Agents.AI;\nusing Microsoft.Extensions.AI;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Agents;\nusing OpenAI.Chat;\n\nvar endpoint = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\") ?? throw new InvalidOperationException(\"AZURE_OPENAI_ENDPOINT is not set.\");\nvar deploymentName = System.Environment.GetEnvironmentVariable(\"AZURE_OPENAI_DEPLOYMENT_NAME\") ?? \"gpt-4o\";\nvar userInput = \"Tell me a joke about a pirate.\";\n\nConsole.WriteLine($\"User Input: {userInput}\");\n\nawait SKAgent();\nawait SKAgent_As_AFAgentAsync();\nawait AFAgent();\n\nasync Task SKAgent()\n{\n    Console.WriteLine(\"\\n=== SK Agent ===\\n\");\n\n    var serviceCollection = new ServiceCollection();\n    serviceCollection.AddKernel().AddAzureOpenAIChatClient(deploymentName, endpoint, new AzureCliCredential());\n    serviceCollection.AddTransient((sp) => new ChatCompletionAgent()\n    {\n        Kernel = sp.GetRequiredService<Kernel>(),","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/AgentFrameworkMigration/AzureOpenAI/Step03_DependencyInjection/Program.cs#L1-L30","documentation":"The Azure OpenAI DependencyInjection migration sample throws InvalidOperationException when AZURE_OPENAI_ENDPOINT is unset. This sample shows DI-based agent registration, comparing SK's ChatCompletionAgent (requiring a Kernel from the service container) with AF's keyed-singleton AIAgent pattern. The endpoint is needed at composition time to build the chat client registered in the container.","triggerScenarios":"Running AzureOpenAI/Step03_DependencyInjection without AZURE_OPENAI_ENDPOINT. The throw at line 12 fires before any ServiceCollection is created or agents registered.","commonSituations":"Expecting DI samples to use IConfiguration or user secrets (they read env vars directly); the endpoint is in launchSettings.json of a different sample project; running from CLI where the VS launch profile env vars do not apply.","solutions":["Set AZURE_OPENAI_ENDPOINT to your Azure OpenAI resource URL","Optionally set AZURE_OPENAI_DEPLOYMENT_NAME (defaults to gpt-4o)","If running from VS, add the variable in Properties/launchSettings.json","Ensure az login is current for AzureCliCredential"],"exampleFix":"// before\nvar endpoint = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\") ?? throw new InvalidOperationException(\"AZURE_OPENAI_ENDPOINT is not set.\");\n\n// after\nvar endpoint = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\");\nif (string.IsNullOrWhiteSpace(endpoint))\n    throw new InvalidOperationException(\n        \"AZURE_OPENAI_ENDPOINT is not set. See README Environment Variables section.\");","handlingStrategy":"validation","validationCode":"var endpoint = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\");\nif (string.IsNullOrWhiteSpace(endpoint))\n{\n    Console.Error.WriteLine(\"AZURE_OPENAI_ENDPOINT is not set. Required for DI samples.\");\n    return;\n}","typeGuard":"static bool IsAzureOpenAIEndpointConfigured() =>\n    !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\"));","tryCatchPattern":"try\n{\n    var endpoint = Environment.GetEnvironmentVariable(\"AZURE_OPENAI_ENDPOINT\")\n        ?? throw new InvalidOperationException(\"AZURE_OPENAI_ENDPOINT is not set.\");\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AZURE_OPENAI_ENDPOINT\"))\n{\n    Console.Error.WriteLine(ex.Message);\n    return;\n}","preventionTips":["DI samples read env vars directly — add them to launchSettings.json for VS debugging","Do not assume IConfiguration or user secrets are consulted","Validate env vars at the top of Program.cs before building the service container","Run az login for AzureCliCredential"],"tags":["configuration","csharp","azure-openai","semantic-kernel","environment-variables","dependency-injection","agent-framework-migration"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}