MVC4的WebAPI进程启动进程、WebAPI

2023-09-03 12:55:32 作者:冷拥

我有一个的 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:

https://m.xsw88.com/allimgs/daicuo/20230903/3855.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.