我遇到了WCF网络API的真正问题。
我有一个上传文件并保存到磁盘的简单方法。我似乎设置了所有正确的参数,但在我尝试上传2mb文件时收到上述错误消息。
服务器代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
HttpServiceHostFactory _factory = new HttpServiceHostFactory();
var config = new HttpConfiguration()
{
EnableTestClient = true,
IncludeExceptionDetail = true,
TransferMode = System.ServiceModel.TransferMode.Streamed,
MaxReceivedMessageSize = 4194304,
MaxBufferSize = 4194304
};
_factory.Configuration = config;
RouteTable.Routes.Add(new ServiceRoute("api/docmanage", _factory, typeof(WorksiteManagerApi)));
}
客户端:
HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.MaxRequestContentBufferSize = 4194304;
var byteArray =
Encoding.ASCII.GetBytes(ConnectionSettings.WebUsername + ":" + ConnectionSettings.WebPassword);
HttpClient httpClient = new HttpClient(httpClientHandler);
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
httpClient.BaseAddress = new Uri(ConnectionSettings.WebApiBaseUrl);
httpClient.MaxResponseContentBufferSize = 4194304;
...
multipartFormDataContent.Add(new FormUrlEncodedContent(formValues));
multipartFormDataContent.Add(byteArrayContent);
var postTask = httpClient.PostAsync("api/docmanage/UploadFile", multipartFormDataContent);
然后,在服务器上:
[WebInvoke(Method = "POST")]
public HttpResponseMessage UploadFile(HttpRequestMessage request)
{
// Verify that this is an HTML Form file upload request
if (!request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
// Create a stream provider for setting up output streams
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider();
// Read the MIME multipart content using the stream provider we just created.
IEnumerable<HttpContent> bodyparts = request.Content.ReadAsMultipart(streamProvider);
foreach (var part in bodyparts)
{
switch (part.Headers.ContentType.MediaType)
{
case "application/octet-stream":
if (part.Headers.ContentLength.HasValue)
{
// BLOWS UP HERE:
var byteArray = part.ReadAsByteArrayAsync().Result;
if (null == fileName)
{
throw new HttpResponseException(HttpStatusCode.NotAcceptable);
}
else
{
uniqueFileId = Guid.NewGuid().ToString();
string tempFilename = Path.GetTempPath() + @"\" + uniqueFileId + fileName;
using (FileStream fileStream = File.Create(tempFilename, byteArray.Length))
{
fileStream.Write(byteArray, 0, byteArray.Length);
}
}
}
break;
}
}
}
有什么想法吗?我正在使用web api的最新预览....我注意到支持文档中缺少很多但似乎有一些缓冲限制,我无法找到如何指定或被忽略.. ...
答案 0 :(得分:4)
HttpContent类的(缺少)文档中没有明确的一点是默认的内部缓冲区是64K,因此任何非流的内容都会抛出一旦内容超出时看到的异常64KB。
解决方法是使用以下方法:
part.LoadIntoBufferAsync(bigEnoughBufferSize).Result();
var byteArray = part.ReadAsByteArrayAsync().Result;
我认为HttpContent类的缓冲区的64K限制是为了防止在服务器上发生很多内存分配。我想知道你是否会更好地将字节数组内容作为StreamContent传递?这样它仍然可以工作,而不必增加HttpContent缓冲区大小。
答案 1 :(得分:1)
您是否在web.config中设置了maxRequestLength:
<httpRuntime maxRequestLength="10240" />
答案 2 :(得分:0)
我在WCF WinAPI上遇到了类似的问题几天,我试图发布一个12Mb的文件,我无法弄清楚发生了什么。我的服务器端是IIS中托管的WCF服务;我的问题不是WCF设置,而是Toni提到的。在IIS中托管时,请记住增加此设置。 MS文档表明默认值是4Mb,这解释了为什么我可以发布400Kb的文件。
希望这有助于其他人遇到同样的麻烦。