this
this

Reputation: 1426

Best way to communicate between ASP.NET sessions?

I'm trying to find what is the right design where a client will open 2 sessions where one is used to upload a large file and another is used to provide SignalR messaging. The goal is that when the server processes the large file, it needs to provide status messages to the SignalR channel so that the client keeps get notified about the processes going on the server during and beyond the upload itself. The assumption is that at the upload, the client will provide the SignalR identifier so that the worker working on the file upload can pass it on to the Hub but what I'm not actually sure about is how the upload worker should connect with Hub. Do I maintain a static singleton class that holds a persistent reference to the hub? Or do I just make a new HTTP session and send via loopback? Or is there a better approach I didn't even think of?

Upvotes: 0

Views: 134

Answers (1)

Stafford Williams
Stafford Williams

Reputation: 9806

You can use dependency injection to get a reference to your hub;

public class UploadController 
{
    IHubContext<YourHub> _hub;
    public UploadController(IHubContext<YourHub> hub) 
    {
        _hub = hub;
    }

    public ActionResult Upload(SomeFile model)
    {
        // start upload processing

        // send progress updates
        var conectionId = DetermineConnectionId(); // store it in a dictionary maybe?
        _hub.Clients.Client(connectionId).SendProgressUpdate();
    }
}

Upvotes: 1

Related Questions