Request.Content.ReadAsMultipartAsync永远不会返回 [英] Request.Content.ReadAsMultipartAsync never returns

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

问题描述

我有使用ASP.NET Web API编写的系统的API,我试图把它扩大到允许上传的图片。我做了一些谷歌上搜索,发现如何推荐的方式接收使用MultpartMemoryStreamProvider和文件的一些异步方法,但我对ReadAsMultipartAsync的await永不再来。

下面是code:

  [HttpPost]
公共异步任务< Htt的presponseMessage> LowResImage(INT ID)
{
    如果(!Request.Content.IsMimeMultipartContent())
    {
        抛出新的Htt presponseException(的HTTPStatus code.UnsupportedMediaType);
    }    VAR提供商=新MultipartMemoryStreamProvider();    尝试
    {
        等待Request.Content.ReadAsMultipartAsync(供应商);        的foreach(在provider.Contents VAR项)
        {
            如果(item.Headers.ContentDisposition.FileName!= NULL)
            {            }
        }        返回Request.CreateResponse(的HTTPStatus code.OK);
    }
    赶上(System.Exception的E)
    {
        返回Request.CreateErrorResponse(的HTTPStatus code.InternalServerError,E);
    }
}

我可以一路过关斩将一步:

 等待Request.Content.ReadAsMultipartAsync(供应商);

在这一点永远不会完成。

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

更新

我试图用卷曲张贴到这一行动,命令如下:

  C:\\卷曲>卷曲-i -F FILEDATA = @ C:\\ LowResExample.jpg的http://本地主机:8000 /原料药/照片/ 89 / LowResImage

我也用下面的HTML张贴到行动,以及尝试,同样的事情发生了:

 <形式方法=POSTACTION =HTTP://本地主机:8000 /原料药/照片/ 89 / LowResImageENCTYPE =的multipart / form-data的>
    <输入类型=文件名称=文件上传/>
    <输入类型=提交名称=提交/>
< /表及GT;


解决方案

我跑进在.NET 4.0中类似的东西(没有异步/等待)。使用调试器的线程堆栈,我可以告诉大家,ReadAsMultipartAsync被发射任务到同一个线程,所以它会死锁。我做了这样的事情:

 的IEnumerable< HttpContent>部分= NULL;
Task.Factory
    .StartNew(()=方式>零件= Request.Content.ReadAsMultipartAsync()Result.Contents,
        CancellationToken.None,
        TaskCreationOptions.LongRunning,//保证单独的线程
        TaskScheduler.Default)
    。等待();

该TaskCreationOptions.LongRunning参数对我来说关键,因为没有它,通话将继续启动任务到同一个线程。你可以尝试使用像下面的伪code的东西,看它是否为你的作品在C#5.0:

 等待TaskEx.Run(异步()=>等待Request.Content.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.

Here is the code:

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

I can step through all the way to:

await Request.Content.ReadAsMultipartAsync(provider);

at which point it will never complete.

What is the reason why my await never returns?

Update

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

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>

解决方案

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();

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天全站免登陆