Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Signal-R fit in the IIS activation model?

I am learning Signal-R, and this is something that has been in my head during all time.

  • How does Signal-R fits in the IIS/ASP.NET life cycle?
  • How long does the Hubs live (I see they have re-connection semantics)?
  • Does IIS does prevent the shutdown of an AppDomain that has a persistent connection?

It is my understanding that IIS is designed to handle request-response scenarios. A request hits IIS, this finds the AppDomain, activate it, and then pass the request to it. And after an idle time, shutdown the AppDomain. If the request takes too long, a timeout exception is thrown.

Now let´s imagine that I have another application that broadcast information through a TCP socket. I want my javascript clients to get that information in real time, so I create a Signal-R web application. I can create a TCP client on application start, but what does guarantee that IIS is not going to shutdown the whole thing after some time with inactivity?

I could self host the Signal-R app in a window service, but then I would have to use a different port, enable cross domain, etc... Many problems for deployment. But, I am concerned about using an ASP.NET MVC application for this, since it looks to me like fitting a driving wheel in a motorbike.

Cheers.

like image 684
vtortola Avatar asked Dec 22 '13 12:12

vtortola


1 Answers

SignalR in IIS/ASP.NET Lifecycle

  • SignalR uses Owin: http://owin.org/
  • A good article on Owin here: http://msdn.microsoft.com/en-us/magazine/dn451439.aspx

Hub object lifetime

From the SignalR docs: http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server#transience:

You don't instantiate the Hub class or call its methods from your own code on the server; all that is done for you by the SignalR Hubs pipeline. SignalR creates a new instance of your Hub class each time it needs to handle a Hub operation such as when a client connects, disconnects, or makes a method call to the server.

Because instances of the Hub class are transient, you can't use them to maintain state from one method call to the next. Each time the server receives a method call from a client, a new instance of your Hub class processes the message. To maintain state through multiple connections and method calls, use some other method such as a database, or a static variable on the Hub class, or a different class that does not derive from Hub. If you persist data in memory, using a method such as a static variable on the Hub class, the data will be lost when the app domain recycles.

Your long running TCP client

This is not a problem with SignalR. Your TCP client can be shutdown by IIS: http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

I would rather make the TCP client run in a windows service. The TCP client receives TCP broadcast messages and forwards the messages to the Hub using the SignalR .NET client.

like image 77
Hallvar Helleseth Avatar answered Oct 11 '22 22:10

Hallvar Helleseth