microsoft/aspire · error · ArgumentNullException

Value cannot be null. (Parameter 'sourcePath')

Error message

Value cannot be null. (Parameter 'sourcePath')

What it means

ConfigFileAnnotation records the source path of a custom emulator config file and rejects a null path in its constructor. A null SourcePath would produce an invalid container file mount later, so it fails fast at annotation creation.

Solutions

  1. Ensure the path passed to WithConfigFile/ConfigFileAnnotation is a resolved, non-null string
  2. Validate that the config file exists before annotating the resource
  3. Fix the upstream code that produced a null path (e.g. failed configuration lookup)

Example fix

// before
string? configPath = ResolveConfigPath();
emulator.WithConfigFile(configPath!); // NRE risk / ArgumentNullException here
// after
var configPath = ResolveConfigPath();
if (configPath is not null && File.Exists(configPath))
{
    emulator.WithConfigFile(configPath);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (string.IsNullOrEmpty(configFilePath)) throw new ArgumentException("Config file path must be provided.");

Type guard

static bool IsValidConfigPath(string? path) => !string.IsNullOrWhiteSpace(path) && File.Exists(path);

Try / catch

try { emulator.WithConfigFile(configPath); }
catch (ArgumentNullException ex) when (ex.ParamName == "sourcePath") { /* resolve the config path first */ }

Prevention

When it happens

Trigger: Calling the ConfigFileAnnotation constructor with a null string — typically via the RunAsEmulator WithConfigFile extension where the config file path argument is null.

Common situations: Passing a nullable path variable that was never resolved (config lookup failed); test code constructing the annotation directly; refactoring that changed the path to nullable.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/84e513a06a5f8b98. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.EventHubs/ConfigFileAnnotation.cs:15

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting.Azure.EventHubs;

/// <summary>
/// Represents an annotation for a custom config file source.
/// </summary>
internal sealed class ConfigFileAnnotation : IResourceAnnotation
{
    public ConfigFileAnnotation(string sourcePath)
    {
        SourcePath = sourcePath ?? throw new ArgumentNullException(nameof(sourcePath));
    }

    public string SourcePath { get; }
}

View on GitHub (pinned to 25830f84bd)