Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic registration of pre- and post-processors does not work as I expect #1099

Open
Phundamentals opened this issue Feb 28, 2025 · 0 comments

Comments

@Phundamentals
Copy link

Phundamentals commented Feb 28, 2025

I'm quite new to MediatR, so please forgive me if I lack understanding.

I was playing ping-pong using MediatR and encountered some unexpected results. I feel like this might be related to issue #1087.

I've included a project that demonstrates my findings (hopefully not too elaborate):

Registration problems.zip

It consists of these public classes, all in a single assembly:

class Ping : IRequest<Pong>;
class Pong;
class PingHandler : IRequestHandler<Ping, Pong>;
class PingPreProcessor : IRequestPreProcessor<Ping>;
class PingPostProcessor : IRequestPostProcessor<Ping, Pong>;
class GenericPreProcessor<TRequest> : IRequestPreProcessor<TRequest> where TRequest : Ping;
class GenericPostProcessor<TRequest, TResponse> : IRequestPostProcessor<TRequest, TResponse> where TRequest : Ping where TResponse : Pong

I was expecting that AutoRegisterRequestProcessors would register PingPreProcessor and PingPostProcessor, but it does not. I have to register them manually.

I also expected the generic variants to auto-register, but they don't. What does happen, is that the generic preprocessor will register if I manually register PingPreProcessor, and the other way around with the post one. So the manual registration of PingPreProcessor and PingPostProcessor triggers the automatic registration of their generic counterparts.

Is this behavior as expected?

I see no other way to register the generic variants than by closing their type arguments by doing AddRequestPreProcessor<GenericPreProcessor<Ping>>() and AddRequestPostProcessor<GenericPostProcessor<Ping, Pong>>().

Issue #1087 mentions that MediatR will try to register all possible combinations for open generics, which seems to contradict what I'm seeing.

The attached project allows for easy trying out combinations and logs handler/processor activations. Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant