MVC4 + async/await + return response before action completes

前端 未结 3 2119
忘掉有多难
忘掉有多难 2020-12-24 14:55

In my MVC4 app I need to add a controller for uploading and processing large files. Immediately after the file is uploaded I need to start async processing of that file and

3条回答
  •  天命终不由人
    2020-12-24 15:11

    You have an error in your code. You must await the call to TestAsync in your action. If you don't, it simply returns a "Task" object without running anything.

    public async Task Test()
    {
        await TestAsync();
        return View("Test");
    }
    

    and the correct way to sleep in an async method is to call

    await Task.Delay(10000);
    

    You have to change the signature of TestAsync: you should not mark a method async if it returns void. It must return Task instead. Returning void is reserved for compatibility with .net events.

    public async Task TestAsync()
    {
        await LongRunning();
    }
    

    The method body is the same.

提交回复
热议问题