C# – Bad Request Error 400 – WCF Client

cwcf

I have a WCF Web Service Running on my IIS. It takes data from client and inserts in the Database. My Web Service requies an object of complex Data type Players. In the Client I create an object of Players and pass it to the Web Service to do the Work.

In my client console app i am doing

string strBaseAddress = ConfigurationSettings.AppSettings["WEBSERVICE"];
Uri baseAddress = new Uri(strBaseAddress);
WebServiceHost host = new WebServiceHost(typeof(TrialCenterAPI), baseAddress);
host.open
// Doing Work
host.close

This App is working fine.

Now when I wrote another client by adding a service reference to do the same work. It gives me the 400 Bad Request Error every time.

I created object of the API Client which was generated automatically, then called the appropriate method of web service and passed the object of Players to it. I tried googling and i didn't get any solution., I increased the Max size in Server as well as Client Web Config.

The client Web Config uses Custom Binding as follows:

<system.serviceModel>
      <bindings>
        <customBinding>
          <binding name="WebHttpBinding">
            <textMessageEncoding 
                 maxReadPoolSize="2147483647" 
                 maxWritePoolSize="2147483647"
                 messageVersion="None" writeEncoding="utf-8" >
              <readerQuotas maxDepth="2147483647" 
                     maxStringContentLength="2147483647" 
                     maxArrayLength="2147483647"
                     maxBytesPerRead="2147483647" 
                     maxNameTableCharCount="2147483647" />
            </textMessageEncoding>
            <httpTransport 
                 maxReceivedMessageSize="2147483647" 
                 maxBufferSize="2147483647"/>
          </binding>
        </customBinding>
      </bindings>
      <client>
          <endpoint name="WebHttpBinding"  
              address="Service Address/"
              binding="customBinding" 
              bindingConfiguration="WebHttpBinding"
              contract="TestClientAPI.API" />
      </client>
   </system.serviceModel>

Any help will be highly appreciated.

Update:

I found one thread in msdn which says

"This is a limitation of the web programming model itself. Unlike SOAP endpoints (i.e., those with BasicHttpBinding, WSHttpBinding, etc) which have a way to expose metadata about itself (WSDL or Mex) with information about all the operations / parameters in the endpoint, there's currently no standard way to expose metadata for a non-SOAP endpoint – and that's exactly what the webHttpBinding-based endpoints are. In short, the WCF Test Client won't be useful for web-based endpoints. If some standard for representing web-style endpoints emerges when WCF ships its next version, we'll likely update the test client to support it, but for now there's none widely adopted.

One alternative you can try (although definitely not as simple / handy) is to create the test client yourself in code using the ChannelFactory class (using the same contract / binding / behavior used in the service (see example below). You can then use some network capture/replay tool such as Fiddler (www.fiddlertool.com) to send additional requests to the service."

Link is : http://social.msdn.microsoft.com/forums/en-US/wcf/thread/deabd25b-a219-4e95-9826-d40dc2f75543

And I am looking for the solution of this exact problem. I am able to consume service using the ChannelFactory class or Fiddler. I am not being able to use the Service client. I guess there is still no progress in this issue in WCF model.

One more thing, I dont have in my web.config at server or app.config in client. I added this in the Server Web.config and then I got a different error "The type initializer for 'System.ServiceModel.ClientBase`1' threw an exception." Also i removed the webHttpBinding Settings from config.

Best Answer

First of all, the webHttpBinding (REST-style) does not use any of those settings - those are only for SOAP-based bindings like wsHttpBinding or basicHttpBinding.

For WCF REST style bindings, you need to make sure the IIS can support larger transfers - by default, the IIS max message size to higher values than it is by default.

Check your web.config - do you have an entry like this?

<system.web>
     ......
     <httpRuntime maxRequestLength="32678"/>  
     ......
</system.web>

This is a limit in KB - the default is 4096 (4 MB) - you might need to increase that to a higher value.

If it's not the message size that causes the problem, then maybe your service is causing an exception? What exactly are you doing? Uploading or downloading files or something?

Marc

Related Topic