Search code examples
c#.netwcfwcf-bindingwcf-client

WCF - Error handling depends on the binding used?


Is there some difference in the way how the exceptions are handled in regards of the binding used?

I'm getting different results if I use WSHttpBinding or BasicHttpBanding.

For example, this is my error handling routine in the client side:

//MyClient client = new MyClient("WSBinding");
MyClient client = new MyClient("BasicBinding");
try
{

    Result = client.DoTheWork("test");
    Console.WriteLine(Result);
}
catch (FaultException e)
{
    if (e.Code.SubCode.Name.Equals("BadParameters"))
        Console.WriteLine("Bad parameter entered");

    Console.WriteLine(e);
}

I can catch the exception on the client when I use WSHttpBinding, however if I use basicHttpHandling I cann't, I get:

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.

This is my web.config,

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>   
  <wsHttpBinding>
    <binding name="wsBinding">
      <security mode="None">
        <transport clientCredentialType="None" />
        <message establishSecurityContext="false" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<services>
  <service name="MailboxServiceLibrary.MailboxService" behaviorConfiguration="ServiceBehavior" >
    <!--endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsBinding" contract="ParServiceLibrary.IParService">
      <identity>
        <dns value="MyServer.com" />
      </identity>
    </endpoint-->     
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="ParServiceLibrary.IParService"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Seems that BasicHttpBinding uses the Soap 1.1 format and for WSHttpBinding, it uses Soap 1.2 formats but not sure if this can be the cause.

Thanks,


Solution

  • I just realized that I was using a wrong approach to catch the exception in the client site,

    I was using,

    if (e.Code.SubCode.Name.Equals("MyFaultID"))
    

    And it must be,

    if (e.Code.Name.Equals("MyFaultID"))
    

    In this way works fine in both bindings.