我可以使用线程来进行IIS上长期运行的工作吗? [英] Can I use threads to carry out long-running jobs on IIS?

查看:142
本文介绍了我可以使用线程来进行IIS上长期运行的工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.Net应用程序,用户点击网页上的一个按钮以及这然后通过事件处理程序实例的服务器上的对象并调用对象的方法。
该方法熄灭到外部系统做的东西,这可能需要一段时间。所以,我希望做的是运行另一个线程的方法调用,所以我可以将控制权与您的要求已提交用户。
我有理由高兴这样做,因为发射后不管,但它会更好,如果用户能保持轮询对象的状态。

In an ASP.Net application, the user clicks a button on the webpage and this then instantiates an object on the server through the event handler and calls a method on the object. The method goes off to an external system to do stuff and this could take a while. So, what I would like to do is run that method call in another thread so I can return control to the user with "Your request has been submitted". I am reasonably happy to do this as fire-and-forget, though it would be even nicer if the user could keep polling the object for status.

我不知道是,如果IIS允许我的线程继续运行,即使用户会话过期。
可以想象,用户引发事件和我们实例在服务器上的对象,并触发该方法在一个新的线程。用户满意您的请求已提交信息,并关闭浏览器。最终,该用户的会话将会超时在IIS上,但线程可能仍在运行,做的工作。请问IIS允许线程继续运行或者一旦用户会话过期它会杀死它和对象的处置?

What I don't know is if IIS allows my thread to keep running, even if the user session expires. Imagine, the user fires the event and we instantiate the object on the server and fire the method in a new thread. The user is happy with the "Your request has been submitted" message and closes his browser. Eventually, this users session will time out on IIS, but the thread may still be running, doing work. Will IIS allow the thread to keep running or will it kill it and dispose of the object once the user session expires?

编辑:从答案和意见,我明白,要做到这一点的最好办法是将长期运行IIS处理之外。除了一切,这涉及的是应用程序域回收问题。在实践中,我需要在有限的时间得到第1版离地面,并有现有的框架内工作,所以想避免服务层,因此刚断火里面IIS线程的愿望。在实践中,长期运行,这里将只几分钟,在网站上并发会很低,所以应该没问题。但是,下一个版本肯定会需要拆分成单独的服务层。

From the answers and comments, I understand that the best way to do this is to move the long-running processing outside of IIS. Apart from everything else, this deals with the appdomain recycling problem. In practice, I need to get version 1 off the ground in limited time and has to work inside an existing framework, so would like to avoid the service layer, hence the desire to just fire off the thread inside IIS. In practice, "long running" here will only be a few minutes and the concurrency on the website will be low so it should be okay. But, next version definitely will need splitting into a separate service layer.

推荐答案

可以完成你想要的东西,但它通常是一个坏主意。一些ASP.NET博客和CMS发动机采取这种方式,是因为他们想成为一个共享的主机系统上安装,而不是采取一个需要安装一个Windows服务的依赖。通常情况下,他们齐齐在Global.asax中一个长期运行的线程应用程序启动时,并让该线程进程排队任务。

You can accomplish what you want, but it is typically a bad idea. Several ASP.NET blog and CMS engines take this approach, because they want to be installable on a shared hosting system and not take a dependency on a windows service that needs to be installed. Typically they kick off a long running thread in Global.asax when the app starts, and have that thread process queued up tasks.

在除了减少提供给IIS / ASP.NET来处理请求,您还可以与线程被杀害时的AppDomain被回收,然后你必须处理任务的持久性,而这是可以在资源问题飞行,以及在开始工作的备份时的AppDomain回来了。

In addition to reducing resources available to IIS/ASP.NET to process requests, you also have issues with the thread being killed when the AppDomain is recycled, and then you have to deal with persistence of the task while it is in-flight, as well as starting the work back up when the AppDomain comes back up.

记住,在许多情况下的AppDomain是在默认的时间间隔自动回收,以及如果你更新的web.config,因为等。

Keep in mind that in many cases the AppDomain is recycled automatically at a default interval, as well as if you update the web.config, etc.

如果你能处理的持续存在和你的线程在任何时候被杀害的事务方面,那么你可以有一些外部进程,使您的网站在某个区间的请求得到各地的AppDomain回收 - 所以,如果网站循环可以保证有再次回升X分钟内自动启动。

If you can handle the persistence and transactional aspects of your thread being killed at any time, then you can get around the AppDomain recycling by having some external process that makes a request on your site at some interval - so that if the site is recycled you are guaranteed to have it start back up again automatically within X minutes.

同样,这通常是一个坏主意。

Again, this is typically a bad idea.

编辑:这里是行动这种技术的一些例子:

Here are some examples of this technique in action:

<一个href=\"http://web.archive.org/web/20090504010733/http://dev.communityserver.com/forums/t/459942.aspx\">Community服务器:使用Windows服务主场迎战后台线程在预定的时间间隔运行code
<一href=\"http://professionalaspnet.com/archive/2007/09/02/creating-a-background-thread-when-the-web-site-first-starts.aspx\">Creating后台线程网站时首次启动

编辑(从遥远的未来) - 这些天我会使用迟发型

EDIT (from the far distant future) - These days I would use Hangfire.

这篇关于我可以使用线程来进行IIS上长期运行的工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆