Now that we have all of the supporting distributed transactions of the service and the client, we will test this service. We will propagate a transaction from the client to the service, test the multiple database support of the WCF service, and discuss the Distributed Transaction Coordinator and Firewall settings for the distributed transactions support of the WCF service.
In this section, we will re-run the distributed test client, and verify the distributed transaction support of the enhanced WCF service.
Just press F5 or Ctrl+F5 to start the host and client applications. From the source code, we know that in the first method it will try to update two products (30 and 31)
. Both updates are wrapped in one client transaction, which will be propagated into the service, and the service will participate in this distributed transaction. Due to the failure of the second update, the client application will roll back this distributed transaction at the end, and the service should also roll back every update that is within this distributed transaction. So, in the end, the first update should not be committed to the database.
In the second method, it will try to update a category description (category 4)
and a product price (product 30)
. Both updates are wrapped in one client transaction, which will be propagated into the service, and the service will participate in this distributed transaction. Due to the failure of the second update, the client application will roll back this distributed transaction at the end, and the service should also roll back every update that is within this distributed transaction. So, in the end, the first update should not be committed to the database.
From the output window, we can see that the first transaction fails due to the database constraint. And we can also see that the prices of both products remain the same, which proves that the first update has been rolled back. The second transaction also fails due to the database constraint. We can also see that both the category description and the product price remain the same, which proves that the first update has been rolled back, too. From this output, we know that both method calls are within a distributed transaction, and the WCF service now fully supports the distributed transaction.
If you didn't get a similar output as shown here, and instead got one of the following error messages:
Then it is possible that you haven't set your Distributed Transaction Coordinator or firewall correctly. In this case, you can follow the instructions in the next two sections to configure these settings.
In the previous section, when we called two services to update two databases on two different computers, a distributed transaction was started. In this case, Microsoft Distributed Transaction Coordinator (MSDTC) was activated to manage this distributed transaction. If MSDTC hadn't been started or configured properly, the distributed transaction would have failed.
To test this, we can disable MSDTC on the remote machine, and try to run the same test to see what happens. You can follow these steps to disable MSDTC on the remote machine:
Now, MSDTC on the remote machine is disabled. If you run the client again, you may get this result:
Description and price before update:Description updated at 8:16:46 PM 55.8900
Description and price after update:Description updated at 8:16:46 PM 55.8900
CategoryFault returned: could not update category. Error message:MSDTC on server '[remote_pc_name]' is unavailable.
If you uncheck Network DTC Access or Allow Outbound in the MSDTC Security Configuration window on your local computer where your client application is running, you may get this error message:
Description and price before update:Description updated at 8:16:46 PM 55.8900
Description and price after update:Description updated at 8:16:46 PM 55.8900
CategoryFault returned: could not update category. Error message:Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool.
This test tells us that the MSDTC must be enabled for the WCF service to support distributed transactions. Now, change the settings back to the original values so that we can continue our next test. You should have the following settings in your MSDTC Security Configuration window for both your local and remote computers (if your remote computer is a Windows 2003 box, you may need to select No Authentication Required):
Even though the Distributed Transaction Coordinator has been enabled, the distributed transaction may still have failed if the firewall is turned on and hasn't been set up properly for MSDTC.
To test this, follow these steps:
Now, the firewall will block msdtc.exe
. If you run the client again, you may get this result:
Description and price before update:Description updated at 8:16:46 PM 55.8900
Description and price after update:Description updated at 8:16:46 PM 55.8900
CategoryFault returned: could not update category. Error message:The transaction has already been implicitly or explicitly committed or aborted.
This means that the distributed transaction can't be started due to the firewall blocking msdtc.exe
. So, to run the distributed transaction, you need to either turn off the firewall, or add windowssystem32msdtc.exe
to the firewall exception list.