Search code examples
c#asp.net-mvcsocketsasp.net-mvc-4background-process

How would I run a continuous task on an ASP.Net Server?


I've tried researching this, but haven't found much that sounds similar to something I'm needing to implement. In short, we'll be running an ASP Website on a server that will be accessed by clients. Ideally, we have a function that we want to initialize upon the start of a user's session, and stop when the session ends. While the session is happening, this function sends and receives messages via socket communication, meaning we need to access the send/receive functions of this class from pages in order to move information. What's the best way to go about this?


Solution

  • Look into SignalR. That's probably what you're wanting. Its "hubs" are effectively what you're looking for to spin up on session initiation, and spin down when the user disappears. It has a client-side JS library that automatically chooses the best connection method available (e.g., websockets > server-sent-events > long-polling), and it allows you to send messages both from the client to the server, and from the server to the client.

    http://www.asp.net/signalr

    Another alternative that I've played around with in the past is XSockets:

    https://xsockets.net/

    It's similar to SignalR in many respects, but it's not free.