无法向WCF服务发送超过13 Mb的内容(请求长度超过最大限制)

3

我无法向WCF服务发送超过13 Mb的数据。我收到了请求长度超出最大限制异常。

这里是CreateServiceHost实现:

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{

 IssuedSecurityTokenParameters itp = new IssuedSecurityTokenParameters(
                   "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1");

            itp.IssuerAddress = new EndpointAddress(ConfigManager.ActAsSTS);
            itp.IssuerMetadataAddress = new EndpointAddress(ConfigManager.ActAsSTS + "/mex");

            // Create the security binding element
            SecurityBindingElement sbe = SecurityBindingElement.CreateIssuedTokenForCertificateBindingElement(itp);
            sbe.MessageSecurityVersion =
                MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;


            // Create the HTTP transport binding element
            HttpTransportBindingElement httpBe = new HttpTransportBindingElement();
           httpBe.MaxReceivedMessageSize = httpBe.MaxBufferPoolSize = Constants.MaxFileSize;


           TextMessageEncodingBindingElement encodingElement = new TextMessageEncodingBindingElement();
            XmlDictionaryReaderQuotas quotas = encodingElement.ReaderQuotas;
            quotas.MaxArrayLength = quotas.MaxBytesPerRead = quotas.MaxStringContentLength = quotas.MaxNameTableCharCount = quotas.MaxDepth = (int)Constants.MaxFileSize;

            // Create the custom binding using the prepared binding elements
            CustomBinding binding = new CustomBinding(sbe, encodingElement, httpBe);


            EndpointAddress endpointAddress = new EndpointAddress(new Uri(ConfigManager.BaseAddress));
            ServiceEndpoint endpoint = new ServiceEndpoint(contractDescription, binding, endpointAddress);
            host.Description.Endpoints.Add(endpoint);


            host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My,
                X509FindType.FindByThumbprint, ConfigManager.ServiceCertificateThumbprint);
}

常量MaxFileSize = 20971520(20 Mb = 20 * 1024 * 1024)

所有必要的设置MaxReceivedMessageSize,MaxBytesPerRead...均已设置。

此外,在web.config文件中还有<httpRuntime maxRequestLength="20480"/> 设置。


1
这只是一个猜测,因为我很久以前就没有使用过WCF了。但是,由于您正在使用HTTP作为传输协议,所以消息是否被base64编码,导致实际数据的13 MB被编码成20 MB?文档中没有提到这样的情况,而且我认为base64通常会导致大小增加30%,所以可能不是这个原因。 - Lance U. Matthews
1个回答

0

如果我理解你想要实现的功能,你想创建一个接收大文件上传的WCF服务。

如果是这样,也许使用带有流的WCF绑定可以帮助你实现目标。

一段时间以前,我编写了一个类似于以下配置的服务,用于接受大文件上传:

<netTcpBinding>
 <binding name="netTcpStreaming" sendTimeout="00:15:00" transferMode="Streamed" maxReceivedMessageSize="2147483648">
          <security mode="None" />
  </binding>
</netTcpBinding>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接