0

I have a WCF which I get the

"The remote server returned an unexpected response: (400) Bad Request."

error just in calling a specific Method of it.

This is web.Config in server side

<configuration>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="CableContext" connectionString="metadata=res://*/CableModel.csdl|res://*/CableModel.ssdl|res://*/CableModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=pgdbserver;initial catalog=CableDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />     
 </connectionStrings>

<system.web>
<compilation debug="true" targetFramework="4.0" />

</system.web>
<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>

    </behavior>
    </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
  <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>

   </configuration>

and this is the app.config in client side

 <?xml version="1.0" encoding="utf-8"?>
   <configuration>
     <appSettings>
       <add key="UserManager" value="http://appserver:8080/SecurityServices/UserManager.asmx" />
      <add key="ClientSettingsProvider.ServiceUri" value="" />
     </appSettings>
    <system.serviceModel>
    <bindings>
      <basicHttpBinding>
          <binding name="BasicHttpBinding_ICableService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="9830400" maxBufferPoolSize="524288" maxReceivedMessageSize="9830400" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="64" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
       </binding>
      </basicHttpBinding>
     </bindings>
     <client>      
     <endpoint address="http://appserver:8080/CableDataService/CableService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICableService" contract="CableServiceReference.ICableService" name="BasicHttpBinding_ICableService" />
    </client>
   </system.serviceModel>
   <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
  <providers>
    <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
  </providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
  <providers>
    <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
  </providers>
  </roleManager>
  </system.web>
  </configuration>

and this is the code I am Calling

   public int Export(Cable cableToSave )
    {
        int result = 0;
        using (UnitOfWork unitOfWork = new UnitOfWork())
        {

                        if (cableToSave.CableProperty != null && cableToSave.CableProperty.CableApplication != null && cableToSave.CableProperty.CableApplication.State == State.Added)
                        {
                            cableToSave.CableProperty.CableApplication.CableProperties = null;
                            unitOfWork.CableApplicationRepository.Insert(cableToSave.CableProperty.CableApplication);
                        }
                        if (cableToSave.CableProperty != null && cableToSave.CableProperty.State == State.Added)
                        {
                            cableToSave.CableProperty.Cables = null;
                            unitOfWork.CablePropertyRepository.Insert(cableToSave.CableProperty);
                        }
                        if (cableToSave.State == State.Added)
                        {
                            unitOfWork.CableRepository.Insert(cableToSave);
                            result = cableToSave.Id;
                            if (cableToSave.Cores != null)
                            foreach (Core coreToSave in cableToSave.Cores)
                            {
                                unitOfWork.CoreRepository.Insert(coreToSave);
                            }
                        }                            


            unitOfWork.Save();
            return result;
        }
    }
Incredible
  • 3,495
  • 8
  • 49
  • 77
nnmmss
  • 2,850
  • 7
  • 39
  • 67
  • So, did you debug your method? Do you have any way to make sure it actually runs fine without exceptions? – nvoigt Nov 19 '13 at 06:04
  • I happens when i call the method client.Export(diffCable), i cannot enter the service – nnmmss Nov 19 '13 at 06:11
  • That's why you test it independently of WCF. Create an instance of your service class in a test project and call the method from code. See if it works without WCF first. If it does, check if your Cable parameter is properly serializable. – nvoigt Nov 19 '13 at 06:13
  • SOrry but i am new in this subject, can you give me an example of how can i test it and how can i know whether it is properly serialized? – nnmmss Nov 19 '13 at 06:17
  • You should follow the way described in the last comment. If you want an easy test, comment out your function body and "return 0;" instead. If it still fails, post your parameter's class declaration. If it works with zero, your code has an error. Find and fix that the normal way. – nvoigt Nov 19 '13 at 06:21
  • most of the time the service works fine, in some Data it gives me that error, so what conclusion can i take from that? – nnmmss Nov 19 '13 at 06:25
  • I would take the conclusion that you are in dire need of Unit Testing. But your conclusions may differ. Debug it. This is normal developer work. Find the input that is the problem, test with that input until you find the error. – nvoigt Nov 19 '13 at 06:28

2 Answers2

2

An error code beginning with 4xx (that is, four-hundred-something) means that the problem is with the data you're sending to the server - the server can't understand the data you're sending. For example, if the request expects an integer parameter but you send a string, you'll see this problem.

(By contrast, a 5XX error means that the server understood your request, but threw an error during the processing.)

Often, a 4xx error in a WCF service means that the request isn't even reaching your code, because it may be that WCF can't deserialize the data you're sending into the types required to call your methods. In this case, if you're posting data that isn't a valid Cable, you'll see a 400 error without your code ever being called.

You can test this by examining the requests that you're sending, and also by writing a small test harness (I recommend using Linqpad!) to deserialize your request body manually - you may find the cause of your issue there.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
0

You'll probably need to turn on the WCF logging to figure this out. The logging will show you the content of the inbound request, plus your service's reaction to it. Based on that, you should be able to figure out the problem.

Here's a link to the "how to"

Community
  • 1
  • 1
Brian
  • 3,653
  • 1
  • 22
  • 33