I want to send message from Service A using SignalR when some event occures (for example, message from Service B received). So hub method needs to be called from some sort of handler, that not constructed using Dependency Injection. How I can do this?
So far, I tried and read about the following things:
- I can inject context into Controller and lead it to my handler. I probably can do that, but passing hub context from the top (controller class) to the bottom (handler class) is not the best approach, which adds a lot of dependencies to the classes that should not be aware of this context, so I would like to avoid that.
- I can inject my IHubContext in "any" class, but then, the thing is, how to get an instance of that class on my handler?
- Add Static method to class with injected context!? Well, that works until you have 1 client because with new client static property is going to be overwritten.
So I cannot imagine, how handler can use dependency injected IHubContext.
Probably, someone did that before and have an example of how to truly inject context into any class.
Thank you in advance and any additional information will be provided, if necessary.
AddSingleton
or otherAdd*
methods. Then resolve it using theServiceProvider
which will automatically resolve the IHubContext dependency on my handler. Can you please elaborate with more code? – Shahzad Hassan