简体   繁体   English

Signal-R如何适应IIS激活模型?

[英]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. 我正在学习Signal-R,这是我一直在想的事情。

  • How does Signal-R fits in the IIS/ASP.NET life cycle? Signal-R如何适应IIS / ASP.NET生命周期?
  • 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? IIS是否会阻止关闭具有持久连接的AppDomain?

It is my understanding that IIS is designed to handle request-response scenarios. 据我所知,IIS旨在处理请求 - 响应方案。 A request hits IIS, this finds the AppDomain, activate it, and then pass the request to it. 一个请求命中IIS,它找到AppDomain,激活它,然后将请求传递给它。 And after an idle time, shutdown the AppDomain. 在空闲时间之后,关闭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. 现在让我们假设我有另一个通过TCP套接字广播信息的应用程序。 I want my javascript clients to get that information in real time, so I create a Signal-R web application. 我希望我的javascript客户端能够实时获取该信息,因此我创建了一个Signal-R Web应用程序。 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? 我可以在应用程序启动时创建一个TCP客户端,但是什么能保证IIS在一段时间不活动后不会关闭整个事件?

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. 我可以自己在一个窗口服务中托管Signal-R应用程序,但后来我将不得不使用不同的端口,启用跨域等...部署的许多问题。 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. 但是,我担心使用ASP.NET MVC应用程序,因为它看起来像是在摩托车中安装驱动轮。

Cheers. 干杯。

SignalR in IIS/ASP.NET Lifecycle IIS / ASP.NET生命周期中的SignalR

Hub object lifetime Hub对象的生命周期

From the SignalR docs: http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server#transience : 来自SignalR文档: 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; 您没有实例化Hub类或从服务器上自己的代码调用其方法; all that is done for you by the SignalR Hubs pipeline. 所有这些都由SignalR Hubs管道完成。 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. 每次需要处理Hub操作时,SignalR都会创建Hub类的新实例,例如客户端连接,断开连接或对服务器进行方法调用时。

Because instances of the Hub class are transient, you can't use them to maintain state from one method call to the next. 因为Hub类的实例是瞬态的,所以不能使用它们来维持从一个方法调用到下一个方法调用的状态。 Each time the server receives a method call from a client, a new instance of your Hub class processes the message. 每次服务器从客户端接收方法调用时,Hub类的新实例都会处理该消息。 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. 要通过多个连接和方法调用来维护状态,请使用其他方法,例如数据库,Hub类上的静态变量,或不从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. 如果将数据保留在内存中,使用Hub类上的静态变量等方法,则应用程序域回收时数据将丢失。

Your long running TCP client 您长时间运行的TCP客户端

This is not a problem with SignalR. 这不是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/ 您的TCP客户端可以被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. 我宁愿让TCP客户端在Windows服务中运行。 The TCP client receives TCP broadcast messages and forwards the messages to the Hub using the SignalR .NET client. TCP客户端接收TCP广播消息,并使用SignalR .NET客户端将消息转发到集线器。

Hubs are recreated on each SignalR request, so if you need a persistent connection you may have to look into using static vars or dictionary to hold state. 在每个SignalR请求上重新创建集线器,因此如果您需要持久连接,则可能需要考虑使用静态变量或字典来保持状态。 But as you point ASP.NET can restart for a variety of reasons. 但是,正如您所指出的,ASP.NET可以出于各种原因重启。

It depends on what persistancy you really need. 这取决于你真正需要的持久性。 If you have a connection that MUST stay alive at all times and cannot be torn down and reestablished then hosting in IIS is not the right choice. 如果你的连接必须始终保持活着并且无法拆除并重新建立,那么在IIS中托管不是正确的选择。 However, if you can re-establish the same connection after a shutdown, then maybe this can still work. 但是,如果您可以在关机后重新建立相同的连接,那么这可能仍然有效。

You can do quite a bit in making sure that ASP.NET apps don't shut down in recent versions of IIS: 您可以做很多工作来确保ASP.NET应用程序在最新版本的IIS中不会关闭:

http://weblog.west-wind.com/posts/2013/Oct/02/Use-IIS-Application-Initialization-for-keeping-ASPNET-Apps-alive http://weblog.west-wind.com/posts/2013/Oct/02/Use-IIS-Application-Initialization-for-keeping-ASPNET-Apps-alive

If that's not enough for you running as a separate service is an option. 如果这不足以让您作为单独的服务运行是一个选项。 If you run as a service on the same IP address there are no cross domain concerns. 如果您在同一IP地址上作为服务运行,则不存在跨域关注。 Here's more info on running SignalR using a Windows Service: 以下是使用Windows服务运行SignalR的更多信息:

http://weblog.west-wind.com/posts/2013/Sep/04/SelfHosting-SignalR-in-a-Windows-Service http://weblog.west-wind.com/posts/2013/Sep/04/SelfHosting-SignalR-in-a-Windows-Service

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM