{"record":{"id":"b1eafdc4c3eae589","repo":"LuckyPennySoftware/MediatR","slug":"the-type-openbehaviortype-name-must-implement","errorCode":null,"errorMessage":"The type \"{openBehaviorType.Name}\" must implement IPipelineBehavior<,> interface.","messagePattern":"The type \"(.+?)\" must implement IPipelineBehavior<,> interface\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MediatR/Entities/OpenBehavior.cs","lineNumber":50,"sourceCode":"    /// </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":32,"sourceCodeEnd":53,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Entities/OpenBehavior.cs#L32-L53","documentation":"OpenBehavior's ValidatePipelineBehaviorType uses reflection to confirm the supplied Type implements the open-generic IPipelineBehavior<,> interface. If no implemented interface matches the IPipelineBehavior<,> generic definition, it throws InvalidOperationException so an invalid behavior never silently registers.","triggerScenarios":"Constructing OpenBehavior with a Type that is not generic and/or does not close IPipelineBehavior<TRequest,TResponse> (e.g. a plain class, an interface implementor of a different contract, or a closed non-behavior class).","commonSituations":"Registering a request pre/post-processor or a stream pipeline behavior by mistake against AddOpenBehavior; passing an open generic that closes IStreamPipelineBehavior<,> instead of IPipelineBehavior<,>; passing a concrete non-generic class.","solutions":["Ensure the type implements IPipelineBehavior<TRequest, TResponse> (use AddStreamBehavior / IStreamPipelineBehavior for stream pipelines).","If the type is closed (not open generic), use AddBehavior instead of AddOpenBehavior.","Register via typeof(YourBehavior<,>) for open-generic or typeof(YourBehavior<TReq,TResp>) for closed."],"exampleFix":"// before\n// StreamBehavior<TReq,TResp> implements IStreamPipelineBehavior<,>, not IPipelineBehavior<,>\nconfiguration.AddOpenBehavior(typeof(StreamBehavior<,>));\n// after\nconfiguration.AddOpenStreamBehavior(typeof(StreamBehavior<,>));","handlingStrategy":"validation","validationCode":"bool isPipelineBehavior = openBehaviorType!.GetInterfaces()\n    .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPipelineBehavior<,>));\nif (!isPipelineBehavior)\n    throw new InvalidOperationException($\"{openBehaviorType.Name} is not an IPipelineBehavior<,>.\");","typeGuard":"static bool IsOpenPipelineBehavior(Type t) =>\n    t.IsGenericType && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPipelineBehavior<,>));","tryCatchPattern":null,"preventionTips":["Use AddBehavior for closed behaviors and AddOpenBehavior for open ones.","Keep stream pipelines on IStreamPipelineBehavior / AddStreamBehavior."],"tags":["pipeline-behavior","open-behavior","reflection","registration","type-validation"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}