状态代码 413 (RequestEntityTooLarge) / 状态代码 415 (UnsupportedMediaType)。 WCF 以编程方式

Status code 413 (RequestEntityTooLarge) / Status code 415 (UnsupportedMediaType). WCF Programmatically

我通过 SvcUtil.exe 生成 class 文件以编程方式调用 WCF Web 服务。

一切正常,但是当我尝试发送图像(实际上是大小超过 200KB 的文件)时,出现此错误。

我知道我需要将 maxReceivedMessageSizemaxBufferPoolSize 增加到最大值才能将大文件发送到 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 生成的 class 文件,因此我需要以编程方式调用我的 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# 版本失败,堆栈跟踪如下:

There was an error on processing web request: Status code 413(RequestEntityTooLarge): Request Entity Too Large

编辑

有人联系我使用自定义绑定。下面是我的自定义绑定代码:

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));

There was an error on processing web request: Status code 415(UnsupportedMediaType): Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.

我不知道怎么了。你能帮帮我吗?

我刚刚找到了一个答案并在此处发布相同的答案,以供将来可能遇到与我相同问题的用户使用:

我的客户来电

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