Thursday, October 27

Force an exception with a Throw Exception shape and send the notification using the ESB Portal from an Orchestration Send Port

Sometimes you might wish to force your application to throw an exception if the processing logic goes down a certain decide branch, just to inform the tester or support administrator that the message is being terminated according to plan. the example below forces an exception and sends customized information to the ESB portal

Looking at the post, some might wonder why the scope and exception block? Well, ESB has a known bug - if you try to use the message assignment expression used below outside an exception handler, you will create an endless loop on your host instance. This bug is better described here

1.       In your orchestration, add a reference to Microsoft.Practices.ESB.ExceptionHandling and Microsoft.Practices.ESB.ExceptionHandling.Schemas.Faults (Open your project, Richt-Click on References à Add Reference à .Net)
2.       Create a Scope of transaction type None. Create a variable of .Net type System.ApplicationException (found in mscorlib) called excApplication
3.       Add an Expression Shape called ‘CreateException’ in this scope
4.       Add a Throw Exception Shape called ‘ThrowException’. In the Throw Exception Shape properties, enter the exception object you created above as the exception object type (i.e. excApplication)
5.       Create an orchestration Message called ‘msgESBFault’ of type Microsoft.Practices.ESB.ExceptionHandling.Schemas.Faults.FaultMessage
6.       Right-click on the scope and add a new Exception Handler called ‘ThrowExceptionHandler’
7.       In the Exception properties, enter exApplicationobj as the exception object name
8.       In the Exception properties, Select  System.ApplicationException as your exception object type (found in .Net Exception à mscorlib à system à ApplicationException)
9.       Add a Construct Message Shape called ‘ConstructESBFaultmsg’ with Message Assignment Shape  called ‘AssignESBFaultmsg’ and Send Shape called ‘SndESBFaultmsg’
11.    Add ‘Microsoft.Practices.ESB.ExceptionHandling.Schemas.Faults.FaultMessage’ as the Message Type of the direct send port’s Operation
12.    In the Message Assignment shape, generate the ESB fault message and the ESB message such as:
// create new fault message msgESBFault=Microsoft.Practices.ESB.ExceptionHandling.ExceptionMgmt.CreateFaultMessage();
// set fault message properties
msgESBFault.FaultCode="0202";
msgESBFault.FaultDescription="Exception notification from branch <branch descriptor> of orchestration <orchestration name>" + System.Environment.NewLine + "InnerXML is: " + exApplicationobj.InnerException.ToString();
msgESBFault.FailureCategory="Exception notification from branch <branch descriptor> of orchestration <orchestration name>";
msgESBFault.FaultSeverity=Microsoft.Practices.ESB.ExceptionHandling.FaultSeverity.Information;
//Add the fault message to the original message
Microsoft.Practices.ESB.ExceptionHandling.ExceptionMgmt.AddMessage(msgESBFault, <Originalmsg>);
13.    Connect the send shape ‘SndESBFaultmsg’ to the direct send port

No comments:

Post a Comment