{"record":{"id":"f748053b1f6377eb","repo":"LuckyPennySoftware/MediatR","slug":"error-registering-the-generic-type-requesttype-f-f74805","errorCode":null,"errorMessage":"Error registering the generic type: {requestType.FullName}. The total number of generic type registrations exceeds the maximum allowed ({MaxGenericTypeRegistrations}).","messagePattern":"Error registering the generic type: (.+?)\\. The total number of generic type registrations exceeds the maximum allowed \\((.+?)\\)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MediatR/Registration/ServiceRegistrar.cs","lineNumber":274,"sourceCode":"        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)\n            return new List<List<Type>> { new List<Type>() };\n       \n        cancellationToken.ThrowIfCancellationRequested();\n\n        var currentList = lists[depth];\n        var childCombinations = GenerateCombinations(requestType, lists, depth + 1, cancellationToken);\n        var combinations = new List<List<Type>>();\n\n        foreach (var item in currentList)\n        {\n            foreach (var childCombination in childCombinations)\n            {\n                var currentCombination = new List<Type> { item };\n                currentCombination.AddRange(childCombination);","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Registration/ServiceRegistrar.cs#L256-L292","documentation":"Thrown by GenerateCombinations while computing the running product totalCombinations: once it exceeds MaxGenericTypeRegistrations (default 125000) the registration is aborted. The product is the cartesian product of candidate counts across parameters.","triggerScenarios":"Open generic handler with multiple parameters each closed against many candidates such that the product (e.g. 100 x 100 x 20 = 200000) exceeds 125000.","commonSituations":"Several broad marker interfaces combined in one open generic; large domain models scanned wholesale; raising MaxTypesClosing without anticipating the multiplicative effect.","solutions":["Reduce the candidate set per parameter (narrow marker interface usage, scan fewer assemblies).","Lower MaxTypesClosing so the product stays under the cap.","If the registrations are genuinely needed, raise the cap: cfg.MaxGenericTypeRegistrations = 250000; and benchmark startup memory/time.","Prefer explicit closed registrations over open-generic auto-closing for high-fan-out cases."],"exampleFix":"// before\ncfg.MaxTypesClosing = 100; // 100x100x100 = 1,000,000 -> exceeds 125000\n\n// after\ncfg.MaxTypesClosing = 40; // 40x40x40 = 64,000 < 125000\n// or\ncfg.MaxGenericTypeRegistrations = 250000;","handlingStrategy":"validation","validationCode":"long product = candidateLists.Aggregate(1L, (acc, l) => acc * l.Count);\nif (product > cfg.MaxGenericTypeRegistrations)\n    Console.WriteLine($\"Combinatorial product {product} exceeds cap {cfg.MaxGenericTypeRegistrations}\");","typeGuard":null,"tryCatchPattern":"try { services.AddMediatR(cfg => { /* ... */ }); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"total number of generic type registrations exceeds\"))\n{\n    logger.LogError(ex, \"Combinatorial registration cap exceeded\");\n    throw;\n}","preventionTips":["Estimate the cartesian product of candidate counts before scanning.","Lower MaxTypesClosing before raising MaxGenericTypeRegistrations."],"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"}