我需要将文件从一个 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 包装调用,也不会处理异常。
我想保留异步实现并且不想使用同步实现,这可能吗?为什么会出现问题?有什么想法吗?