MVC4的WebAPI进程启动 [英] MVC4 WebApi process launcher

查看:227
本文介绍了MVC4的WebAPI进程启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的 MVC4 REST API 的试图启动一个进程在一个新的线程。我正与框架4.5,并尝试使用同步,等待clausules。

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.

我的code是这样的:

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;
    }  
}

在理想情况下,有人会执行API REST调用(/任务/ slowtask /发射)与POST谓词和期望202(接受)非常快的。

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

使用招网络调试我提出的要求,code进入启动(// 1),然后去计谋(// 3),缓慢的任务是创建并启动和接受返回( // 2)。但在这一点上,提琴手​​不显示202的结果。见所附图片:

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:

<一个href="http://imageshack.us/photo/my-images/703/fiddler1.png/">http://imageshack.us/photo/my-images/703/fiddler1.png/

在缓慢的任务结束后,code继续捕捉退出code(// 4),然后在202被捕获到提琴手。

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

这是非常奇怪的,因为我做了回很久以前。我缺少的是什么?我如何改变才能返回202非常快,忘了任务code。

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.

注意:我知道如何与非框架4.5的功能做到这一点,我想学习如何使用异步/计谋。

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

推荐答案

我想这是因为 runProcessAsync()运行在请求的同步环境。如果你不希望出现这种情况,你可以使用 Task.Run(()=&GT; runProcessAsync(ID)); 。但要小心,因为IIS可以在这段时间内回收您的AppDomain,所以有机会的话 runProcessAsync()将无法完成。

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.

这篇关于MVC4的WebAPI进程启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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