LuckyPennySoftware/MediatR · critical · InvalidOperationException
Could not create wrapper type for {requestType}
Error message
Could not create wrapper type for {requestType} What it means
In the strongly-typed Send<TResponse>(IRequest<TResponse>) path, MediatR builds and caches a RequestHandlerWrapperImpl<TRequest,TResponse> via reflection. MakeGenericType constructs the closed wrapper type and Activator.CreateInstance instantiates it; the null-coalescing throw fires only if Activator returns null, which for these internal parameterless wrapper types should never happen under normal runtime conditions.
Source
Thrown at src/MediatR/Mediator.cs:60
_serviceProvider.CheckLicense();
}
/// <summary>
/// Gets or sets the license key. You can find your license key in your <a href="https://luckypennysoftware.com/account">account</a>.
/// </summary>
public static string? LicenseKey { get; set; }
public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var handler = (RequestHandlerWrapper<TResponse>)_requestHandlers.GetOrAdd(request.GetType(), static requestType =>
{
var wrapperType = typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse));
var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($"Could not create wrapper type for {requestType}");
return (RequestHandlerBase)wrapper;
});
return handler.Handle(request, _serviceProvider, cancellationToken);
}
public Task Send<TRequest>(TRequest request, CancellationToken cancellationToken = default)
where TRequest : IRequest
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var handler = (RequestHandlerWrapper)_requestHandlers.GetOrAdd(request.GetType(), static requestType =>
{
var wrapperType = typeof(RequestHandlerWrapperImpl<>).MakeGenericType(requestType);
var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($"Could not create wrapper type for {requestType}");
View on GitHub (pinned to 916ef1b3d6)
Solutions
- Confirm you are using an unmodified, released MediatR assembly from NuGet.
- If running under trimming/AOT, ensure MediatR's wrapper types are preserved (dynamic registration requires reflection support).
- If running in partial trust, grant ReflectionPermission / use a full-trust host.
Defensive patterns
Strategy: try-catch
Try / catch
try { return await mediator.Send<TResponse>(request, ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Could not create wrapper type"))
{ /* internal-invariant: report assembly/trimming/AOT health, do not retry unchanged */ throw; } Prevention
- Ship the unmodified released MediatR assembly.
- If publishing trimmed/AOT, add root descriptors for RequestHandlerWrapperImpl<,>.
- Run the host under full trust with reflection permitted.
When it happens
Trigger: Reached only if Activator.CreateInstance returns null for the closed RequestHandlerWrapperImpl<,> type. Practically this implies a type-loading or reflection-permission failure, or a corrupted MediatR build, since the wrapper types are internal MediatR types with public parameterless constructors.
Common situations: Essentially an internal-invariant failure: partial-trust / reflection-restricted environments, a custom MediatR build where wrapper constructors were removed, or AOT/trimming scenarios where the wrapper type was trimmed away.
Related errors
- Could not create wrapper for type {requestType}
- Could not create wrapper for type {notificationType}
- {requestType.Name} does not implement IRequest
- {requestType.Name} does not implement IStreamRequest<TRespon
- Open behavior type can not be null.
AI-assisted analysis of LuckyPennySoftware/MediatR@916ef1b3d6 (2026-08-13).
Data as JSON: /api/errors/4c494ea2dc6377be.
Report an issue: GitHub.