WCF REST 400 Bad Request

Today while I was writing my WCF Rest Client, I was getting error as "400 Bad Request". Finally I realized the culprit of error was service name in web.config file. In order to resolve this error, make sure service name is matching with your service markup's service name.

If you have servicename1.svc, right click on that file and select "View MarkUp". Copy Service value from that and paste it into webconfig's services service name.

Service1.svc MarkUp:

<%@ ServiceHost Language="C#" Debug="true" Service="MyServiceNameSpace.Service1" CodeBehind="Service1.svc.cs" %>


Web.config Services Section:
<services>
   <service name="MyServiceNameSpace.Service1">
     <endpoint address="" behaviorConfiguration="restService"
           contract="MyServiceNameSpace.IService1" binding="webHttpBinding"/>
     <endpoint address="mex" contract="MyServiceNameSpace.IService1"  binding="mexHttpBinding" />
   </service>
</services>

Working Rest Configuration:


<?xml version="1.0"?>
<configuration>     
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>   
    <behaviors>     
              <endpointBehaviors>
                     <behavior name="restService">
                           <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/>
                     </behavior>        
              </endpointBehaviors>
              <serviceBehaviors>
                     <behavior> 
                           <serviceMetadata httpGetEnabled="true" />
                     </behavior>
              </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"  aspNetCompatibilityEnabled="true" />
       <services>
              <service name="MyServiceNameSpace.Service1">
                     <endpoint address=""  behaviorConfiguration="restService"
contract="MyServiceNameSpace.IService1" binding="webHttpBinding"/>
                     <endpoint address="mex" contract="MyServiceNameSpace.IService1" inding="mexHttpBinding" />
              </service>  
       </services>
  </system.serviceModel>
</configuration>

Comments

Anonymous said…
Thank you so much!!! I had the exact same problem and this helped me finally get it fixed after 8+ hours of Google searches and code rewriting!

Popular posts from this blog

Windows Azure Package Build Error: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Resource ID : 1. The request limit for the database is 180 and has been reached.

How to get Client's Location using IPAddress