我无法向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"/>
设置。