地火和ASP.NET MVC忘记 [英] Fire and Forget with ASP.NET MVC

查看:81
本文介绍了地火和ASP.NET MVC忘记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找最佳实践的信息的射后不理asp.net的MVC行动......基本上我想要一个手机客户端拨打电话;服务器启动异步任务;然后返回到所述移动客户端尽可能快

I'm looking for info on best practices for a fire and forget asp.net mvc action ... essentially I want a mobile client to make a call; the server start an async task; and then return to the mobile client as fast as possible.

但我要确保,假设没有例外,异步任务成功完成。有明显的一些不同的选择:

But I want to make sure that, assuming no exceptions, the async task will complete successfully. There's obviously a few different options:


  • 请新的Thread

  • 在线程池队列中的工作项

  • 开始一个异步调用的委托

  • 启动任务

我承担的任务将是最好的选择在这里,但想从SO得到的想法。

I assume the Task would be the best option here, but wanted to get thoughts from SO.

<青霉>编辑:的基于几个已经答案澄清:客户端不需要响应。我想HTTP请求作为服务器开始异步任务尽可能快尽快完成。我知道在客户端异步模式,但是我想限制时间的移动设备需要保持打开的连接的数量。此外,希望避免具有单独的过程,轮询或被推的消息(经由队列,总线等),因为这是矫枉过正。我只是想在一个数据库中记录的东西,客户并不需要保持连接直到IO完成。

to clarify based on a few of the answers already: The client doesn't need a response. I want the HTTP request to complete as fast as possible as soon as the server begins the async task. I know about async patterns on the client, however I want to limit the amount of time the mobile device needs to maintain a connection open. Also, want to avoid having a separate process which polls or is pushed a message (via queue, bus, etc.) because that's overkill. I just want to log something in a database, the client doesn't need to remain connected until that IO is finished.

推荐答案

我知道这是一个老问题,但这里是我拿这样的事情,因为这是非常值得的,因为我接受的答案不同意。

I know this is an old question, but here's my take on such things, for what it's worth, since I disagree with the accepted answer.

因为你不感兴趣,等待你的异步操作完成你并不需要一个 AsyncController 。所以回答你的问题相对于事物的MVC一面是:没关系。你可以做你的工作任何方式和刚刚揭开序幕的处理,并返回任何你想要的结果一个普通的旧动作。

You don't need an AsyncController because you are not interested in waiting for your async operations to complete. So the answer to your question with respect to the MVC side of things is: it doesn't matter. You can do your work any which way and have just a regular old action that kicks off the process and returns whatever result you want.

你问题的第二部分是真的更有意义。你要确保没有什么会发生因为你从网络进程启动它们,假设一个任务本身并不抛出异常的异步任务。这个问题的答案取决于你的可靠性要求。

The second part of your question is really more relevant. You want to make sure nothing is going to happen to your async tasks given that you've started them from your web process, assuming a task itself does not throw an exception. The answer to this depends on your reliability requirements.

您提到,您不希望一个独立的进程,这限制了你的选择。你的任务将与Web应用程序相同的应用程序域中运行。如果有什么事情会断开应用程序域或过程中,您的任务会死,有可能在一个陌生的国家。这不一定甚至未处理的异常。 IIS可以被设置为应用程序自动回收不时或在某些条件。或者,如果你发布新的code或bin目录碰任何东西,你的应用程序域将被拆除,所有的请求都完成之后,和一个新的开始。如果这些案件是一个表明,塞你,那么你别无选择,只能将你的任务进程外,并与一些消息进行通信。

You mentioned that you don't want a separate process, and this limits your options. Your tasks will be running in the same app domain with your web application. If anything brings down the app domain or the process, your tasks will die, potentially in a strange state. This isn't necessarily even from unhandled exceptions. IIS can be set to automatically recycle an application from time to time or in certain conditions. Or if you release new code or touch anything in the bin directory, your app domain will be torn down after all requests are finished, and a new one is started. If these cases are a show-stopper for you, then you have no choice but to move your tasks out of process and communicate with some sort of messaging.

如果您不担心IIS杀了你,你还是要自己操心。从其它后台任务未处理的异常会带来下跌过程中,如果你不最后机会处理其与 AppDomain.UnhandledException 事件。在使用任务并行库,任务与你不要观察等待 ING他们或观看结果例外的属性,如果你不这样做最后机会观察他们在 TaskScheduler.UnobservedTaskException 事件。

If you are not worried about IIS killing you, you still have to worry about yourself. Unhandled exceptions from other background tasks will bring down the process if you don't last-chance handle them with the AppDomain.UnhandledException event. In the case of using the Task Parallel Library, Tasks with exceptions that you don't observe by Waiting on them or viewing the Result or Exception properties will bring down the process if you don't last-chance observe them in the TaskScheduler.UnobservedTaskException event.

另外一个值得注意的是,用于您的后台操作的线程池的线程将无法在这段时间内,以服务为你的web应用程序的请求。你可以在游泳池管理最大线程数,或者替代启动一个新线程。或者,如果你正在使用TPL使用默认的调度,安排与 LongRunning 提示,有效地获得一个新线程的任务。

A further note is that any ThreadPool threads used for your background operations will not be able to serve requests for your web application during that time. You could manage the max threads in the pool, or instead start a new Thread. Or if you're using TPL with the default scheduler, schedule the task with the LongRunning hint to effectively gain a new thread.

这篇关于地火和ASP.NET MVC忘记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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