我正在为 Sharepoint 2010 构建自定义 WCF Rest 服务。服务的主要任务是从 Sharepoint 下载和上传一些文件。下载按预期工作,但上传大于 64Kb 的文件时出现以下错误:
The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.
堆栈跟踪 :
System.ServiceModel.Channels.HttpInput.ThrowHttpProtocolException(String message, HttpStatusCode statusCode, String statusDescription)
System.ServiceModel.Channels.HttpInput.ThrowMaxReceivedMessageSizeExceeded()
System.ServiceModel.Channels.HttpInput.GetMessageBuffer()
System.ServiceModel.Channels.HttpInput.ReadBufferedMessage(Stream inputStream)
System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(Exception& requestException)
System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, ItemDequeuedCallback callback)
System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
System.ServiceModel.PartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2()
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke()
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks()
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state)
System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
所以我的第一个猜测是通过自定义配置来增加最大消息大小,如下所示:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Project.Poc.StorageServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Project.Poc.IStorageService" behaviorConfiguration="Project.Poc.StorageServiceBehavior">
<endpoint binding="webHttpBinding" bindingConfiguration="StreamedRequestWebBinding" contract="Project.Poc.IStorageService"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="StreamedRequestWebBinding"
sendTimeout="00:05:00"
openTimeout="00:05:00"
receiveTimeout="00:05:00"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
transferMode="StreamedRequest">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
应用此之后,部署我的 WSP 并执行 IIS 重置错误仍然存在。与来自 WCF 跟踪的上述信息相同的 HTTP 400。所以不知何故我的 web.config 配置不好,因为 WCF 服务没有使用它?
上传小于 64KB 的文件按预期工作。
这也是我的 IStorageService.cs 定义:
namespace Project.Poc
{
[ServiceContract]
public interface IStorageService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/Download?path={path}")]
Stream DownloadFile(string path);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Upload?path={path}")]
void UploadFile(string path, Stream content);
}
}
还有我的 web.config 所在的 ISAPI 文件夹中的 StorageService.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="Project.Poc.StorageService, Project.Poc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d2ed6a8d86479f52" CodeBehind="StorageService.svc.cs" Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
我尝试了所有其他我在谷歌上搜索但没有运气的 web.config 变体。这可能是什么原因?任何帮助都非常受欢迎。
此外,如果您需要源代码的任何其他部分,请告诉我。我会更新我的问题。
先感谢您!