{"record":{"id":"3f386917a2aa1aee","repo":"LuckyPennySoftware/MediatR","slug":"openbehaviortype-name-must-be-generic","errorCode":null,"errorMessage":"{openBehaviorType.Name} must be generic","messagePattern":"(.+?) must be generic","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs","lineNumber":212,"sourceCode":"    /// <returns>This</returns>\r\n    public MediatRServiceConfiguration AddBehavior(Type serviceType, Type implementationType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)\r\n    {\r\n        BehaviorsToRegister.Add(new ServiceDescriptor(serviceType, implementationType, serviceLifetime));\r\n\r\n        return this;\r\n    }\r\n\r\n    /// <summary>\r\n    /// Registers an open behavior type against the <see cref=\"IPipelineBehavior{TRequest,TResponse}\"/> open generic interface type\r\n    /// </summary>\r\n    /// <param name=\"openBehaviorType\">An open generic behavior type</param>\r\n    /// <param name=\"serviceLifetime\">Optional service lifetime, defaults to <see cref=\"ServiceLifetime.Transient\"/>.</param>\r\n    /// <returns>This</returns>\r\n    public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)\r\n    {\r\n        if (!openBehaviorType.IsGenericType)\r\n        {\r\n            throw new InvalidOperationException($\"{openBehaviorType.Name} must be generic\");\r\n        }\r\n\r\n        var implementedGenericInterfaces = openBehaviorType.GetInterfaces().Where(i => i.IsGenericType).Select(i => i.GetGenericTypeDefinition());\r\n        var implementedOpenBehaviorInterfaces = new HashSet<Type>(implementedGenericInterfaces.Where(i => i == typeof(IPipelineBehavior<,>)));\r\n\r\n        if (implementedOpenBehaviorInterfaces.Count == 0)\r\n        {\r\n            throw new InvalidOperationException($\"{openBehaviorType.Name} must implement {typeof(IPipelineBehavior<,>).FullName}\");\r\n        }\r\n\r\n        foreach (var openBehaviorInterface in implementedOpenBehaviorInterfaces)\r\n        {\r\n            BehaviorsToRegister.Add(new ServiceDescriptor(openBehaviorInterface, openBehaviorType, serviceLifetime));\r\n        }\r\n\r\n        return this;\r\n    }\r\n\r","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs#L194-L230","documentation":"AddOpenBehavior requires an open-generic type so the DI container can close it per request. The first guard checks openBehaviorType.IsGenericType and throws InvalidOperationException if false, failing fast before reflection over generic-interface definitions would be meaningless.","triggerScenarios":"Calling AddOpenBehavior(typeof(SomeClosedBehavior)) or AddOpenBehavior(typeof(NonGenericClass)) — any Type whose IsGenericType is false.","commonSituations":"Passing a closed behavior to AddOpenBehavior by mistake (should use AddBehavior); passing a concrete non-generic class; passing an unbound generic that was prematurely closed.","solutions":["If the behavior is closed, use AddBehavior(typeof(ClosedBehavior)).","If it is genuinely open-generic, pass the open form typeof(Foo<,>) (with unbound type parameters).","Double-check the type has unbound generic arguments before registering."],"exampleFix":"// before\nconfiguration.AddOpenBehavior(typeof(ValidationBehavior<MyRequest, MyResponse>)); // closed\n// after\nconfiguration.AddOpenBehavior(typeof(ValidationBehavior<,>));              // open\n// or for closed:\nconfiguration.AddBehavior(typeof(ValidationBehavior<MyRequest, MyResponse>));","handlingStrategy":"validation","validationCode":"if (!openBehaviorType.IsGenericType)\n    throw new InvalidOperationException($\"{openBehaviorType.Name} is not generic; use AddBehavior for closed behaviors.\");","typeGuard":"static bool IsOpenGeneric(Type t) => t.IsGenericType && t.ContainsGenericParameters;","tryCatchPattern":null,"preventionTips":["Pass the open form typeof(Foo<,>) to AddOpenBehavior.","Use AddBehavior for closed behaviors.","Verify ContainsGenericParameters is true before registering an open behavior."],"tags":["pipeline-behavior","open-behavior","registration","di","generics"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}