{"record":{"id":"3fd8c474a3f3111b","repo":"LuckyPennySoftware/MediatR","slug":"error-registering-the-generic-type-requesttype-f","errorCode":null,"errorMessage":"Error registering the generic type: {requestType.FullName}. The number of generic type parameters exceeds the maximum allowed ({MaxGenericTypeParameters}).","messagePattern":"Error registering the generic type: (.+?)\\. The number of generic type parameters exceeds the maximum allowed \\((.+?)\\)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MediatR/Registration/ServiceRegistrar.cs","lineNumber":260,"sourceCode":"\n        if (requestType.IsGenericParameter)\n            return null;\n\n        var requestGenericTypeDefinition = requestType.GetGenericTypeDefinition();\n              \n        var combinations = GenerateCombinations(requestType, typesThatCanCloseForEachParameter, 0, cancellationToken);\n\n        return combinations.Select(types => requestGenericTypeDefinition.MakeGenericType(types.ToArray())).ToList();\n    }\n\n    // Method to generate combinations recursively\n    public static List<List<Type>> GenerateCombinations(Type requestType, List<List<Type>> lists, int depth = 0, CancellationToken cancellationToken = default)\n    {\n        if (depth == 0)\n        {\n            // Initial checks\n            if (MaxGenericTypeParameters > 0 && lists.Count > MaxGenericTypeParameters)\n                throw new ArgumentException($\"Error registering the generic type: {requestType.FullName}. The number of generic type parameters exceeds the maximum allowed ({MaxGenericTypeParameters}).\");\n\n            foreach (var list in lists)\n            {\n                if (MaxTypesClosing > 0 && list.Count > MaxTypesClosing)\n                    throw new ArgumentException($\"Error registering the generic type: {requestType.FullName}. One of the generic type parameter's count of types that can close exceeds the maximum length allowed ({MaxTypesClosing}).\");\n            }\n\n            // Calculate the total number of combinations\n            long totalCombinations = 1;\n            foreach (var list in lists)\n            {\n                totalCombinations *= list.Count;\n                if (MaxGenericTypeParameters > 0 && totalCombinations > MaxGenericTypeRegistrations)\n                    throw new ArgumentException($\"Error registering the generic type: {requestType.FullName}. The total number of generic type registrations exceeds the maximum allowed ({MaxGenericTypeRegistrations}).\");\n            }\n        }\n\n        if (depth >= lists.Count)","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Registration/ServiceRegistrar.cs#L242-L278","documentation":"Thrown by GenerateCombinations (depth 0) when the number of generic type parameters on an open generic request type (lists.Count) exceeds MaxGenericTypeParameters (default 10). MediatR refuses to attempt the combinatorial explosion of closing an open generic handler interface against candidate types.","triggerScenarios":"An open generic request handler interface with more than 10 generic parameters being connected to concrete types during AddMediatR assembly scanning, e.g. IRequestHandler<T1,T2,...,T11> closed against many candidates each.","commonSituations":"A request interface with an unusually high arity; combining many open generic handler abstractions in one assembly; raising MaxTypesClosing without considering arity effects.","solutions":["Redesign the open generic interface to fewer type parameters (group related parameters into a single wrapper type).","If the arity is intentional and bounded, raise the cap: cfg.MaxGenericTypeParameters = 15; (increases registration cost).","Exclude that interface from automatic scanning by not registering the assembly, or split handler types into a dedicated assembly scanned with a narrower surface."],"exampleFix":"// before\npublic interface IRequestHandler<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11> { ... }\n\n// after\npublic interface IRequestHandler<TBatch> where TBatch : IRequest { ... }\ncfg.MaxGenericTypeParameters = 12; // only if arity is truly required","handlingStrategy":"validation","validationCode":"// Reject high-arity open generic handler interfaces before AddMediatR scans\nforeach (var t in assembly.GetTypes())\n{\n    foreach (var i in t.GetInterfaces().Where(i => i.IsGenericType))\n    {\n        var args = i.GetGenericArguments().Length;\n        if (args > cfg.MaxGenericTypeParameters)\n            Console.WriteLine($\"{t} closes {i.GetGenericTypeDefinition()} with {args} args\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try { services.AddMediatR(cfg => { /* ... */ }); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"exceeds the maximum allowed\"))\n{\n    logger.LogError(ex, \"Open generic arity exceeds MaxGenericTypeParameters\");\n    throw;\n}","preventionTips":["Keep open generic handler interfaces to a small arity.","Group related type parameters into a single wrapper type."],"tags":["di-registration","open-generic","combinatorics","configuration"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}