Testing the distributed transaction support of the WCF service

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.

Propagating a transaction from client to 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.

Propagating a transaction from client to the WCF service

If you didn't get a similar output as shown here, and instead got one of the following error messages:

  • MSDTC on server 'xxxxxx' is unavailable
  • Network access for Distributed Transaction Manager(MSDTC) has been disabled
  • The transaction has already been implicitly or explicitly committed or aborted

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.

Configuring the Distributed Transaction Coordinator

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:

  1. Open Component Services from Control Panel | Administrative Tools on the remote machine.
  2. In the Component Services window, expand Component Services and Computers, and then right-click on My Computer.
  3. Select Properties from the context menu.
  4. On the My Computer Properties window, click on the MSDTC tab.
  5. Click the Security Configuration button.
  6. Uncheck Network DTC Access.

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):

Configuring the Distributed Transaction Coordinatordistributed transaction support testing, WCF servicetransaction, propogating from client to WCF service

Note

You may have to restart the MSDTC service and your host application after you have changed your MSDTC settings for the changes to take effect.

Configuring the firewall

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:

  1. Open the Windows Firewall window from the Control Panel.
  2. If the firewall is off, turn it on.
  3. If Don't allow exceptions is checked, you can skip the next two steps.
  4. Click on the Exceptions tab.
  5. Uncheck (windowssystem32)msdtc.exe, if it is in the list.

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.

Note

You may need to restart your host application after you have changed your firewall settings. In some cases, you may also have to stop and then restart your firewall for the changes to take effect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset