LuckyPennySoftware/MediatR · error · ArgumentNullException
Open behavior type can not be null.
Error message
Open behavior type can not be null.
What it means
OpenBehavior is a MediatR registration entity that wraps a Type flagged as an open-generic IPipelineBehavior<,>. Its constructor validation rejects a null type with ArgumentNullException before any reflection occurs, so registration fails fast with a clear cause rather than NullReferenceException inside GetInterfaces.
Source
Thrown at src/MediatR/Entities/OpenBehavior.cs:43
/// <summary>
/// The type of the open behavior.
/// </summary>
public Type OpenBehaviorType { get; }
/// <summary>
/// The service lifetime of the open behavior.
/// </summary>
public ServiceLifetime ServiceLifetime { get; }
/// <summary>
/// Validates whether the specified type implements the <see cref="IPipelineBehavior{TRequest, TResponse}"/> interface.
/// </summary>
/// <param name="openBehaviorType">The type to validate.</param>
/// <exception cref="InvalidOperationException">Thrown if the type does not implement <see cref="IPipelineBehavior{TRequest, TResponse}"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="openBehaviorType"/> is null.</exception>
private static void ValidatePipelineBehaviorType(Type openBehaviorType)
{
if (openBehaviorType == null) throw new ArgumentNullException($"Open behavior type can not be null.");
bool isPipelineBehavior = openBehaviorType.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPipelineBehavior<,>));
if (!isPipelineBehavior)
{
throw new InvalidOperationException($"The type \"{openBehaviorType.Name}\" must implement IPipelineBehavior<,> interface.");
}
}
}View on GitHub (pinned to 916ef1b3d6)
Solutions
- Confirm the Type argument you pass to AddOpenBehavior / the OpenBehavior constructor is non-null.
- If you build the Type from a string via Type.GetType, validate the result is non-null before registering (a null return means the assembly-qualified name could not be resolved).
- Register the behavior by typeof(...) directly to avoid nulls from runtime type resolution.
Example fix
// before
var t = Type.GetType(config.BehaviorTypeName); // null if unresolvable
configuration.AddOpenBehavior(t);
// after
var t = Type.GetType(config.BehaviorTypeName)
?? throw new InvalidOperationException($"Cannot resolve {config.BehaviorTypeName}");
configuration.AddOpenBehavior(t); Defensive patterns
Strategy: validation
Validate before calling
if (openBehaviorType is null)
throw new ArgumentNullException(nameof(openBehaviorType), "Cannot register a null open behavior type."); Type guard
static bool IsValidOpenBehaviorType(Type? t) => t is not null;
Prevention
- Register behaviors via typeof(...) literals, never runtime Type.GetType that can return null.
- When resolving types by name, fail loudly on null before handing the type to MediatR.
When it happens
Trigger: Constructing `new OpenBehavior(null, serviceLifetime)` — i.e. the AddOpenBehavior plumbing passed a null Type through, typically because the caller passed null as the open behavior type.
Common situations: Calling `configuration.AddOpenBehavior(null)` or a DI/registration helper that resolves the type dynamically and returns null (e.g. Type.GetType returning null for an unresolvable assembly-qualified name).
Related errors
- The type "{openBehaviorType.Name}" must implement IPipelineB
- {openBehaviorType.Name} must implement {typeof(IPipelineBeha
- {openBehaviorType.Name} must be generic
- {implementationType.Name} must implement {typeof(IPipelineBe
- Could not create wrapper type for {requestType}
AI-assisted analysis of LuckyPennySoftware/MediatR@916ef1b3d6 (2026-08-13).
Data as JSON: /api/errors/801ba1a6d7aee12f.
Report an issue: GitHub.