microsoft/aspire · error · ArgumentException

Array params contains empty item

Error message

Array params contains empty item: [{values}]

What it means

DistributedApplicationFactory validates the args array passed to its constructor: an element that is the empty string is rejected with ArgumentException naming the args parameter and listing all values. Empty CLI arguments would be forwarded to the host builder and cause ambiguous parsing, so they are treated as invalid input.

Solutions

  1. Filter empty entries before constructing the factory: args.Where(a => !string.IsNullOrEmpty(a)).ToArray()
  2. Fix the upstream Split/Join logic (use RemoveEmptyEntries: StringSplitOptions.RemoveEmptyEntries)
  3. Default missing optional values to a meaningful argument or omit the argument entirely
  4. Validate the composed args in test setup before passing them to the factory

Example fix

// before
var args = input.Split(' ');
var factory = new DistributedApplicationFactory<Program>(args);
// after
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var factory = new DistributedApplicationFactory<Program>(args);
Defensive patterns

Strategy: validation

Validate before calling

static string[] ValidateArgs(string[] args) =>
    args.All(a => !string.IsNullOrEmpty(a))
        ? args
        : throw new ArgumentException("args contains empty entries", nameof(args));

Type guard

static bool HasNoEmpty(string?[] args) => Array.TrueForAll(args, a => !string.IsNullOrEmpty(a));

Try / catch

try
{
    var factory = new DistributedApplicationFactory<Program>(args);
}
catch (ArgumentException ex) when (ex.ParamName == "args")
{
    throw new InvalidOperationException("Test args must not contain empty strings", ex);
}

Prevention

When it happens

Trigger: Calling new DistributedApplicationFactory<TEntryPoint>(args) with an array containing "" — commonly produced by string.Split on missing segments, Join/Split round-trips, or trimming entries that leave empty strings.

Common situations: args.Split(' ') on an input with double spaces; env-var or config-derived arguments that are empty; test code templating args where a placeholder resolved to nothing.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs:163

    /// </summary>
    /// <param name="application">The application.</param>
    protected virtual void OnBuilt(DistributedApplication application)
    {
    }

    private static string[] ThrowIfNullOrContainsIsNullOrEmpty(string[] args)
    {
        ArgumentNullException.ThrowIfNull(args);
        foreach (var arg in args)
        {
            if (string.IsNullOrEmpty(arg))
            {
                var values = string.Join(", ", args);
                if (arg is null)
                {
                    throw new ArgumentNullException(nameof(args), $"Array params contains null item: [{values}]");
                }
                throw new ArgumentException($"Array params contains empty item: [{values}]", nameof(args));
            }
        }
        return args;
    }

    private void OnBuiltCore(DistributedApplication application)
    {
        _shutdownTimeout = application.Services.GetService<IOptions<HostOptions>>()?.Value.ShutdownTimeout ?? _shutdownTimeout;
        _appTcs.TrySetResult(application);
        OnBuilt(application);
    }

    private static void PreConfigureBuilderOptions(
        DistributedApplicationOptions applicationOptions,
        HostApplicationBuilderSettings hostBuilderOptions,
        string[] args,
        Assembly entryPointAssembly)
    {

View on GitHub (pinned to 25830f84bd)