Advanced Error Handling
This sample demonstrates the Windows Communication Foundation (WCF) routing service. The routing service is a WCF component that makes it easy to include a content-based router in your application. This sample shows how the routing service intelligently recovers from errors, using transactions and other more complex messaging concepts such as multicasting.
The samples may already be installed on your machine. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.
In this sample, the routing service is configured to read a message from an MSMQ queue and multicast this message to two lists of queues. One list is used for service queues and another is used for logging queues.
Because, by default, the MSMQ binding that the routing service is configured to use supports the use of transactions, the routing service makes sure that the message is transactional and received by at least one queue in each list before reporting to the Inbound Queue (
InQ) that the message was successfully routed. Thus, in the case where both of the service queues or both of the logging queues are unavailable, the routing service reports that the message could not be routed and the inbound queue should take some action. This action consists of moving the message to the system dead letter queue.
To use this sample
Install MSMQ before running this sample. If MSMQ is not installed, an exception message is returned when running the sample. Instructions for installing MSMQ can be found at Installing Message Queuing (MSMQ).
Using Visual Studio 2012, open AdvancedErrorHandling.sln.
Press F5 or CTRL+SHIFT+B in Visual Studio.
- If you build the application with CTRL+SHIFT+B, you must start the application at ./RoutingService/bin/debug/RoutingService.exe.
In the console window, press ENTER to start the client.
The client returns different statistics about the queues for each case.
The following is the output returned for case 1 (no failures).
The inbound queue has 0 messages. The primary service queue has 1 messages. The backup service queue has 0 messages. The primary logging queue has 1 messages. The backup logging queue has 0 messages. Press <Enter> to continue
The following is the output returned for case 3 (primary service and logging queue failures).
The inbound queue has 0 messages. The primary service queue does not exist. The backup service queue has 1 messages. The primary logging queue does not exist. The backup logging queue has 1 messages. Press <ENTER> to continue.
The following is the output returned for case 4 (primary service queue and primary and backup logging queue failures).
The inbound queue has 0 messages. The primary service queue does not exist. The backup service queue has 0 messages. The primary logging queue does not exist. The backup logging queue does not exist. The System Dead Letter queue has 1 messages. Press <ENTER> to Quit.
The following is the output returned for case 2 (primary service queue failure).
The inbound queue has 0 messages. The primary service queue does not exist. The backup service queue has 1 messages. The primary logging queue has 1 messages. The backup logging queue has 0 messages. Press <ENTER> to continue.
Configurable Via Code or App.config
The sample ships configured to use an App.config file to define the router’s behavior. You can also change the name of the RoutingService\App.config file to something else so that it is not recognized and change the value of the
configDriven field in RoutingService\Program.cs to
false to use the configuration defined in the code. Either method results in the same behavior from the router.
This sample demonstrates that the routing service can handle advanced messaging capabilities, such as transactions and receive context, and that it can utilize these capabilities as a part of correctly handling error scenarios.
Real World Scenario
Contoso wants to utilize transactional receives through the routing service to ensure that all necessary services receive information even during error conditions. Furthermore, they would like errors to be handled correctly and automatically and failures to be reported in the case that a message is undeliverable even when error handling logic is utilized. For this purpose, they configure the routing service to fail over to specific endpoints as expected and the routing service handles the error situations, which includes the creation, completion, and rollback/aborting of transactions/receive contexts as necessary.