我变为Asp MVC 3个工作,并在我的应用程序创建异步控制器,像一些方法:天冬氨酸MVC和网络API异步控制器
public void ActionAsync()
{
AsyncManager.OutstandingOperations.Increment();
AsyncManager.Parameters["someResult"] = GetSomeResult();
AsyncManager.OutstandingOperations.Decrement();
}
public JsonResult ActionCompleted(SometResultModel someResult)
{
return Json(someResult, JsonRequestBehavior.AllowGet);
}
而现在,当我与MVC4和网络工作API,我需要像MVC 3,在当前的时间它的创建与异步操作控制器的样子:
public Task<HttpResponseMessage> PostActionAsync()
{
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
var result = GetSomeResult();
return Request.CreateResponse(HttpStatusCode.Created, result);
});
}
它是一个好主意,使在网页API异步操作这样或者有一些更好的方法是存在的?
UPD。另外,如果我将使用
public async Task<HttpResponseMessage> ActionAsync()
{
var result = await GetSomeResult();
return Request.CreateResponse(HttpStatusCode.Created, result);
}
这个完整的动作将在后台线程中工作吗?以及如何使我的GetSomeResult()
函数等待?这是Task<HttpResponseMessage>
不等待的回报。
你能告诉我这是怎么回事行动应该看起来像?我尝试过使用这个http://pastebin.com/NeRKfqDp并且我所有的'POST'查询都返回“404” – 2013-04-29 14:49:13
Factory.StartNew在这个上下文中肯定是错误的,因为你正在旋转一个额外的线程(或者调度一些额外的在线程池中工作)以及当前处理当前请求的线程。 EventModel.PrepareModel是否返回任务? – 2013-04-29 16:21:54
'EventModel.PrepareModel'是一个void函数。所以,如果'Factory.StartNew'错了,我应该用什么来发出异步请求? – 2013-04-30 07:27:40