{"record":{"id":"4c494ea2dc6377be","repo":"LuckyPennySoftware/MediatR","slug":"could-not-create-wrapper-type-for-requesttype","errorCode":null,"errorMessage":"Could not create wrapper type for {requestType}","messagePattern":"Could not create wrapper type for (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/MediatR/Mediator.cs","lineNumber":60,"sourceCode":"        _serviceProvider.CheckLicense();\r\n    }\r\n\r\n    /// <summary>\r\n    /// Gets or sets the license key. You can find your license key in your <a href=\"https://luckypennysoftware.com/account\">account</a>.\r\n    /// </summary>\r\n    public static string? LicenseKey { get; set; }\r\n\r\n    public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)\r\n    {\r\n        if (request == null)\r\n        {\r\n            throw new ArgumentNullException(nameof(request));\r\n        }\r\n\r\n        var handler = (RequestHandlerWrapper<TResponse>)_requestHandlers.GetOrAdd(request.GetType(), static requestType =>\r\n        {\r\n            var wrapperType = typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse));\r\n            var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($\"Could not create wrapper type for {requestType}\");\r\n            return (RequestHandlerBase)wrapper;\r\n        });\r\n\r\n        return handler.Handle(request, _serviceProvider, cancellationToken);\r\n    }\r\n\r\n    public Task Send<TRequest>(TRequest request, CancellationToken cancellationToken = default)\r\n        where TRequest : IRequest\r\n    {\r\n        if (request == null)\r\n        {\r\n            throw new ArgumentNullException(nameof(request));\r\n        }\r\n\r\n        var handler = (RequestHandlerWrapper)_requestHandlers.GetOrAdd(request.GetType(), static requestType =>\r\n        {\r\n            var wrapperType = typeof(RequestHandlerWrapperImpl<>).MakeGenericType(requestType);\r\n            var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($\"Could not create wrapper type for {requestType}\");\r","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Mediator.cs#L42-L78","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return await mediator.Send<TResponse>(request, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not create wrapper type\"))\n{ /* internal-invariant: report assembly/trimming/AOT health, do not retry unchanged */ throw; }","preventionTips":["Ship the unmodified released MediatR assembly.","If publishing trimmed/AOT, add root descriptors for RequestHandlerWrapperImpl<,>.","Run the host under full trust with reflection permitted."],"tags":["mediator","reflection","wrapper","internal-invariant","activator"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}