microsoft/semantic-kernel · critical · InvalidOperationException

Configuration is not setup correctly.

Error message

Configuration is not setup correctly.

What it means

The Booking Restaurant sample builds an AppConfig from user secrets plus environment variables and uses a null-coalescing throw when Get<AppConfig>() returns null. This means no configuration source could bind the required shape at all. The exception fires before Validate() runs, so it is a 'config entirely missing' failure rather than a field-level one.

Source

Thrown at dotnet/samples/Demos/BookingRestaurant/Program.cs:22

using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Plugins;

// Use this for application permissions
string[] scopes;

var config = new ConfigurationBuilder()
    .AddUserSecrets<Program>()
    .AddEnvironmentVariables()
    .Build()
    .Get<AppConfig>() ??
    throw new InvalidOperationException("Configuration is not setup correctly.");

config.Validate();

TokenCredential credential = null!;
if (config.AzureEntraId!.InteractiveBrowserAuthentication) // Authentication As User
{
    /// Use this if using user delegated permissions
    scopes = ["User.Read", "BookingsAppointment.ReadWrite.All"];

    credential = new InteractiveBrowserCredential(
        new InteractiveBrowserCredentialOptions
        {
            TenantId = config.AzureEntraId.TenantId,
            ClientId = config.AzureEntraId.ClientId,
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
            RedirectUri = new Uri(config.AzureEntraId.InteractiveBrowserRedirectUri!)
        });
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Run `dotnet user-secrets init` then `dotnet user-secrets set <Key> <Value>` for each AppConfig property per the README.
  2. Alternatively export the equivalent environment variables (hierarchical keys use __ as separator).
  3. Verify the user-secrets ID in the .csproj matches the assembly so secrets resolve, then re-run.
  4. Confirm AppConfig.Validate() passes once Get() is non-null; this throw only covers the null case.

Example fix

// before
.Get<AppConfig>() ??
    throw new InvalidOperationException("Configuration is not setup correctly.");

// after (diagnostic: keep throw, but surface what is missing)
var raw = new ConfigurationBuilder()
    .AddUserSecrets<Program>()
    .AddEnvironmentVariables()
    .Build();
var config = raw.Get<AppConfig>() ??
    throw new InvalidOperationException(
        "Configuration is not setup correctly. Missing keys: " +
        string.Join(", ", typeof(AppConfig).GetProperties().Select(p => p.Name)));
Defensive patterns

Strategy: validation

Validate before calling

var raw = new ConfigurationBuilder().AddUserSecrets<Program>().AddEnvironmentVariables().Build();
var missing = typeof(AppConfig).GetProperties()
    .Where(p => raw[typeof(AppConfig).Name + ":" + p.Name] is null)
    .Select(p => p.Name).ToList();
if (missing.Any())
    throw new InvalidOperationException("Missing config keys: " + string.Join(", ", missing));

Type guard

static bool IsConfigPresent(IConfigurationRoot root) => root.Get<AppConfig>() is not null;

Try / catch

try { config.Validate(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Configuration is not setup"))
{
    Console.Error.WriteLine("Run: dotnet user-secrets set <Key> <Value> for each AppConfig property.");
    return;
}

Prevention

When it happens

Trigger: No user secrets are registered for the project (missing `dotnet user-secrets` init/set) AND no matching environment variables are present, so ConfigurationBuilder.Get<AppConfig>() returns null.

Common situations: First run of the sample on a machine without secrets, cloning the repo without running the README setup, wrong secrets GUID (user secrets bound to a different assembly), or renamed config keys that no longer bind.

Related errors


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