LuckyPennySoftware/MediatR · error · ArgumentException

Error registering the generic type: {requestType.FullName}.

Error message

Error registering the generic type: {requestType.FullName}. The number of generic type parameters exceeds the maximum allowed ({MaxGenericTypeParameters}).

What it means

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.

Source

Thrown at src/MediatR/Registration/ServiceRegistrar.cs:260

        if (requestType.IsGenericParameter)
            return null;

        var requestGenericTypeDefinition = requestType.GetGenericTypeDefinition();
              
        var combinations = GenerateCombinations(requestType, typesThatCanCloseForEachParameter, 0, cancellationToken);

        return combinations.Select(types => requestGenericTypeDefinition.MakeGenericType(types.ToArray())).ToList();
    }

    // Method to generate combinations recursively
    public static List<List<Type>> GenerateCombinations(Type requestType, List<List<Type>> lists, int depth = 0, CancellationToken cancellationToken = default)
    {
        if (depth == 0)
        {
            // Initial checks
            if (MaxGenericTypeParameters > 0 && lists.Count > MaxGenericTypeParameters)
                throw new ArgumentException($"Error registering the generic type: {requestType.FullName}. The number of generic type parameters exceeds the maximum allowed ({MaxGenericTypeParameters}).");

            foreach (var list in lists)
            {
                if (MaxTypesClosing > 0 && list.Count > MaxTypesClosing)
                    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}).");
            }

            // Calculate the total number of combinations
            long totalCombinations = 1;
            foreach (var list in lists)
            {
                totalCombinations *= list.Count;
                if (MaxGenericTypeParameters > 0 && totalCombinations > MaxGenericTypeRegistrations)
                    throw new ArgumentException($"Error registering the generic type: {requestType.FullName}. The total number of generic type registrations exceeds the maximum allowed ({MaxGenericTypeRegistrations}).");
            }
        }

        if (depth >= lists.Count)

View on GitHub (pinned to 916ef1b3d6)

Solutions

  1. Redesign the open generic interface to fewer type parameters (group related parameters into a single wrapper type).
  2. If the arity is intentional and bounded, raise the cap: cfg.MaxGenericTypeParameters = 15; (increases registration cost).
  3. Exclude that interface from automatic scanning by not registering the assembly, or split handler types into a dedicated assembly scanned with a narrower surface.

Example fix

// before
public interface IRequestHandler<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11> { ... }

// after
public interface IRequestHandler<TBatch> where TBatch : IRequest { ... }
cfg.MaxGenericTypeParameters = 12; // only if arity is truly required
Defensive patterns

Strategy: validation

Validate before calling

// Reject high-arity open generic handler interfaces before AddMediatR scans
foreach (var t in assembly.GetTypes())
{
    foreach (var i in t.GetInterfaces().Where(i => i.IsGenericType))
    {
        var args = i.GetGenericArguments().Length;
        if (args > cfg.MaxGenericTypeParameters)
            Console.WriteLine($"{t} closes {i.GetGenericTypeDefinition()} with {args} args");
    }
}

Try / catch

try { services.AddMediatR(cfg => { /* ... */ }); }
catch (ArgumentException ex) when (ex.Message.Contains("exceeds the maximum allowed"))
{
    logger.LogError(ex, "Open generic arity exceeds MaxGenericTypeParameters");
    throw;
}

Prevention

When it happens

Trigger: 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.

Common situations: A request interface with an unusually high arity; combining many open generic handler abstractions in one assembly; raising MaxTypesClosing without considering arity effects.

Related errors


AI-assisted analysis of LuckyPennySoftware/MediatR@916ef1b3d6 (2026-08-13). Data as JSON: /api/errors/3fd8c474a3f3111b. Report an issue: GitHub.