ASP.NET中的异步任务 [英] Asynchronous task in ASP.NET

查看:129
本文介绍了ASP.NET中的异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图迫使一个ASP.NET网站,pre-装入大量的对象为在启动缓存。每个对象需要2-10秒钟产生,这意味着装载1200+对象依次将采取40分钟以上才能完成。

I’m trying to force an ASP.NET site to pre-load a large number of objects into the cache on start-up. Each objects takes 2-10 seconds to generate, which means loading 1200+ objects sequentially would take upwards of 40 minutes to complete.

我显然不希望到现场需要40分钟开机,所以才坚持在的Application_Start的pre-装载过程是不是一个真正的选择。我不能在Web应用程序的情况下(如Windows服务或外部进程)以外的移动它,因为我需要访问缓存。理论上讲,该任务将被异步执行,离开现场去了解其业务在其间。使用 BackgroundWorker的类分拆一位同事提出了一个新的线程,这听起来像一个pretty的很好的解决方案。但是,据我所看到的,特定的类真的不适合ASP.NET,但WinForms的。我担心在完成前线程可以杀死了。

I obviously don’t want to the site to take 40 minutes to boot, so just sticking the pre-loading procedure in Application_Start isn’t really an option. And I can’t move it outside the context of the web app (i.e. a Windows Service or external process) because I need to access the cache. Ideally this task would be executed asynchronously, leaving the site to go about its business in the meantime. A colleague suggested using the BackgroundWorker class to spin off a new thread, which sounds like a pretty good solution. However, as far as I can see, that particular class really isn’t intended for ASP.NET, but WinForms. I’m concerned the thread may be killed off before it completes.

  • 是BackgroundWorker的类安全的ASP.NET应用程序使用?
  • 在我有什么其他选择来解决这个问题?

更新:

我会看看到Windows AppFabric的更好的缓存,我可以从Web应用程序之外加载。然而,在此之前我必须继续前进,使用ASP.NET缓存和BackgroundWorker的类。它不工作,我已经问了一个新的问题,装入对象为从缓存中新线程

I'll look into Windows AppFabric for better caching which I can load from outside the web app. However, until then I have to go ahead and use the ASP.NET Cache and the BackgroundWorker class. It's not working, and I've asked a new question, Load objects into cache from new thread.

推荐答案

使用任务并行库(如果你使用的是.NET 4.0)。第三方物流的每个实现是专为特定的主机,并有一个用于ASP.NET,这意味着你有你自己的TaskScheduler和的SynchronizationContext符合该IIS规定,当涉及到线程和规则线程上下文。

Use the Task Parallel Library (if you are using .Net 4.0). Each implementation of the TPL is designed for the specific host, and there's one for ASP.NET, which means you have your own TaskScheduler and SynchronizationContext that complies with the rules that IIS imposes when it comes to threading and threading context.

有一个很好的MSDN文章解释diferent TPL行为这里

There's a very good MSDN article explaining the diferent TPL behaviours here.

使用TPL是,如果你熟悉的lambda防爆pressions很简单,为您节省了大量的地下$ C $了C管理线程和线程池。

Using the TPL is very simple if you are familiar with Lambda Expressions, and saves you a lot of underground code managing threads and thread pools.

这篇关于ASP.NET中的异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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