{"record":{"id":"5a8f045476c9e2c2","repo":"LuckyPennySoftware/MediatR","slug":"error-registering-the-generic-type-requesttype-f-5a8f04","errorCode":null,"errorMessage":"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}).","messagePattern":"Error registering the generic type: (.+?)\\. One of the generic type parameter's count of types that can close exceeds the maximum length allowed \\((.+?)\\)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MediatR/Registration/ServiceRegistrar.cs","lineNumber":265,"sourceCode":"              \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)\n            return new List<List<Type>> { new List<Type>() };\n       \n        cancellationToken.ThrowIfCancellationRequested();\n\n        var currentList = lists[depth];","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Registration/ServiceRegistrar.cs#L247-L283","documentation":"Thrown by GenerateCombinations (depth 0) when any single generic parameter's candidate-closing list has more entries than MaxTypesClosing (default 100). Each parameter is closed against all concrete types that fit, so one parameter with >100 candidates is rejected to prevent combinatorial blow-up.","triggerScenarios":"An open generic handler parameter that more than 100 concrete types could close (e.g. an IRequestHandler<MyMarker,...> where MyMarker is implemented by 150 classes in scanned assemblies).","commonSituations":"Marker interfaces or base request types shared by a very large number of DTOs; scanning a big assembly where many types happen to satisfy the closing constraint; inheriting a common interface broadly across a domain model.","solutions":["Raise the cap deliberately: cfg.MaxTypesClosing = 250; and verify registration time stays acceptable.","Reduce the candidate count by narrowing which types implement the shared marker, or split marker implementations across assemblies and only scan the relevant one.","Register the specific closed handlers explicitly instead of relying on open-generic auto-closing."],"exampleFix":"// before\n// 150 classes implement ICommonRequest in one scanned assembly\ncfg.MaxTypesClosing = 100; // default\n\n// after\ncfg.MaxTypesClosing = 200;\n// or register explicitly:\ncfg.AddRequestHandler<MySpecificRequest, MySpecificHandler>();","handlingStrategy":"validation","validationCode":"// Count candidate closers per parameter and warn before AddMediatR\nforeach (var list in candidateLists)\n    if (list.Count > cfg.MaxTypesClosing)\n        Console.WriteLine($\"Parameter has {list.Count} closers (cap {cfg.MaxTypesClosing})\");","typeGuard":null,"tryCatchPattern":"try { services.AddMediatR(cfg => { /* ... */ }); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"count of types that can close exceeds\"))\n{\n    logger.LogError(ex, \"A generic parameter has too many closers; raise MaxTypesClosing or narrow types\");\n    throw;\n}","preventionTips":["Restrict marker interfaces to the types that truly need them.","Scan dedicated handler assemblies rather than broad domain assemblies."],"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"}