{"record":{"id":"6c3a3bbca39d2ff9","repo":"microsoft/aspire","slug":"argumentnullexception-hostbuilder-aspireredisclientbuilder","errorCode":null,"errorMessage":"ArgumentNullException: hostBuilder","messagePattern":"ArgumentNullException: hostBuilder","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Components/Aspire.StackExchange.Redis/AspireRedisClientBuilder.cs","lineNumber":20,"sourceCode":"// The .NET Foundation licenses this file to you under the MIT license.\n\nusing Microsoft.Extensions.Hosting;\nusing StackExchange.Redis;\n\nnamespace Aspire.StackExchange.Redis;\n\n/// <summary>\n/// Provides a builder for configuring Redis client services using StackExchange.Redis in an Aspire application.\n/// </summary>\n/// <param name=\"hostBuilder\">The <see cref=\"IHostApplicationBuilder\"/> with which services are being registered.</param>\n/// <param name=\"settings\">The <see cref=\"StackExchangeRedisSettings\"/> to configure the Redis client.</param>\n/// <param name=\"serviceKey\">The service key used to register the <see cref=\"IConnectionMultiplexer\"/> service, if any.</param>\npublic sealed class AspireRedisClientBuilder(IHostApplicationBuilder hostBuilder, StackExchangeRedisSettings settings, string? serviceKey)\n{\n    /// <summary>\n    /// Gets the <see cref=\"IHostApplicationBuilder\"/> with which services are being registered.\n    /// </summary>\n    public IHostApplicationBuilder HostBuilder { get; } = hostBuilder ?? throw new ArgumentNullException(nameof(hostBuilder));\n\n    /// <summary>\n    /// Gets the <see cref=\"StackExchangeRedisSettings\"/> used to configure the Redis client.\n    /// </summary>\n    public StackExchangeRedisSettings Settings { get; } = settings;\n\n    /// <summary>\n    /// Gets the service key used to register the <see cref=\"IConnectionMultiplexer\"/> service, if any.\n    /// </summary>\n    public string? ServiceKey { get; } = serviceKey;\n}\n","sourceCodeStart":2,"sourceCodeEnd":32,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Components/Aspire.StackExchange.Redis/AspireRedisClientBuilder.cs#L2-L32","documentation":"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.","triggerScenarios":"Directly constructing new AspireRedisClientBuilder(null, settings, serviceKey), e.g. in unit tests or custom factory code, instead of obtaining the builder via AddRedisClient/AddKeyedRedisClient.","commonSituations":"Unit tests instantiating the builder with fake/null host builders, refactoring that loses the builder instance before construction, hand-rolled DI composition roots.","solutions":["Pass a valid IHostApplicationBuilder (e.g. builder from WebApplication.CreateBuilder / Host.CreateApplicationBuilder).","Prefer the public AddRedisClient extension over constructing AspireRedisClientBuilder manually.","In tests, use a real HostApplicationBuilder rather than null."],"exampleFix":"// before\nvar redisBuilder = new AspireRedisClientBuilder(null!, redisSettings, \"redis\");\n\n// after\nvar appBuilder = Host.CreateApplicationBuilder();\nvar redisBuilder = new AspireRedisClientBuilder(appBuilder, redisSettings, \"redis\");","handlingStrategy":"type-guard","validationCode":"if (hostBuilder is null) throw new ArgumentNullException(nameof(hostBuilder)); // before constructing","typeGuard":"static bool IsValidRedisBuilderInput(IHostApplicationBuilder? hb, StackExchangeRedisSettings? s) => hb is not null && s is not null;","tryCatchPattern":"try { var b = new AspireRedisClientBuilder(hostBuilder, settings, key); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"hostBuilder\")\n{ /* developer bug: fix caller, do not retry */ }","preventionTips":["Do not construct AspireRedisClientBuilder manually; use AddRedisClient extensions.","Use non-nullable IHostApplicationBuilder parameters so the compiler forces a valid value.","In tests, build a real HostApplicationBuilder fixture instead of passing null."],"tags":["redis","argument-null","dependency-injection","programming-error"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}