[英]ASP.NET global background worker thread
I would like to create a background thread to pull data from another server periodically and send out real time email alerts to users. 我想创建一个后台线程来定期从另一台服务器提取数据,并向用户发送实时电子邮件警报。
Requirement: 需求:
The worker should never time out. 工人永不超时。
The worker should be running on a separate thread which means the server should still be able to handle user's requests while it is running 工作程序应在单独的线程上运行,这意味着服务器在运行时仍应能够处理用户的请求
The worker should not be created per request but rather as a global thread which will be running when the server starts 不应按请求创建工作程序,而应将其作为在服务器启动时将运行的全局线程
I know it is a bad idea to create a background thread per request, but what is the best practice for a global backgroundworker thread? 我知道为每个请求创建一个后台线程是一个坏主意,但是全局backgroundworker线程的最佳实践是什么? Thanks in advance
提前致谢
Edited: 编辑:
I am using Windows Azure to host the site, so I dont think I can create a windows service to run the task 我正在使用Windows Azure托管站点,所以我认为我无法创建Windows服务来运行任务
Usually, you don't run such tasks in the web application itself as the application pool will be shut down after some time of inactivity depending on the configuration of the environment. 通常,您不会在Web应用程序本身中运行此类任务,因为在一段时间不活动之后,根据环境的配置,应用程序池将被关闭。
To make this work in a reliable way, create a separate application that periodically retrieves the data and sends the alerts. 为了使此工作可靠地进行,请创建一个单独的应用程序,该应用程序定期检索数据并发送警报。 There are several ways to achieve this:
有几种方法可以实现此目的:
Most people would recommend you to work with a Windows Service to accomplish it. 大多数人会建议您使用Windows服务来完成它。 However, a reasonable way to do this would be using a scheduling framework like Quartz .NET : http://quartznet.sourceforge.net/
但是,一种合理的方法是使用像Quartz .NET这样的调度框架: http : //quartznet.sourceforge.net/
That way, if you think about it, your application deployment would even be easier - no win services or EXEs to deploy. 这样,如果您考虑一下,您的应用程序部署将更加容易-无需部署win服务或EXE。
So If you decide to do it and run it embedded in your ASP.NET application, then you can start utilize it in the Global.Asax
file, at Application_Start()
, like this: 因此,如果您决定执行此操作并将其嵌入ASP.NET应用程序中运行,则可以在
Application_Start()
的Global.Asax
文件中开始使用它,如下所示:
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteServer";
// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";
ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();
sched.Start();
I needed something similar to build scrapers. 我需要类似的东西来建造刮板。 What I did is use the .Net ThreadPool class to send async http requests.
我所做的是使用.Net ThreadPool类发送异步http请求。 I built a wrapper for building async requests with state object and then call:
我构建了一个包装器,用于使用状态对象构建异步请求,然后调用:
ThreadPool.QueueUserWorkItem(new WaitCallback(asyncWrapper.BeginGetMethod), asyncStateObject);
Typically web servers are designed for serving up web pages. 通常,Web服务器是为提供网页而设计的。 Requests come in, HTML pages (among other things) go out.
请求进入,HTML页面(以及其他内容)消失。 Unless you have full control over the server, what you are trying to do will be hard to achieve.
除非您完全控制服务器,否则您将很难实现所要执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.