public class AvaterController : BaseApiController { [HttpPost] public async Task<IHttpActionResult> UploadAvater(int userId) { AvatarBLL pictureBLL = new AvatarBLL(this.Request); await pictureBLL.UploadAvatar(userId); return Ok(); } //其他Action } public class AvatarBLL { private HttpRequestMessage HttpRequestMessage; public AvatarBLL(HttpRequestMessage httpRequestMessage) { this.HttpRequestMessage = httpRequestMessage; } public async Task UploadAvatar(int userId) { if (!HttpRequestMessage.Content.IsMimeMultipartContent("form-data")) { //抛异常 } //获得客户端通报随处事器的数据 List<byte> list = new List<byte>(); await HttpRequestMessage.Content.ReadAsMultipartAsync().ContinueWith(multipartContent => { if (multipartContent.IsFaulted || multipartContent.IsCanceled) { //抛异常 } foreach (var content in multipartContent.Result.Contents) { var b = content.ReadAsByteArrayAsync().Result; list.AddRange(b); } }); //其他部分(将数据存入Mongodb以及其他的业务逻辑) } }
2错误的做法
public class AvaterController : BaseApiController { [HttpPost] public IHttpActionResult UploadAvater(int userId) { AvatarBLL pictureBLL = new AvatarBLL(this.Request); pictureBLL.UploadAvatar(userId); return Ok(); } //其他Action } public class AvatarBLL { private HttpRequestMessage HttpRequestMessage; public AvatarBLL(HttpRequestMessage httpRequestMessage) { this.HttpRequestMessage = httpRequestMessage; } public void UploadAvatar(int userId) { if (!HttpRequestMessage.Content.IsMimeMultipartContent("form-data")) { //抛异常 } //获得客户端通报随处事器的数据 List<byte> list = new List<byte>(); MemoryStream ms = new MemoryStream(); try { MultipartMemoryStreamProvider mmsp = new MultipartMemoryStreamProvider(); var task = HttpRequestMessage.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(mmsp, 100000); task.Wait(); var contents = task.Result.Contents; foreach (var c in contents) { var b = c.ReadAsByteArrayAsync(); b.Wait(); list.AddRange(b.Result); } } catch (AggregateException ex) { } //其他部分(将数据存入Mongodb以及其他的业务逻辑) } }
3 错误现象:
给与第二种方法,如果客户端上传随处事的数据量(挪用UploadAvater上传的数据)小于处事端设置的缓冲区的巨细,那么可正常上传文件,如果大于处事端设置的缓冲区的巨细,则无法正常上传,调试处事端代码,当执行到task.Wait();这行语句时,客户端一直期待,,直到客户端挪用超时,永远也无法返回挪用功效,产生了死锁!!!使用HttpRequestMessage.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(mmsp, 1000);设置缓冲区巨细为1000bit。这个要领有几个重载的要领,此中一个是不显示设置缓冲区巨细,那么缓冲区巨细为默认的。
4 对第二种要领的错误点分析:
看Web api dll源码中的设置:
可以看出 默认的缓冲区区巨细为32*1024,即32K,那么上传赶过32k而不设置缓冲区巨细的情况下,为什么会发存亡锁,而将缓冲区设置赶过上传文件巨细为什么不会发存亡锁呢?岂论是否将缓冲区巨细设置的足够大,都有发存亡锁的可能。
主要的要领见上图,在要领体中有下面这段代码:
这段代码的核心要领: