Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 714
JtHa77 Avatar asked Nov 11 '15 20:11

JtHa77


2 Answers

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.

like image 197
Ken Smith Avatar answered Oct 18 '22 07:10

Ken Smith


It's hard to tell from you description, are you looking to communicate with the client browser via sockets? Or are you trying to communicate with some other service via sockets?

Web applications are not ideally suited for deterministic types of actions. It's difficult for the web server to know whether or not the client has actually closed their browser or not. In most cases, sessions simply time out after a period of inactivity (20+ minutes in most cases). So you cannot reliably know when the users session has actually ended.

To top it off, there are certain edge cases where Session_End will not fire. For instance, if the app pool recycles, then no Session_End event will fire. This may not be an issue, since if the app pool recycles your other connections would also recycle, but it's still an issue to keep in mind.

Finally, Web apps are not intended to be long running.

like image 36
Erik Funkenbusch Avatar answered Oct 18 '22 07:10

Erik Funkenbusch