WCF Service Layer & Client Consuming Exception Handling

Topics: Exception Handling Application Block
May 9, 2011 at 8:35 AM

 We are implemented Exception handling in WCF Service Layer and Asp.Net Clinet. I did not get any examples in EHAB for handling WCF Service exceptions. Can you please provide next versions EnLibV.0 Hands On Labs Exception Handling in WCF Services.

I used below URL for Implementation Exception handling in WCF Service Layer and Client Consuming.

http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/04/07/Shielding-WCF-Services-with-Exception-Handling-Application-Block-_2D00_-Part-1.aspx

Can you pleae Review the below code and tell me we are implemented correct or not?

WCF Exception Handling

[DataContract]

public class Systemfault

{

    private string message;

    private Guid id;

 

    [DataMember]

    public string MessageText

    {

        get { return message; }

        set { message = value; }

    }

 

    [DataMember]

    public Guid Id

    {

        get { return id; }

        set { id = value; }

    }

}  

<exceptionPolicies>

    <add name="WCF Exception Shielding">

      <exceptionTypes>

        <add type="System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException" name="InvalidOperation">

          <exceptionHandlers>

            <add           type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF"

              name="DefaultFaultContract Handler"

              faultContractType="Bursteg.Samples.WCFIntegration.ServiceContracts.Systemfault, Bursteg.Samples.WCFIntegration.ServiceContracts">

              <mappings>

                <add name="Id" source="{Guid}"/>

                <add name="MessageText" source="{Message}"/>

              </mappings>

            </add>

          </exceptionHandlers>

        </add>

      </exceptionTypes>

    </add>

  </exceptionPolicies>

 4.  The FaultContractExceptionHandler converts the exception to a fault contract. Hence we need to specify FaultContract to our service contract to receive it on client side. 

 [ServiceContract]

[ExceptionShielding]

public interface IOrdersService

{

    [OperationContract]

    [FaultContract(typeof(Systemfault))]

    int CreateOrder(string currency, double amount);

 }

public string UserTest(string name)

       {

           try

           {

               throw new InvalidOperationException("Wrong");

           }

           catch (Exception e)

           {

               SystemFault fx = new SystemFault();

               fx.Message = e.Message;

               ExceptionPolicy.HandleException(e, "WCF Exception Shielding");

               throw new FaultException<SystemFault>(fx, "Method not present.");

           }

       }

   6. In order to receive this meaningful exception in the client, you should catch a FaultException<T> where T is the FaultContract use used.

try

{

    // Consume the service method that has a fault contract on it

}

catch (FaultException< Systemfault > ex)

{

   Systemfault fault = ex.Detail;

    Lblerroemsg.text= fault.messagetext;

 }

 We used single Systemfault for all the different services. Each module services each fault class any usefull?

 

 

May 9, 2011 at 10:39 AM

Hi,

I believe this really depends on your requirements and design choice. Sometimes other module service may need a custom fault class with some additional members.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 9, 2011 at 10:44 AM

 

Thank you.

Can you please tell me , This is right way of implementing Exception in WCF Services. Do you have any sample code urls. Can you please tell me, we are in right path of implementing EHAB for WCF.

May 9, 2011 at 11:31 AM

With your code, you need to implement the IOrdersService then call the implemented class to the client. This is a sample code:



In your service:
public class MyOrderService : IOrderService
{

	public int CreateOrder(string currency, double Amount)
	{
		if(currency != "Peso")
		{
			SystemFault myFault = new SystemFault();
			myFault.id = F8A56CF1-925E-E011-AC74-0022190F2631;
			myFault.Message = "Invalid Currency!";
			throw new FaultException(myFault);
		}
		return 1;
	}

}

In your client:

ServiceReference.IOrderService clientOrderService = new ServiceReference.MyOrderService();

try
{
	clientOrderService.CreateOrder("Dollar", 20.50)
}
catch(FaultException<ServiceReference.ServiceFault> ex)
{
       //do whatever you want with the exception
}

You can also refer to this article. Hope this helps! :)



Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com