IIS后台线程和SignalR [英] IIS Background thread and SignalR
问题描述
我需要有一个后台线程来完成一些工作,并通过SignalR向与服务相关联的用户发送数据。
考虑在IIS中托管此线程,并在Application_Start首次命中或单独的工作进程中生成它。
I thought of hosting this thread inside IIS, and to spawn it when Application_Start is first hit, or in a separate worker process.
如果我在IIS中托管它,在应用程序的开始创建它
- 该线程仅在首次命中应用程序时启动。我需要它运行,只要我开始服务。
- 我无法通过桌面GUI控制此线程,我无法以简单的方式停止或暂停此线程。
If I host it in IIS and create it at the start of the application - The thread starts only when the app is first hit. I need it running as soon as I start the service. - I do not have control over this thread via a desktop GUI, I can't stop or pause it in a simple way.
如果我在一个单独的进程,如Windows服务托管它
- 我没有访问SignalR服务实例
- 我不想连接到SignalR服务用户向其他用户发送数据。我想要一个不同的方法,这不意味着工人是SignalR本身的客户。
If I host it in a separate process, such as a Windows Service - I don't have access to the SignalR service instance - I don't want to connect to the SignalR service as a user to send data to other users. I wanr a different approach to this, one that doesn't imply the worker being a client to SignalR itself.
您对此有什么看法?你看到任何其他解决方案吗?
推荐答案
您的Windows服务可以调用的Web应用程序。
The way we have approached this is to create a separate endpoint on the web application that your Windows service can call.
假设ASP.NET MVC控制器中存在以下URI: http:// [myserver] / api / TellUsers / [msg] 中。在此方法中,您可以获取连接的中心客户端并拨打电话。
Imagine the following URI exists in an ASP.NET MVC controller: http://[myserver]/api/TellUsers/[msg]. Inside of this method, you can get the connected hub clients and make the call.
[HttpPut]
public void TellUsers(string msg)
{
var connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
var demoClients = connectionManager.GetClients<MyHubDerivedClass>();
demoClients.TellUsers(msg);
}
[在此插入关于正确检查错误的注意事项。]
当然,你不必使用MVC。任何公开访问的URI都可以工作。为了保护它,您可以使用任何有效的技术保护ASP.NET端点。
Of course, you don't have to use MVC. Any publicly accessible URI would work. As for securing it, you can use any valid technique for securing ASP.NET endpoints.
这篇关于IIS后台线程和SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!