WCF Exception Shielding not working

Topics: Exception Handling Application Block
Jun 15, 2010 at 1:03 AM

I am trying to use WCF Exception Shielding feature of Enterprise Library. Below are configuration settings that I am using.

<exceptionHandling>
  <exceptionPolicies>
    <add name="Policy">
      <exceptionTypes>
        <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          postHandlingAction="NotifyRethrow" />
      </exceptionTypes>
    </add>
    <add name="WCF Exception Shielding">
      <exceptionTypes>
        <add name="InvalidOperation" type="System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          postHandlingAction="NotifyRethrow">
          <exceptionHandlers>
            <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF"
              faultContractType="TestServiceFault, TestService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
              name="DefaultFaultContract Handler">
              <mappings>
                <add source="{Guid}" name="FaultID" />
                <add source="{Message}" name="Message" />
                <add source="{Source}" name="Source" />
                <add source="{StackTrace}" name="StackTrace" />
              </mappings>
            </add>
          </exceptionHandlers>
        </add>
      </exceptionTypes>
    </add>
  </exceptionPolicies>
</exceptionHandling>

Here is data contract that represents a class for FaultContract

[DataContract]
    public class TestServiceFault
    {
        [DataMember]
        public Guid FaultID { get; set; }
        [DataMember]
        public string Message { get; set; }
        [DataMember]
        public string Source { get; set; }
        [DataMember]
        public string StackTrace { get; set; }
    }

Here is how I have wired TestServiceFault with the TestMethod

[OperationContract]
[FaultContract(typeof(TestServiceFault))]
DataSet TestMethod(string name, int? debug);

And here is how I implement it in code

catch (Exception ex)
{
       Common.WriteLog(ex.Message + "," + ex.StackTrace);
       bool rethrow = ExceptionPolicy.HandleException(ex, "WCF Exception Shielding");
       if (rethrow)
       {
              throw;
       }
}

Now when the exception occurs in TestService I am supposed to receive an exception wrapped in TestServiceFault class. But its not happening. I am not what am I missinh here. Can somebody help?

Thanks

Jun 15, 2010 at 1:26 PM

Hi,

From the client side you'll need to catch the exception via

try
{
    // Call the service method that has a fault contract on it
}
catch (FaultException<TestServiceFault> ex)
{
    ServiceFault fault = ex.Detail;
    Console.WriteLine(fault.Message);
    Console.WriteLine(fault.StackTrace);
}

Also, for some more details see this blog if may help http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/04/07/Shielding-WCF-Services-with-Exception-Handling-Application-Block-_2D00_-Part-1.aspx regarding WCF Exception Shielding.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 15, 2010 at 11:14 PM
Thanks for the reply. I am already doing what you have suggested. Can I ask if you have any sample solution that I can refer to?

Thanks


From: [email removed]
To: [email removed]
Date: Tue, 15 Jun 2010 05:26:32 -0700
Subject: Re: WCF Exception Shielding not working [entlib:216018]

From: AvanadeSupport
Hi,
From the client side you'll need to catch the exception via
try
{
    // Call the service method that has a fault contract on it
}
catch (FaultException<TestServiceFault> ex)
{
    ServiceFault fault = ex.Detail;
    Console.WriteLine(fault.Message);
    Console.WriteLine(fault.StackTrace);
}
Also, for some more details see this blog if may help http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/04/07/Shielding-WCF-Services-with-Exception-Handling-Application-Block-_2D00_-Part-1.aspx regarding WCF Exception Shielding.
Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Read the full discussion online.
To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)
To start a new discussion for this project, email entlib@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Hotmail is redefining busy with tools for the New Busy. Get more from your inbox. See how.
Jun 16, 2010 at 1:12 AM
I am getting exception "InvalidOperationException was unhandled by user code" at the hightlighted line

try
{
    // Call the service method that has a fault contract on it
    throw new InvalidOperationException("Test Exception");
} catch (FaultException<TestServiceFault> ex) { TestServiceFault fault = ex.Detail;
}



From: [email removed]
To: [email removed]
Date: Tue, 15 Jun 2010 05:26:32 -0700
Subject: Re: WCF Exception Shielding not working [entlib:216018]

From: AvanadeSupport
Hi,
From the client side you'll need to catch the exception via
try
{
    // Call the service method that has a fault contract on it
}
catch (FaultException<TestServiceFault> ex)
{
    ServiceFault fault = ex.Detail;
    Console.WriteLine(fault.Message);
    Console.WriteLine(fault.StackTrace);
}
Also, for some more details see this blog if may help http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/04/07/Shielding-WCF-Services-with-Exception-Handling-Application-Block-_2D00_-Part-1.aspx regarding WCF Exception Shielding.
Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Read the full discussion online.
To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)
To start a new discussion for this project, email entlib@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox. Learn more.
Jun 16, 2010 at 3:22 AM

You need to set the PostHandlingAction property of your exception policy to ThrowNewException instead of NotifyRethrow.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 17, 2010 at 12:53 AM

Thanks. I am curious to know if it is possible to wrap exception types other InvalidOperationException into FaultException<T> type. For example I want to wrap SqlException into FaultException<T>. How do I do this?

Jun 17, 2010 at 2:10 AM

Please discard the previous message. My fault. I was pointing to wrong dll. Thanks.