Request.Content.ReadAsMultipartAsync 从不返回 [英] Request.Content.ReadAsMultipartAsync never returns

查看:31
本文介绍了Request.Content.ReadAsMultipartAsync 从不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 ASP.NET Web Api 编写的系统 API,我正在尝试扩展它以允许上传图像.我已经做了一些谷歌搜索,发现了如何使用 MultpartMemoryStreamProvider 和一些异步方法来接受文件的推荐方法,但我对 ReadAsMultipartAsync 的等待永远不会返回.

I have an API for a system written using the ASP.NET Web Api and am trying to extend it to allow images to be uploaded. I have done some googling and found how the recommended way to accept files using MultpartMemoryStreamProvider and some async methods but my await on the ReadAsMultipartAsync never returns.

代码如下:

[HttpPost]
public async Task<HttpResponseMessage> LowResImage(int id)
{
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    var provider = new MultipartMemoryStreamProvider();

    try
    {
        await Request.Content.ReadAsMultipartAsync(provider);

        foreach (var item in provider.Contents)
        {
            if (item.Headers.ContentDisposition.FileName != null)
            {

            }
        }

        return Request.CreateResponse(HttpStatusCode.OK);
    }
    catch (System.Exception e)
    {
        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
    }
}

我可以逐步完成:

await Request.Content.ReadAsMultipartAsync(provider);

在这一点上它永远不会完成.

at which point it will never complete.

我的 await 永远不会返回的原因是什么?

What is the reason why my await never returns?

我正在尝试使用 curl POST 到此操作,命令如下:

I am attempting to POST to this action using curl, the command is as follows:

C:cURL>curl -i -F filedata=@C:LowResExample.jpg http://localhost:8000/Api/Photos/89/LowResImage

我也尝试过使用以下 html 来 POST 到操作,并且发生了同样的事情:

I have also tried using the following html to POST to the action as well and the same thing happens:

<form method="POST" action="http://localhost:8000/Api/Photos/89/LowResImage" enctype="multipart/form-data">
    <input type="file" name="fileupload"/>
    <input type="submit" name="submit"/>
</form>

推荐答案

我在 .NET 4.0 中遇到了类似的问题(没有 async/await).使用调试器的线程堆栈,我可以知道 ReadAsMultipartAsync 正在将任务启动到同一个线程上,因此它会死锁.我做了这样的事情:

I ran into something similar in .NET 4.0 (no async/await). Using the debugger's Thread stack I could tell that ReadAsMultipartAsync was launching the task onto the same thread, so it would deadlock. I did something like this:

IEnumerable<HttpContent> parts = null;
Task.Factory
    .StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents,
        CancellationToken.None,
        TaskCreationOptions.LongRunning, // guarantees separate thread
        TaskScheduler.Default)
    .Wait();

TaskCreationOptions.LongRunning 参数对我来说很关键,因为没有它,调用将继续在同一线程上启动任务.您可以尝试使用类似以下伪代码的内容,看看它是否适用于 C# 5.0:

The TaskCreationOptions.LongRunning parameter was key for me because without it, the call would continue to launch the task onto the same thread. You could try using something like the following pseudocode to see if it works for you in C# 5.0:

await TaskEx.Run(async() => await Request.Content.ReadAsMultipartAsync(provider))

这篇关于Request.Content.ReadAsMultipartAsync 从不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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