1

我需要将文件从一个 Mvc4 应用程序发送到另一个(另一个是 Mvc4、WebApi 应用程序)。出于发送的目的,我使用了 HttpClient 的 PostAsync 方法。下面是执行发送的代码:

public class HomeController : Controller
{
    public async Task<ActionResult> Index()
    {
        var result =
            await Upload("http://localhost/target/api/test/post", "Test", System.IO.File.Open(@"C:\SomeFile", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));

        return View(result);
    }

    public async Task<String> Upload(String url, string filename, Stream stream)
    {
        using (var client = new HttpClient())
        {
            var formData = new MultipartFormDataContent();

            var fileContent = new StreamContent(stream);

            var header = new ContentDispositionHeaderValue("attachment") { FileName = filename };

            fileContent.Headers.ContentDisposition = header;

            formData.Add(fileContent);

            var result = await client.PostAsync(url, formData); // Use your url here

            return "123";
        }
    }
}

出于接收的目的,我正在使用官方 web api 教程中的示例之一,这是执行此操作的代码:

public class TestController : ApiController
{
    // POST api/values
    public Task<HttpResponseMessage> Post()
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/App_Data");


        var provider = new MultipartFormDataStreamProvider(root);


        // Read the form data and return an async task.
        var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>
        {
            if (t.IsFaulted || t.IsCanceled)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
            }

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                Trace.WriteLine("Server file path: " + file.LocalFileName);

            }

            return new HttpResponseMessage
            {
                Content = new StringContent("File uploaded.")
            };

            //var response = Request.CreateResponse(HttpStatusCode.OK);

            //return response;
        });

        return task;
    }
}

问题:

接收方成功获取文件,但是当它使用以下代码响应时:

return new HttpResponseMessage
{
    Content = new StringContent("File uploaded.")
};

发送方在 .Net 的 mscorlib 内部深处的某个地方中断。即使我await用 try / catch 包装调用,也不会处理异常。

我想保留异步实现并且不想使用同步实现,这可能吗?为什么会出现问题?有什么想法吗?

4

1 回答 1

7

仔细检查您的教程代码。我假设您正在谈论这个页面,在这种情况下,您应该使用 .NET 4.5 版本的代码(带有await),而不是旧的 .NET 4.0 示例。

ASP.NET 内部函数(HttpContext、请求、响应等)只能从请求上下文中访问。您的代码在使用ContinueWith时未指定 a ,在这种情况下,这将导致该 lambda在请求上下文之外TaskScheduler的线程池上运行。

async代码中,你根本不应该使用ContinueWith(除非你真的需要它);改用await

// POST api/values
public async Task<HttpResponseMessage> Post()
{
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);

    try
    {
        // Read the form data.
        await Request.Content.ReadAsMultipartAsync(provider);

        // This illustrates how to get the file names.
        foreach (MultipartFileData file in provider.FileData)
        {
            Trace.WriteLine(file.Headers.ContentDisposition.FileName);
            Trace.WriteLine("Server file path: " + file.LocalFileName);
        }

        return new HttpResponseMessage
        {
            Content = new StringContent("File uploaded.")
        };
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
    }
}
于 2013-06-20T12:18:07.157 回答