Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 WebApi process launcher

I have a MVC4 API REST trying to launch a process in a new thread. I am working with framework 4.5 and trying to use sync and await clausules.

My code looks like:

[AcceptVerbs("POST")]
public HttpResponseMessage Launch(string id)
{
    runProcessAsync(id);   // 1                                                        

    return Request.CreateResponse(HttpStatusCode.Accepted); // 2
}

protected async void runProcessAsync(string id)
{
    int exitcode = await runProcess(id);  // 3
    string exitcodesz = string.Format("exitcode: {0}", exitcode);  // 4
    // do more stuff with exitcode
}

protected Task<int> runProcess(string id)
{
    var tcs = new TaskCompletionSource<int>();

    var process = new Process
    {
        StartInfo = { FileName = @"C:\a_very_slow_task.bat" },
            EnableRaisingEvents = true
        };
        process.Exited += (sender, args) => tcs.SetResult(((Process)sender).ExitCode);            

        process.Start();

        return tcs.Task;
    }  
}

Ideally, someone will perform a api rest call ( /task/slowtask/launch ) with POST verb and expect a 202 (accepted) very fast.

Using Fiddler Web Debugger I make the request, the code enters into Launch (// 1), then goes to the await (// 3), the slow task is created and started and a Accepted is returned (// 2). But at this point, Fiddler doesn't show the 202 result. See attached image:

http://imageshack.us/photo/my-images/703/fiddler1.png/

When the slow task ends, the code continues capturing the exitcode (// 4) and then the 202 is captured into Fiddler.

That is very weird, because I did the return long time ago. What I am missing? how can I change that code in order to return a 202 very fast and forget about the task.

Note: I know how to do this with non framework 4.5 features, I am trying to learn how to use async / await.

like image 750
Jordi Avatar asked Feb 15 '13 13:02

Jordi


1 Answers

I think this is because runProcessAsync() runs on the request's synchronization context. If you don't want that, you can use Task.Run(() => runProcessAsync(id));. But be careful with this, because IIS can recycle your AppDomain during that time, so there is a chance runProcessAsync() won't complete.

like image 110
svick Avatar answered Sep 28 '22 13:09

svick