Paritosh
Paritosh

Reputation: 4503

mvc controller read signalr value

We are currently using asp.net MVC with SignalR to communicate with the browser's client side code.

Currently our hub is built as:

public class SectionHub : Hub
{
}

MVC controller gets a reference to it. And within a loop it uses the hub to notify the client when certain steps are done:

var progressHub = GlobalHost.ConnectionManager.GetHubContext<SectionHub>();
progressHub.Clients.All.updateCurrentProgress(....)

All this works fine. We are now adding a stop button on the client and want to notify the MVC controller to stop the process when it's clicked. We updated the SectionHub to include a property which the client is able to update. However, since the progressHub object is an IHubContext it does not have access to that property. Is there anyway to get the new property into the hub and have it accessible in the MVC controller, or create the hub as a SectionHub rather than IHubContext?

Upvotes: 0

Views: 352

Answers (2)

Anders
Anders

Reputation: 17554

You should not have that kind of logic in the hub, the hub should only be used to push state. Move all code to a background worker, let the MVC controller action queue the work and return directly (Long running controller action is a anti pattern). let the stop MVC controller action call the worker queue and tell it to stop the work.

Thats a much more solid solution if you ask me.

edit: Btw, I think my library would help you, you could use it to seamless push progress state from your worker to your clients https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki

Upvotes: 1

Wasp
Wasp

Reputation: 3425

Even if you would find a way to get an handle on a SectionHub instance, it would not work properly because those instances are created and dropped at each request, therefore its value would not be reliable.

My advice is, use whatever way you think fits the best to access that property in a global way: a static variable, a dependency-injected service, a database record, a queuing system. Those are all reliable ways to reach your goal (of course some more, some less, it depend on your requirements about fault tolerance, and on the degree of modularity you are trying to achieve).

PS: your code lets anybody stop notifications for everybody to be sent, is this really what you want? Just curious, it might have an impact on the question itself.

Upvotes: 1

Related Questions