microsoft/semantic-kernel · error · InvalidOperationException

AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.

Error message

AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.

What it means

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.

Source

Thrown at dotnet/samples/AgentFrameworkMigration/AzureAIFoundry/Step03_DependencyInjection/Program.cs:14

// Copyright (c) Microsoft. All rights reserved.

using Azure.AI.Agents.Persistent;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.AzureAI;

#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.

var azureEndpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
var deploymentName = System.Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o";
var userInput = "Tell me a joke about a pirate.";

Console.WriteLine($"User Input: {userInput}");

await SKAgentAsync();
await SKAgent_As_AFAgentAsync();
await AFAgentAsync();

async Task SKAgentAsync()
{
    Console.WriteLine("\n=== SK Agent ===\n");

    var serviceCollection = new ServiceCollection();
    serviceCollection.AddSingleton((sp) => AzureAIAgent.CreateAgentsClient(azureEndpoint, new AzureCliCredential()));
    serviceCollection.AddTransient<AzureAIAgent>((sp) =>
    {
        var azureAgentClient = sp.GetRequiredService<PersistentAgentsClient>();

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Set AZURE_FOUNDRY_PROJECT_ENDPOINT to your Azure AI Foundry project URL
  2. Optionally set AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME (defaults to gpt-4o)
  3. Consider persisting the variable in your shell profile (.bashrc, .zshrc) or system environment
  4. Verify the endpoint resolves to a valid Foundry project with a deployed model

Example fix

// before
var azureEndpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");

// after
var azureEndpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT");
if (string.IsNullOrWhiteSpace(azureEndpoint))
{
    Console.Error.WriteLine("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
    Console.Error.WriteLine("Example: https://<project>-resource.services.ai.azure.com/api/projects/<project>");
    return;
}
Defensive patterns

Strategy: validation

Validate before calling

var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT");
if (string.IsNullOrWhiteSpace(endpoint))
{
    Console.Error.WriteLine("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
    Console.Error.WriteLine("Required for Foundry DI samples. See README.md.");
    return;
}

Type guard

static bool IsFoundryEndpointConfigured() =>
    !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT"));

Try / catch

try
{
    var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT")
        ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AZURE_FOUNDRY_PROJECT_ENDPOINT"))
{
    Console.Error.WriteLine(ex.Message);
    return;
}

Prevention

When it happens

Trigger: Running AzureAIFoundry/Step03_DependencyInjection without AZURE_FOUNDRY_PROJECT_ENDPOINT. The throw at line 14 fires before any service collection or DI container is built.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/38c025e9563bc51c. Report an issue: GitHub.