6

WCF and errorhandling

by klh 25. juni 2008 06:56

Whenever you're doing WCF calls you must expect Exceptions. (see Pedram Rezaei's blog)

To handle this I have made some generic functionality in order to not copy the ExceptionHandling functionality around.

First of all I make a proxy using SVCUTIL (since I use Visual Studio 2005).

Then I make a wrapper class for the service, this is the class I will use as a service in my code. By doing this I am more resilient to change since I don't know the proxy.

The service wrapper assigns output variables that is uses later in the call to the proxy.

The ServiceCaller is then called, the ServiceCaller wraps a call back to the ServiceWrapper with the ExceptionHandling code.

 

image

The service wrapper should implement the IService interface:

public interface IService
{
void Execute();
}

 

This Execute method is used for the Exception wrapper class I have made called ServiceCaller.

This ServiceCaller handles Exceptions using the P&P ExceptionHandling block. It is important to note that the exception should always be rethrown, the reThrow bool is only there to comply with the way we use the ExceptionHandling applicationBlock elsewhere.

The WCF policy whould log the Exception.

The ServiceCaller looks like this:

	public class ServiceCaller
	{
	public static void Execute(IService theService)
	{
	Log.Debug("calling ServiceCaller.Execute on " + theService.GetType().FullName, typeof(ServiceCaller));
	try
	{
	                theService.Execute();
	}
	                //State of the proxy and channels
	catch (ObjectDisposedException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	catch (InvalidOperationException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	//Business Exceptions
	catch (FaultException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	catch (TimeoutException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	catch (EndpointNotFoundException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	//Trying to use the proxy after the underlying channel has faulted
	catch (CommunicationObjectFaultedException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	//Typically when client and server binding are not compatible
	catch (ProtocolException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	//Problem evaluation the security settings of the message
	catch (MessageSecurityException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	//Network avaliability, Wrong Address, Host process not running etc.
	catch (CommunicationException ex)
	{
	bool reThrow = ExceptionPolicy.HandleException(ex, "WCF Policy");
	if (reThrow)
	{
	throw;
	}
	return;
	}
	}
	}
	

Now if I have a simple service that gets the type of a message in a message store, Ie. when using the Saga Pattern, I will implement it like this, please note how no ExceptionHandling is in this code, since it is in the ServiceCaller :

public class GetMessageTypeService : IGetMessageTypeService, IService
{
private string _id;
private MessageTypeEnum messageType;
public static IGetMessageTypeService FactoryMethod()
{
return new GetMessageTypeService();
}
public MessageTypeEnum GetMessageType(string id)
{
_id = id;
ServiceCaller.Execute(this);
return messageType;
}
void IService.Execute()
{
using (execute_pptClient target = new execute_pptClient("conig"))
{
InputParameters inp = GetInputParameter();
OutputParameters outp =  target.execute(inp);
if(outp.O_TYPEID == 1)
{
messageType = MessageTypeEnum.Loenmodtager;
}
else
{
messageType = MessageTypeEnum.Selvstaendig;
}
}
}
private InputParameters GetInputParameter()
{
InputParameters inp = new InputParameters();
decimal decMessageID;
decimal.TryParse(_id, out decMessageID);
inp.I_ID = decMessageID;
inp.I_IDSpecified = true;
return inp;
}
}

Tags:

WCF

0

WCF presentation in Aarhus .Net User Group

by klh 29. maj 2008 03:53

Yesterday I gave a presentation in Aarhus .Net User Group on Windows Communication Foundation.

I thought it went rather well.

It was a basic introduction to WCF, with a presentation of a WCF service being called from SoapUI and from a WCF Client.

The second part of the presentation was an overview of the project where we use WCF at my work.

 

I have only included the first part since the second part does not make sense without a speak.

The presentation is in danish.

 

Here is the presentation

 

Here is a rar file containing the Visual Studio 2005 solution

0

WCF in Aarhus .Net User Group

by klh 26. maj 2008 16:29

Tommorow I am giving a presentation in Aarhus .Net User Group on WCF.

It's not like I'm an expert or anything. But I have experience from a SOA project.

So I'm going to give an introduction to WCF, and then I'm going to show how we use WCF in our SOA

0

Ny webcast om WCF

by klh 18. december 2007 15:37

Jeg har lavet en webcast om WCF

Denne webcast omhandler meget basal WCF og kun http via soap;

Tags: ,

WCF | SOA | C# | Webcasts

Powered by BlogEngine.NET 1.4.5.0
Original Design by Laptop Geek, Adapted by onesoft