receiving 413 and 400 errors for WCF service

66 Views Asked by At

I am coding a WCF service using json that is currently executing in a console app for debugging/testing. Once completed this WCF service will be executing as a Windows Service. The WCF service will be consumed by a Windows Form app and an Android app.

My latest change added 2 operation contracts that will pass image data in request and reply messages.

During testing of these new operation contracts, if I attempt to pass a large image file, I receive this exception:

'The remote server returned an error: (413) Request Entity Too Large.'

In researching this exception I found lots of threads explaining how to correct it. During my initial debugging/testing, I was not specifying an endpoint in my app.config file. To add the basicHttpBinding information to increase the size limits, I found that I also needed to add an endpoint. Once I did this, I started receiving this exception for ALL of my operation contracts:

System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'

I do not understand why I receiving this 400 Bad Request exception.

Here is the servicemodel section from my app.config.

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="MyBasic" maxReceivedMessageSize="2147483647" 
                 maxBufferPoolSize="2147483647" maxBufferSize="2147483647" />
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="StampTrackerServiceLibrary.StampTrackerService">
        <endpoint address="http://LocalHost:9091/StampTrackerService" binding="basicHttpBinding"
          bindingConfiguration="MyBasic" contract="StampTrackerServiceLibrary.IStampTrackerServiceLibrary"/>
      </service>
    </services>
  </system.serviceModel>

Can anyone provide any assistance in determining what I am doing incorrectly?

I have researched both 413 and 400 exceptions and attempted to solve my issues.

1

There are 1 best solutions below

0
Steve On

Based on this post from 2012

[https://social.msdn.microsoft.com/Forums/vstudio/en-US/8ae01b2c-842c-4a79-a0da-0d635ed9d335/wcf-http-400-bad-request?forum=wcf][1]

I changed my config file to this and things started working.

<system.serviceModel>
  <bindings>
    <webHttpBinding>
      <binding name="MyWeb" maxReceivedMessageSize="2147483647"
               maxBufferPoolSize="2147483647" maxBufferSize="2147483647" />
    </webHttpBinding>
  </bindings>
  <behaviors>
    <serviceBehaviors>
      <behavior name="MyBeh">
        <serviceMetadata httpGetEnabled="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service name="StampTrackerServiceLibrary.StampTrackerService" behaviorConfiguration="MyBeh">
      <endpoint address=" " bindingConfiguration="MyWeb"
          binding="webHttpBinding" contract="StampTrackerServiceLibrary.IStampTrackerServiceLibrary" />
      <endpoint address="mex "
          binding="webHttpBinding" bindingConfiguration="" contract="IMetadataExchange" />
    </service>
  </services>
</system.serviceModel>