Exception Behaviour Strategies
As you know, the
ServiceConsumer automatically handles the clean up of service channels.
If an exception occurs at any point throughout the usage of the channel, the
ServiceConsumer will catch it and close the channel if necessary.
In some cases, for example, if an exception occurs while attempting to close the channel, the
ServiceConsumer does not want to propagate the exception back to the calling code, as it is not the caller’s concern.
In order to be a good citizen and prevent the
ServiceConsumer from swallowing exceptions, the concept of the “Exception Behaviour Strategy” was born.
As I expect could be the case, many organisations for instance will want to use a logging library and write out the exceptions to a log file somewhere.
You Are In Control
Whenever an exception occurs throughout the lifetime of the
ServiceConsumer, a corresponding method on the configured Exception Behaviour Strategy class is invoked, thus allowing the exception to be logged and statistics recorded for an organisation’s support purposes.
Each Exception Behaviour Strategy class implements the interface
This interface contains the following methods:
PerformFaultExceptionBehaviour- the behaviour to perform when a fault exception occurs while the service operation is called;
PerformCommunicationExceptionBehaviour- the behaviour to perform when a communication exception occurs while the service operation is called;
PerformTimeoutExceptionBehaviour- the behaviour to perform when a timeout exception occurs while the service operation is called;
PerformUnexpectedExceptionBehaviour- the behaviour to perform when an unexpected exception occurs while the service operation is called;
PerformCloseCommunicationExceptionBehaviour- the behaviour to perform when a communication exception occurs during a close;
PerformCloseTimeoutExceptionBehaviour- the behaviour to perform when a timeout exception occurs during a close;
PerformCloseUnexpectedExceptionBehaviour- the behaviour to perform when an unexpected exception occurs during a close;
PerformAbortExceptionBehaviour- the behaviour to perform when an exception occurs during an abort; and
PerformDestructorExceptionBehaviour- the behaviour to perform when an exception occurs during a destructor/
Out-Of-The-Box Exception Behaviour Strategies
The ChannelAdam.Wcf Library comes with the following Exception Behaviour Strategies:
NullServiceConsumerExceptionBehaviourStrategy- which does not write out any exception details anywhere;
StandardOutServiceConsumerExceptionBehaviourStrategy- which writes out all exceptions to the Standard Error stream; and
StandardErrorServiceConsumerExceptionBehaviourStrategy- which writes out all exceptions to the Standard Out stream.
By default, the
ServiceConsumerFactory configures each
ServiceConsumer with the
Use Your Own
To use your own strategy, simply create a class that implements the interface
There are three ways to change the Exception Behaviour Strategy that is assigned when you create a
- Specify a different Exception Behaviour Strategy on one of the overloads of the
Createmethod on the
- Directly set the property
ServiceConsumerinstance itself; or
- Change the default - in some bootstrap code, set the static property
ServiceConsumerFactory.DefaultExceptionBehaviourStrategyto your desired strategy.
From then on, that strategy will be used by default for all created