{"record":{"id":"9acb1ab9a70a01f0","repo":"LuckyPennySoftware/MediatR","slug":"could-not-create-wrapper-for-type-requesttype","errorCode":null,"errorMessage":"Could not create wrapper for type {requestType}","messagePattern":"Could not create wrapper for type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/MediatR/Mediator.cs","lineNumber":113,"sourceCode":"\r\n            var requestInterfaceType = requestType.GetInterfaces().FirstOrDefault(static i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>));\r\n            if (requestInterfaceType is null)\r\n            {\r\n                requestInterfaceType = requestType.GetInterfaces().FirstOrDefault(static i => i == typeof(IRequest));\r\n                if (requestInterfaceType is null)\r\n                {\r\n                    throw new ArgumentException($\"{requestType.Name} does not implement {nameof(IRequest)}\", nameof(request));\r\n                }\r\n\r\n                wrapperType = typeof(RequestHandlerWrapperImpl<>).MakeGenericType(requestType);\r\n            }\r\n            else\r\n            {\r\n                var responseType = requestInterfaceType.GetGenericArguments()[0];\r\n                wrapperType = typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, responseType);\r\n            }\r\n\r\n            var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($\"Could not create wrapper for type {requestType}\");\r\n            return (RequestHandlerBase)wrapper;\r\n        });\r\n\r\n        // call via dynamic dispatch to avoid calling through reflection for performance reasons\r\n        return handler.Handle(request, _serviceProvider, cancellationToken);\r\n    }\r\n\r\n    public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default)\r\n        where TNotification : INotification\r\n    {\r\n        if (notification == null)\r\n        {\r\n            throw new ArgumentNullException(nameof(notification));\r\n        }\r\n\r\n        return PublishNotification(notification, cancellationToken);\r\n    }\r\n\r","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Mediator.cs#L95-L131","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use an unmodified released MediatR assembly.","Preserve wrapper types under trimming/AOT.","Run under full trust with reflection enabled."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return await mediator.Send(requestObject, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not create wrapper\"))\n{ /* internal-invariant: check MediatR assembly, AOT/trim, trust level */ throw; }","preventionTips":["Use an unmodified MediatR assembly.","Preserve wrapper types under trimming/AOT.","Run under full trust."],"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"}