我正在通过SvcUtil.exe
生成类文件来以编程方式调用WCF Web服务。
一切正常,但是当我尝试发送图像(实际上是一个大小超过200KB的文件)时,出现此错误。
我知道我需要将maxReceivedMessageSize
和maxBufferPoolSize
增加到最大,才能将大文件发送到WCF服务。因此,执行此操作的XML版本如下所示:
<binding name="SampleBinding" allowCookies="true" messageEncoding="Mtom" maxReceivedMessageSize="20000000" maxBufferSize="20000000" maxBufferPoolSize="20000000">
<readerQuotas maxDepth="32" maxArrayLength="200000000" maxStringContentLength="200000000"/>
<security mode="None">
<transport proxyCredentialType="Basic"/>
</security>
</binding>
在这个应用程序中,由于我使用的是SvcUtil生成的类文件,因此需要以编程方式调用WCF服务方法。
我做到了
var binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = binding.MaxBufferPoolSize = long.MaxValue;
binding.AllowCookies = true;
binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
binding.ReaderQuotas.MaxArrayLength = binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = binding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
binding.ReaderQuotas.MaxDepth = int.MaxValue;
webAPI = new WebAPIClient(binding, new EndpointAddress(soapUrl));
XML版本确实运行良好,但是C#版本由于以下堆栈跟踪而失败:
处理Web请求时出错:状态码 413(RequestEntityTooLarge):请求实体太大
编辑
已联系我使用自定义绑定。下面是我的自定义绑定代码:
CustomBinding binding = new CustomBinding()
{
Name = "SampleBinding",
ReceiveTimeout = new TimeSpan(0, 0, 10, 0, 0),
SendTimeout = new TimeSpan(0, 0, 10, 0, 0),
};
var element1 = new TextMessageEncodingBindingElement()
{
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxDepth = 2147483647,
MaxStringContentLength = 2147483647,
MaxArrayLength = 2147483647,
MaxBytesPerRead = 2147483647,
MaxNameTableCharCount = 2147483647
}
};
var element2 = new HttpTransportBindingElement()
{
ManualAddressing = false,
MaxReceivedMessageSize = 2147483647,
AllowCookies = false,
AuthenticationScheme = System.Net.AuthenticationSchemes.None,
BypassProxyOnLocal = false,
MaxBufferSize = 2147483647,
ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.None,
TransferMode = TransferMode.Buffered,
UseDefaultWebProxy = true
};
var element3 = new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, System.Text.Encoding.UTF8);
binding.Elements.Add(element1);
binding.Elements.Add(element2);
binding.Elements.Add(element3);
webAPI = new WebAPIClient(binding, new EndpointAddress(soapUrl));
处理Web请求时出错:状态码 415(UnsupportedMediaType):无法处理消息,因为 内容类型为'application / soap + xml; charset = utf-8'不是 预期的类型为'text / xml; charset = utf-8'。
我不知道怎么了。你能帮我吗?
答案 0 :(得分:0)
我刚刚找到了一个答案,并在此处向可能与我有相同问题的未来用户发布了答案:
我的客户电话
webAPI = new WebAPIClient(new BasicHttpBinding(), new EndpointAddress(_urlString);
WCF服务的服务器配置
<endpoint address="url/WebAPI.svc" binding="basicHttpBinding" contract="MyContract.IWebAPI" />
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
如您所见,在这里我没有提及绑定配置的任何名称,它就像一个超级按钮。
由this SO answer启发的答案