Replies: 6 comments
-
Indeed, it's possible, not only that but also quite fun. The only thing you need is a Something like this: using var scope = _serviceScopeFactory.CreateScope();
var sender = scope.ServiceProvider.GetRequiredService<ISender>();
var result = await sender.Send(new CustomRequest()); And then do whatever you want with the response! This way you get an scoped environment for each request, which is pretty neat and inline with the way MediatR is usually used. Let me know if this solves your question or if I got something wrong on my understanding of your concerns! |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply, but maybe I didn't make myself clear. I will perform the mediatr.Send from somewhere else in the micro service. Inside the background service I want to create a handler to handle a sent message. |
Beta Was this translation helpful? Give feedback.
-
Oh, then I don't think MediatR alone is right for your use case, you probably need something else, more like System.Threading.Channels and the great helper library Open.ChannelExtensions. If you can compromise on sending the data from within the background service or just initiating the connection somewhere else, you could use MediatR no problem. You can also use a combination of both of them, using MediatR to perform handling of the request and leaving the result inside the channel for your background service to pickup and send over TCP. |
Beta Was this translation helpful? Give feedback.
-
One of the solutions is to use ConcurrentQueue which will contain messages(commands) that needs to be handled. PS. This is absolutely not related to MediatR at all. |
Beta Was this translation helpful? Give feedback.
-
I am not sure what is going on here. A ConcurrentQueue will only work if both services run inside the same host. Suppose you want to send requests to other services running in different pods/instances. In that case, you need to implement a handler that either directly sends the message over the network or stores the request/notification in an event streaming platform like Kafka, Redis, or Azure Event Hub. Then your consuming applications have a subscription to the topic and transmit them again. |
Beta Was this translation helpful? Give feedback.
-
Exactly. Concurrent queue will only work inside one process(not the host, cause on one host several processes can run in parallel). And my assumption was that this is what OP meant. |
Beta Was this translation helpful? Give feedback.
-
I have several micro services that contain a .Net class inherited from BackgroundService. I need to be able to execute some methods inside the background service.
Is there a way to send a Mediatr command from elsewhere in the code to a handler defined inside the background service? The methods must be run inside the background service as the commands will send messages over an existing TCP listener connection.
Thanks in advance for any advice.
Beta Was this translation helpful? Give feedback.
All reactions