microsoft/aspire · error · ArgumentNullException

ArgumentNullException: hostBuilder

Error message

ArgumentNullException: hostBuilder

What it means

The AspireRedisClientBuilder constructor validates its IHostApplicationBuilder argument and throws ArgumentNullException when null. This is a programming-error guard: the builder cannot function without a host builder to register services into.

Solutions

  1. Pass a valid IHostApplicationBuilder (e.g. builder from WebApplication.CreateBuilder / Host.CreateApplicationBuilder).
  2. Prefer the public AddRedisClient extension over constructing AspireRedisClientBuilder manually.
  3. In tests, use a real HostApplicationBuilder rather than null.

Example fix

// before
var redisBuilder = new AspireRedisClientBuilder(null!, redisSettings, "redis");

// after
var appBuilder = Host.CreateApplicationBuilder();
var redisBuilder = new AspireRedisClientBuilder(appBuilder, redisSettings, "redis");
Defensive patterns

Strategy: type-guard

Validate before calling

if (hostBuilder is null) throw new ArgumentNullException(nameof(hostBuilder)); // before constructing

Type guard

static bool IsValidRedisBuilderInput(IHostApplicationBuilder? hb, StackExchangeRedisSettings? s) => hb is not null && s is not null;

Try / catch

try { var b = new AspireRedisClientBuilder(hostBuilder, settings, key); }
catch (ArgumentNullException ex) when (ex.ParamName == "hostBuilder")
{ /* developer bug: fix caller, do not retry */ }

Prevention

When it happens

Trigger: Directly constructing new AspireRedisClientBuilder(null, settings, serviceKey), e.g. in unit tests or custom factory code, instead of obtaining the builder via AddRedisClient/AddKeyedRedisClient.

Common situations: Unit tests instantiating the builder with fake/null host builders, refactoring that loses the builder instance before construction, hand-rolled DI composition roots.

Related errors


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

Appendix: source

Thrown at src/Components/Aspire.StackExchange.Redis/AspireRedisClientBuilder.cs:20

// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;
using StackExchange.Redis;

namespace Aspire.StackExchange.Redis;

/// <summary>
/// Provides a builder for configuring Redis client services using StackExchange.Redis in an Aspire application.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostApplicationBuilder"/> with which services are being registered.</param>
/// <param name="settings">The <see cref="StackExchangeRedisSettings"/> to configure the Redis client.</param>
/// <param name="serviceKey">The service key used to register the <see cref="IConnectionMultiplexer"/> service, if any.</param>
public sealed class AspireRedisClientBuilder(IHostApplicationBuilder hostBuilder, StackExchangeRedisSettings settings, string? serviceKey)
{
    /// <summary>
    /// Gets the <see cref="IHostApplicationBuilder"/> with which services are being registered.
    /// </summary>
    public IHostApplicationBuilder HostBuilder { get; } = hostBuilder ?? throw new ArgumentNullException(nameof(hostBuilder));

    /// <summary>
    /// Gets the <see cref="StackExchangeRedisSettings"/> used to configure the Redis client.
    /// </summary>
    public StackExchangeRedisSettings Settings { get; } = settings;

    /// <summary>
    /// Gets the service key used to register the <see cref="IConnectionMultiplexer"/> service, if any.
    /// </summary>
    public string? ServiceKey { get; } = serviceKey;
}

View on GitHub (pinned to 25830f84bd)