{"record":{"id":"30af12922f326f06","repo":"LuckyPennySoftware/MediatR","slug":"requesttype-name-does-not-implement-irequest","errorCode":null,"errorMessage":"{requestType.Name} does not implement IRequest","messagePattern":"(.+?) does not implement IRequest","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MediatR/Mediator.cs","lineNumber":102,"sourceCode":"\r\n    public Task<object?> Send(object 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 = _requestHandlers.GetOrAdd(request.GetType(), static requestType =>\r\n        {\r\n            Type wrapperType;\r\n\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","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/src/MediatR/Mediator.cs#L84-L120","documentation":"The non-generic Send(object) overload inspects the runtime type's interfaces to find IRequest<> or IRequest before building a wrapper. If neither is implemented, the object is not a MediatR request at all, so ArgumentException is thrown naming the offending type.","triggerScenarios":"Calling mediator.Send(someObject) where someObject's runtime type does not implement IRequest or IRequest<TResponse> — e.g. a DTO, a primitive wrapped in object, or a command that was never marked with the IRequest interface.","commonSituations":"Publishing/sending through a non-generic boundary (messaging, reflection, dynamic dispatch) and passing a payload type that forgot to implement IRequest; sending a notification through Send instead of Publish; version mismatch where the request interface was renamed/removed.","solutions":["Mark the request type with `public record Foo : IRequest<Bar>` (or IRequest for unit).","If the object is a notification, call Publish, not Send.","Where possible, use the generic Send<TResponse>(IRequest<TResponse>) overload so the contract is checked at compile time."],"exampleFix":"// before\npublic class DoThing { public int Id; }\nmediator.Send(new DoThing()); // ArgumentException\n// after\npublic record DoThing(int Id) : IRequest<int>;\nmediator.Send(new DoThing(1));","handlingStrategy":"type-guard","validationCode":"var reqInterfaces = request.GetType().GetInterfaces();\nbool isRequest = reqInterfaces.Any(i => i == typeof(IRequest))\n    || reqInterfaces.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>));\nif (!isRequest) throw new ArgumentException($\"{request.GetType().Name} is not a MediatR request.\", nameof(request));","typeGuard":"static bool IsMediatRRequest(object o) =>\n    o.GetType().GetInterfaces().Any(i => i == typeof(IRequest)\n        || (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>)));","tryCatchPattern":null,"preventionTips":["Prefer the generic Send<TResponse>(IRequest<TResponse>) overload so the compiler enforces IRequest.","Mark command/query types with IRequest / IRequest<T> at definition time.","At non-generic boundaries, type-check before calling Send."],"tags":["mediator","send","irequest","type-validation","reflection"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}