FaultContractWrapperException was unhandled by user.

Topics: Exception Handling Application Block
Jan 6, 2010 at 11:39 AM
 

I am using exception handling blocks of EntLib 4.1 to handle exception in my WCF app.

The problem is the exception encountered in the service layer is not converted into the fault contract and propogated to the UI.

Can you please tell me what could be the problem?

Following are the code snippets.

Client:

try
            {
                PersonServiceReference.PersonServiceContractClient clnt = new WindowsFormsCLientFor_WCF_POC.PersonServiceReference.PersonServiceContractClient();
                PersonServiceReference.PersonDC person = clnt.GetPersonByID(100);
                MessageBox.Show("Test");
                person = clnt.SetPersonID(100);
                MessageBox.Show("Test");

            }
            catch (FaultException<ServiceFault> ex)
            {
                MessageBox.Show(string.Format("{0}\n{1}", ex.Detail.Id, ex.Detail.MessageText));
            }

 

Service Contract:

  [ServiceContract]
    
    public interface IPersonServiceContract
    {
        [OperationContract]
        PersonDC GetPersonByID(int id);
        [OperationContract]
        [FaultContract(typeof(ServiceFault))]
        PersonDC SetPersonID(int id);
    }

Service Implementation:

  [ExceptionShielding("WCF Exception Shielding")] 
    public class PersonServiceImplementation : IPersonServiceContract
    {
        public PersonDC GetPersonByID(int id)
        {
            Person per = new Person();
            per = per.GetPersonByID(id);
            return Translators.BLTranslator.PersonToPersonDC(per);

        }

        public PersonDC SetPersonID(int id)
        {
            Person per = new Person();
            try
            {
                
                per.SetPersonID(id);
            }

            catch (Exception ex)
            {
                if (ExceptionPolicy.HandleException(ex, "WCF Exception Shielding"))
                {
                    throw;
                }
            }
            return Translators.BLTranslator.PersonToPersonDC(per);
        }
    }

app.config:

 

 <exceptionHandling>
    <exceptionPolicies>
      <add name="WCF Exception Shielding">
        <exceptionTypes>
          <add type="System.NullReferenceException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException" name="NullReferenceException">
            <exceptionHandlers>
              <add exceptionMessage="Null reference. Rethrow." faultContractType="WCFServiceLibrarySecurityPOC.FaultContracts.ServiceFault, WCFServiceLibrarySecurityPOC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler">
                <mappings>
                  <add source="{Guid}" name="Id" />
                  <add source="{Message}" name="MessageText" />
                </mappings>
              </add>
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

 

Jan 6, 2010 at 9:16 PM

Hi,

Just want to try if you will have the same results, Can you please try not explicitly handling the exception in your code? see if the proper fault contract will be returned to the client.

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.

entlib.support@avanade.com

Jan 14, 2010 at 5:39 PM

Hi,

Saw your post while trying to solve the same problem.  My code worked if I did not re-throw in the catch block.

Try this:

catch (Exception ex)
{
                ExceptionPolicy.HandleException(ex, "WCF Exception Shielding");
}

 

Tim

Jan 15, 2010 at 6:51 AM
Edited Jan 15, 2010 at 7:54 AM

Actually, you can already remove your try catch block and the HandleException code.  As long as you decorate it with the ExceptionShielding attribute and the correct policy name (or no policy name and have the WCF Exception Shielding poliy configured), EHAB integration with WCF will work.

Sorry, I forgot the fact that that error message is generated by entlib when you first asked for it.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

entlib.support@avanade.com

Jan 15, 2010 at 7:38 AM

Thanks Tim and Sarah.

Thanks a bunch!

Jan 15, 2010 at 2:48 PM

Hi,

I figured out how to remove the try / catch block about 10 minutes after I added my post.  I think I had a typo in my ExceptionShielding attribute. 

I knew I had this working 6 months ago.  Goes to show you how fast we forget.

Tim