{"record":{"id":"801ba1a6d7aee12f","repo":"LuckyPennySoftware/MediatR","slug":"open-behavior-type-can-not-be-null","errorCode":null,"errorMessage":"Open behavior type can not be null.","messagePattern":"Open behavior type can not be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/MediatR/Entities/OpenBehavior.cs","lineNumber":43,"sourceCode":"    /// <summary>\n    /// The type of the open behavior.\n    /// </summary>\n    public Type OpenBehaviorType { get; }\n\n    /// <summary>\n    /// The service lifetime of the open behavior.\n    /// </summary>\n    public ServiceLifetime ServiceLifetime { get; }\n\n    /// <summary>\n    /// Validates whether the specified type implements the <see cref=\"IPipelineBehavior{TRequest, TResponse}\"/> interface.\n    /// </summary>\n    /// <param name=\"openBehaviorType\">The type to validate.</param>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the type does not implement <see cref=\"IPipelineBehavior{TRequest, TResponse}\"/>.</exception>\n    /// <exception cref=\"ArgumentNullException\">Thrown if <paramref name=\"openBehaviorType\"/> is null.</exception>\n    private static void ValidatePipelineBehaviorType(Type openBehaviorType)\n    {\n        if (openBehaviorType == null) throw new ArgumentNullException($\"Open behavior type can not be null.\");\n\n        bool isPipelineBehavior = openBehaviorType.GetInterfaces()\n            .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPipelineBehavior<,>));\n\n        if (!isPipelineBehavior)\n        {\n            throw new InvalidOperationException($\"The type \\\"{openBehaviorType.Name}\\\" must implement IPipelineBehavior<,> interface.\");\n        }\n    }\n}","sourceCodeStart":25,"sourceCodeEnd":53,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Entities/OpenBehavior.cs#L25-L53","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nvar t = Type.GetType(config.BehaviorTypeName); // null if unresolvable\nconfiguration.AddOpenBehavior(t);\n// after\nvar t = Type.GetType(config.BehaviorTypeName)\n    ?? throw new InvalidOperationException($\"Cannot resolve {config.BehaviorTypeName}\");\nconfiguration.AddOpenBehavior(t);","handlingStrategy":"validation","validationCode":"if (openBehaviorType is null)\n    throw new ArgumentNullException(nameof(openBehaviorType), \"Cannot register a null open behavior type.\");","typeGuard":"static bool IsValidOpenBehaviorType(Type? t) => t is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["pipeline-behavior","open-behavior","argumentnull","registration","reflection"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}