在 asp.net 核心中调用异步操作意味着 formData 中的文件从 c# API 方法中丢失

Calling async action in asp.net core means file in formData is lost from c# API method

我可以从表单数据中读取文件并按预期保存。但是,当我执行异步操作时,流关闭并且文件不再存在。删除异步操作意味着它再次起作用。我需要执行异步查询以将文件保存在正确的位置。

    [Route("uploadFile")]
    [AllowAnonymous]
    public  async void uploadFile()
    {
        var files = HttpContext.Request.Form.Files;

        if (files != null)
        {
            var file = files.FirstOrDefault();
            var fileName = file.FileName; 

            using (var input = file.OpenReadStream())
            {
                  var id = "someId";
                  await Task.Delay(TimeSpan.FromSeconds(2));
                  // after doing this async action, the file and stream are no longer accessible. 
                  ...

我将此作为问题发布给 asp.net 核心团队并得到了答案 here

Don't use async void in your controller action. Use async Task instead so that the framework can wait until you are done.

我没想到 return 类型会影响此功能,但好吧,现在我知道了。