{"record":{"id":"b9b81bfef43a2787","repo":"LuckyPennySoftware/MediatR","slug":"could-not-create-wrapper-for-type-notificationtyp","errorCode":null,"errorMessage":"Could not create wrapper for type {notificationType}","messagePattern":"Could not create wrapper for type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/MediatR/Mediator.cs","lineNumber":155,"sourceCode":"            _ => throw new ArgumentException($\"{nameof(notification)} does not implement ${nameof(INotification)}\")\r\n        };\r\n\r\n    /// <summary>\r\n    /// Override in a derived class to control how the tasks are awaited. By default the implementation calls the <see cref=\"INotificationPublisher\"/>.\r\n    /// </summary>\r\n    /// <param name=\"handlerExecutors\">Enumerable of tasks representing invoking each notification handler</param>\r\n    /// <param name=\"notification\">The notification being published</param>\r\n    /// <param name=\"cancellationToken\">The cancellation token</param>\r\n    /// <returns>A task representing invoking all handlers</returns>\r\n    protected virtual Task PublishCore(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification, CancellationToken cancellationToken) \r\n        => _publisher.Publish(handlerExecutors, notification, cancellationToken);\r\n\r\n    private Task PublishNotification(INotification notification, CancellationToken cancellationToken = default)\r\n    {\r\n        var handler = _notificationHandlers.GetOrAdd(notification.GetType(), static notificationType =>\r\n        {\r\n            var wrapperType = typeof(NotificationHandlerWrapperImpl<>).MakeGenericType(notificationType);\r\n            var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($\"Could not create wrapper for type {notificationType}\");\r\n            return (NotificationHandlerWrapper)wrapper;\r\n        });\r\n\r\n        return handler.Handle(notification, _serviceProvider, PublishCore, cancellationToken);\r\n    }\r\n\r\n\r\n    public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<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 streamHandler = (StreamRequestHandlerWrapper<TResponse>)_streamRequestHandlers.GetOrAdd(request.GetType(), static requestType =>\r\n        {\r\n            var wrapperType = typeof(StreamRequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse));\r\n            var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($\"Could not create wrapper for type {requestType}\");\r","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Mediator.cs#L137-L173","documentation":"PublishNotification closes NotificationHandlerWrapperImpl<TNotification> and activates it. The throw fires only if Activator.CreateInstance returns null for this internal wrapper type — again an internal-invariant violation rather than a user-facing condition.","triggerScenarios":"A valid INotification is published and the cached wrapper factory's Activator.CreateInstance returns null for the closed NotificationHandlerWrapperImpl type.","commonSituations":"Trimming/AOT removing the wrapper type, partial-trust reflection restrictions, or a corrupted/modified MediatR assembly. Not reachable in a normal full-trust host.","solutions":["Use an unmodified released MediatR assembly.","Preserve MediatR wrapper types under trimming/AOT or avoid reflection-based dispatch.","Run under full trust."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await mediator.Publish(notification, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not create wrapper\"))\n{ /* internal-invariant: verify MediatR assembly, AOT/trim, trust level */ throw; }","preventionTips":["Ship the unmodified MediatR assembly.","Preserve NotificationHandlerWrapperImpl<> under trimming.","Run the host under full trust."],"tags":["mediator","publish","wrapper","internal-invariant","activator","reflection"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}