none
Status code 413 (RequestEntityTooLarge): Request Entity Too Large. WCF Programmatically RRS feed

  • Question

  • I am calling a WCF web service programmatically by generating class files through [`SvcUtil.exe`][1].

    All working fine but when I try to send an image (literally a file with size more than 200KB), I get this error.

    I know I need to increase `maxReceivedMessageSize` and `maxBufferPoolSize` to the maximum to send large files to the WCF service. Hence, the XML version of doing this will be like below:

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

    In this app, since I am using class files generated by SvcUtil, I need to call my WCF service method programmatically.

    So I did,

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

    The XML version works really well but the C# version failed with below stack trace:

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

    **EDIT**

    I have been approached to use custom binding. Below is my custom binding code:

    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'.

    I don't know what's wrong. May you help me?

      [1]: https://docs.microsoft.com/en-us/dotnet/framework/wcf/servicemodel-metadata-utility-tool-svcutil-exe
    Saturday, May 4, 2019 7:20 AM

Answers

  • I just found an answer and posting the same here for future users who might have the same issue as me:

    My client call

        webAPI = new WebAPIClient(new BasicHttpBinding(), new EndpointAddress(_urlString);

    Server Configuration for the WCF Service

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


    **As you can see I did not mention any name for the binding configuration here and it just worked like a charm.**

    Answer inspired from [this SO answer][1]


      [1]: https://stackoverflow.com/a/20667308/3682161

    - Mohamed Thaufeeq A

    Thursday, May 9, 2019 6:44 AM

All replies

  • I am calling a WCF web service programmatically by generating class files through [`SvcUtil.exe`][1].

    All working fine but when I try to send an image (literally a file with size more than 200KB), I get this error.

    I know I need to increase `maxReceivedMessageSize` and `maxBufferPoolSize` to the maximum to send large files to the WCF service. Hence, the XML version of doing this will be like below:

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

    In this app, since I am using class files generated by SvcUtil, I need to call my WCF service method programmatically.

    So I did,

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

    The XML version works really well but the C# version failed with below stack trace:

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

    **EDIT**

    I have been approached to use custom binding. Below is my custom binding code:

    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'.

    I don't know what's wrong. May you help me?

      [1]: https://docs.microsoft.com/en-us/dotnet/framework/wcf/servicemodel-metadata-utility-tool-svcutil-exe
    Saturday, May 4, 2019 5:02 AM
  • You would have had to have done a custom binding on the service-side to override the default binding settings.

    Anyway, WCF issues can be discussed at the WCF forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf


    Saturday, May 4, 2019 6:54 AM
  • You would have had to have done a custom binding on the service-side to override the default binding settings.

    Anyway, WCF issues can be discussed at the WCF forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf


    Thaks. The question was asked there in the WCF Forum site.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/75ec98e4-7f78-4d14-9a43-083af5550cd7/status-code-413-requestentitytoolarge-request-entity-too-large-wcf-programmatically?forum=wcf


    - Mohamed Thaufeeq A

    Saturday, May 4, 2019 7:21 AM
  • Can you please help me with an example or documentation site link to learn more about doing custom binding?

    - Mohamed Thaufeeq A

    Saturday, May 4, 2019 7:22 AM
  • You would have had to have done a custom binding on the service-side to override the default binding settings.

    Anyway, WCF issues can be discussed at the WCF forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf


    Updated my question with CustomBinding. Please help

    - Mohamed Thaufeeq A

    Saturday, May 4, 2019 10:10 AM
  • Can you please help me with an example or documentation site link to learn more about doing custom binding?

    - Mohamed Thaufeeq A

    https://www.itprotoday.com/microsoft-visual-studio/custom-bindings-part-i

    https://www.itprotoday.com/microsoft-visual-studio/custom-bindings-part-ii

    Saturday, May 4, 2019 10:48 AM
  • Hi Mohamed Thaufeeq,
    I can’t locate the problem for sure. I agree with your idea about the solution, which needs to increase the related property.
    Refer to the following related discussion.
    https://stackoverflow.com/questions/10122957/413-request-entity-too-large-uploadreadaheadsize#comment17660577_10123764
    Have you tried to completely copy the XML version configuration file in code due to that I noticed that your configuration created programmatically is not equivalent to the XML version configuration? And ensure that apply the configuration takes effect on both the client and the server.
    BasicHttpBinding binding = new BasicHttpBinding();
                binding.MessageEncoding = WSMessageEncoding.Mtom;
                binding.AllowCookies = true;
                binding.MaxBufferPoolSize = binding.MaxReceivedMessageSize = long.MaxValue;
                binding.MaxBufferSize = int.MaxValue;
                binding.ReaderQuotas.MaxDepth = 32;
                binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
                binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
                binding.Security.Mode = BasicHttpSecurityMode.None;
    

    Client should modify the binding configuration before instancing the client proxy class.
    Feel free to let me know if the problem still exists.
    Best Regards
    Abraham
    Monday, May 6, 2019 2:37 AM
    Moderator
  • Hi Mohamed Thaufeeq,
    I can’t locate the problem for sure. I agree with your idea about the solution, which needs to increase the related property.
    Refer to the following related discussion.
    https://stackoverflow.com/questions/10122957/413-request-entity-too-large-uploadreadaheadsize#comment17660577_10123764
    Have you tried to completely copy the XML version configuration file in code due to that I noticed that your configuration created programmatically is not equivalent to the XML version configuration? And ensure that apply the configuration takes effect on both the client and the server.
    BasicHttpBinding binding = new BasicHttpBinding();
                binding.MessageEncoding = WSMessageEncoding.Mtom;
                binding.AllowCookies = true;
                binding.MaxBufferPoolSize = binding.MaxReceivedMessageSize = long.MaxValue;
                binding.MaxBufferSize = int.MaxValue;
                binding.ReaderQuotas.MaxDepth = 32;
                binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
                binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
                binding.Security.Mode = BasicHttpSecurityMode.None;

    Client should modify the binding configuration before instancing the client proxy class.
    Feel free to let me know if the problem still exists.
    Best Regards
    Abraham

    I tried but it is not working 


    - Mohamed Thaufeeq A

    Monday, May 6, 2019 9:58 AM
  • I just found an answer and posting the same here for future users who might have the same issue as me:

    My client call

        webAPI = new WebAPIClient(new BasicHttpBinding(), new EndpointAddress(_urlString);

    Server Configuration for the WCF Service

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


    **As you can see I did not mention any name for the binding configuration here and it just worked like a charm.**

    Answer inspired from [this SO answer][1]


      [1]: https://stackoverflow.com/a/20667308/3682161

    - Mohamed Thaufeeq A

    Thursday, May 9, 2019 6:44 AM