Hi there!
Currently working on a project that contains a console application. That console app subscribes and listens to a SignalR feed, and whenever a message comes in it is handled by the appropriate event handler.
Whenever the application is started, MediatR and all it's necessary dependencies are registered on the main thread (startup thread). And after that, my SignalR connection is configured adding the necessary event handlers and starting the connection. In the background, SignalR will process incoming messages on seperate threads and let the event handler process the messages.
This is where I get into trouble, because in one of my event handlers I implement IMediator to do some commands and queries. However, I get the following error message:
Cannot access a disposed object. Object name: 'IServiceProvider'.
This is because IMediator uses scoped dependencies which, in the separate thread that has now been created, are not available anymore.
Is there any way to change the lifetime of the dependencies registered by MediatR?
UPDATE
If I inject the command handlers into my event handler directly, in stead of using IMediator, it works like a charm.
Yeah - don't call Mediator from inside a handler, things get weird.
Hey @jbogard, I just ran into this same issue yesterday when calling IMediator from inside a handler. Do you have any alternative suggestions when such need happens?!
Hi,
I ran into that issue also.
Is it ok to publish an event from inside a handler ?
Same issue here.
Do you have any solution, or should i not user mediator at all in this case ?
Most helpful comment
Hey @jbogard, I just ran into this same issue yesterday when calling
IMediatorfrom inside a handler. Do you have any alternative suggestions when such need happens?!