LuckyPennySoftware/MediatR · error · ArgumentException

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

Error message

Error registering the generic type: {requestType.FullName}. The total number of generic type registrations exceeds the maximum allowed ({MaxGenericTypeRegistrations}).

What it means

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.

Source

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

        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)
            return new List<List<Type>> { new List<Type>() };
       
        cancellationToken.ThrowIfCancellationRequested();

        var currentList = lists[depth];
        var childCombinations = GenerateCombinations(requestType, lists, depth + 1, cancellationToken);
        var combinations = new List<List<Type>>();

        foreach (var item in currentList)
        {
            foreach (var childCombination in childCombinations)
            {
                var currentCombination = new List<Type> { item };
                currentCombination.AddRange(childCombination);

View on GitHub (pinned to 916ef1b3d6)

Solutions

  1. Reduce the candidate set per parameter (narrow marker interface usage, scan fewer assemblies).
  2. Lower MaxTypesClosing so the product stays under the cap.
  3. If the registrations are genuinely needed, raise the cap: cfg.MaxGenericTypeRegistrations = 250000; and benchmark startup memory/time.
  4. Prefer explicit closed registrations over open-generic auto-closing for high-fan-out cases.

Example fix

// before
cfg.MaxTypesClosing = 100; // 100x100x100 = 1,000,000 -> exceeds 125000

// after
cfg.MaxTypesClosing = 40; // 40x40x40 = 64,000 < 125000
// or
cfg.MaxGenericTypeRegistrations = 250000;
Defensive patterns

Strategy: validation

Validate before calling

long product = candidateLists.Aggregate(1L, (acc, l) => acc * l.Count);
if (product > cfg.MaxGenericTypeRegistrations)
    Console.WriteLine($"Combinatorial product {product} exceeds cap {cfg.MaxGenericTypeRegistrations}");

Try / catch

try { services.AddMediatR(cfg => { /* ... */ }); }
catch (ArgumentException ex) when (ex.Message.Contains("total number of generic type registrations exceeds"))
{
    logger.LogError(ex, "Combinatorial registration cap exceeded");
    throw;
}

Prevention

When it happens

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

Common situations: Several broad marker interfaces combined in one open generic; large domain models scanned wholesale; raising MaxTypesClosing without anticipating the multiplicative effect.

Related errors


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