Can't get WCF Shielding to raise a FaultException<T>, only FaultException

Topics: Exception Handling Application Block
Nov 30, 2010 at 1:51 PM

Hi,

I'm new to the Enterprise Library, and am trying to use the Exception Handling to provide a WCF service (basic HTTP binding) with exception shielding.
I'm using v5.0 of the Enterprise Library. The WCF is hosted in IIS, and I've modified the web.config with an Exception policy so that a new exception is thrown for all cases.
When I cause the service throws an exception, I can see that Enterprise Library is using the correct policy, because of the message text on the FaultContract. But it raises a new FaultContract, rather than a FaultContract<MyException>.
I'm sure that MyException has been defined correctly; when I load the service proxy into the client app, I can see MyException in the class viewer.

My guess is that the EntLib can't find the required type of MyException to create. How can I point EntLib to the DLL / type? And I won't be able to use a strong name and put the DLL in the GAC.

So, can anyone tell me what I need to do? Futher details are below.

I'm currently testing this on a Windows 7 PC, and am hosting the WCF service in the local IIS, and I have the following files...

C:\inetpub\wwwrooot\MyProject\Services\MyService.svc
C:\inetpub\wwwrooot\MyProject\Services\Web.config
C:\inetpub\wwwrooot\MyProject\Services\Bin\MyService.dll

The exception that I'm trying to get EntLib to raise on my behalf is as follows

And below is a fragment from the web.config

  <exceptionHandling>
    <exceptionPolicies>
      <add name="MyFaultPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                  exceptionMessage="Service error. Please contact your administrator. [MyFaultPolicy]"
                  faultContractType="MyProject.Services.MyException, MyService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                  name="My Exception Handler">
                <mappings>
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="WCF Exception Shielding">
        <exceptionTypes>
          <add name="Exception" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF"
                exceptionMessage="Service error. Please contact your administrator. [WCF Exception Shielding]"
                faultContractType="System.ServiceModel.FaultException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="Fault Contract Exception Handler">
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

Dec 1, 2010 at 1:27 AM

Looking at your config, I noticed you specified FaultException in the faultContractType attribute.  It should be the type (T) of the FaultException<T>.

 

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

Dec 1, 2010 at 9:22 AM

Hi,

 

I should have said, the service method which is raising the error is using the "MyFaultPolicy" exception policy. We have the "WCF Exception Shielding" policy in place as a catch all (as per the "Developer's Guide")
I can see that the "MyFaultPolicy" exception policy is being used rather than "WCF Exception Shielding" due to the differences in the exception message.

Sorry about that.

 

Alastair

Dec 2, 2010 at 1:35 AM

You mean to say you decorated your service with

[ExceptionShielding("MyFaultPolicy")]

but your methods all have a catch block and handle the exception using the WCF Exception Shielding policy?

If this is the case, then that is an odd way of handling exception.  I think you only meant to use the MyFaultPolicy.  Decorating your service with the ExceptionShielding attribute tells it to use the policy you specified for handling exception thus removing you the need for having a try catch block inside each methods. 

 

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

Dec 6, 2010 at 9:47 AM

Hi,

The Service that I'm testing with the EntLib has been decorated with [ExceptionShielding("MyFaultPolicy")]. The service code throws an exception (in this case an ApplicationException), which is then being converted to the FaultException by the EntLib.

Originally the MyFaultPolicy policy also included property mapping and logging of the exception. But as the specified FaultException<T> was not being raised by the EntLib, we have removed everything to make it the simplest case.

We have put the "WCF Exception Shielding" policy in the config file because we belived it acted as a "default" policy, and was used if the EntLib could not find the policy specified by the service (page 79 of "Developer's Guide to Microsoft Enterprise Library"). As we start to move our services over to use the EntLib for exception shielding, it's possible that someone will make a typo in the config file, and it seems like a sensible, cost free, precaution. If you believe it is causing an issue, then we can remove it.

Alastair

Dec 7, 2010 at 12:54 AM

I think you misunderstood what is written in the dev guide.  EntLib only looks for the WCF Exception Shielding policy if you didn't specify an exception policy name in the ExceptionShielding attribute. 

Anyway, could you send a small project which repro the issue you're experiencing?

 

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