LuckyPennySoftware/MediatR · critical · InvalidOperationException

Could not create wrapper for type {requestType}

Error message

Could not create wrapper for type {requestType}

What it means

After the non-generic Send(object) path resolves the IRequest interface and computes the wrapper type, Activator.CreateInstance is invoked. As with the generic variants, the throw only triggers if Activator returns null — an internal-invariant failure for these internal wrapper types.

Source

Thrown at src/MediatR/Mediator.cs:113

            var requestInterfaceType = requestType.GetInterfaces().FirstOrDefault(static i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>));
            if (requestInterfaceType is null)
            {
                requestInterfaceType = requestType.GetInterfaces().FirstOrDefault(static i => i == typeof(IRequest));
                if (requestInterfaceType is null)
                {
                    throw new ArgumentException($"{requestType.Name} does not implement {nameof(IRequest)}", nameof(request));
                }

                wrapperType = typeof(RequestHandlerWrapperImpl<>).MakeGenericType(requestType);
            }
            else
            {
                var responseType = requestInterfaceType.GetGenericArguments()[0];
                wrapperType = typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, responseType);
            }

            var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($"Could not create wrapper for type {requestType}");
            return (RequestHandlerBase)wrapper;
        });

        // call via dynamic dispatch to avoid calling through reflection for performance reasons
        return handler.Handle(request, _serviceProvider, cancellationToken);
    }

    public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
        where TNotification : INotification
    {
        if (notification == null)
        {
            throw new ArgumentNullException(nameof(notification));
        }

        return PublishNotification(notification, cancellationToken);
    }

View on GitHub (pinned to 916ef1b3d6)

Solutions

  1. Use an unmodified released MediatR assembly.
  2. Preserve wrapper types under trimming/AOT.
  3. Run under full trust with reflection enabled.
Defensive patterns

Strategy: try-catch

Try / catch

try { return await mediator.Send(requestObject, ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Could not create wrapper"))
{ /* internal-invariant: check MediatR assembly, AOT/trim, trust level */ throw; }

Prevention

When it happens

Trigger: Non-generic Send where the request type does implement IRequest/IRequest<T>, interface resolution succeeded, but Activator.CreateInstance returned null for the closed RequestHandlerWrapperImpl type.

Common situations: Same class as the generic Activator-null failures: trimming/AOT, partial trust, or a modified MediatR assembly. Realistically unreachable in a normal full-truth desktop/server host.

Related errors


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