I worked with Asp MVC 3, and an Async controller was created in my application with some methods like:
public void ActionAsync() { AsyncManager.OutstandingOperations.Increment(); AsyncManager.Parameters["someResult"] = GetSomeResult(); AsyncManager.OutstandingOperations.Decrement(); } public JsonResult ActionCompleted(SometResultModel someResult) { return Json(someResult, JsonRequestBehavior.AllowGet); }
And now, when I work with MVC4 and Web Api, I need to create a controller with asynchronous actions, for example, in mvc 3. Currently, it looks like this:
public Task<HttpResponseMessage> PostActionAsync() { return Task<HttpResponseMessage>.Factory.StartNew( () => { var result = GetSomeResult(); return Request.CreateResponse(HttpStatusCode.Created, result); }); }
Is it a good idea to do asynchronous actions in web api like this, or is there some better way?
UPD Also, if I will use
public async Task<HttpResponseMessage> ActionAsync() { var result = await GetSomeResult(); return Request.CreateResponse(HttpStatusCode.Created, result); }
Will this full action work in the background thread? And how to make my GetSomeResult() function expected? It returns that Task<HttpResponseMessage> not expected.
source share