How to configure MSDTC and the firewall for the distributed WCF service

Build SOA applications on the Microsoft platform with this hands-on WCF 4.0 book and eBook guide updated for VS2010

Understanding the distributed transaction support of a WCF service

As we have seen, distributed transaction support of a WCF service depends on the binding of the service, the operation contract attribute, the operation implementation behavior, and the client applications.

The following table shows some possible combinations of the WCF-distributed transaction support:

Binding permits transaction flow

Client flows transaction

Service contract opts in transaction

Service operation requires transaction scope

Possible result

True

Yes

Allowed or Mandatory

True

Service executes under the flowed in transaction

True or False

No

Allowed

True

Service creates and executes within a new transaction

True

Yes or No

Allowed

False

Service executes without a transaction

True or False

No

Mandatory

True or False

SOAP exception

True

Yes

NotAllowed

True or False

SOAP exception

Testing the distributed transaction support of the WCF service

Now that we have changed the service to support distributed transaction and let the client propagate the transaction to the service, we will test this. 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 transaction support of the WCF service.

Configuring the Distributed Transaction Coordinator

In a subsequent section, we will call two services to update two databases on two different computers. As these two updates are wrapped within one distributed transaction, Microsoft Distributed Transaction Coordinator (MSDTC) will be activated to manage this distributed transaction. If MSDTC is not started or configured properly the distributed transaction will not be successful. In this section, we will explain how to configure MSDTC on both machines.

You can follow these steps to configure MSDTC on your local and remote machines:

  1. Open Component Services from Control Panel | Administrative Tools.
  2. In the Component Services window, expand Component Services, then 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. If this machine is running Windows XP, click on the Security Configuration button.
  6. If this machine is running Windows 7, verify that Use local coordinator is checked and then close the My Computer Properties window. Expand Distributed Transaction Coordinator under My Computer node, right-click on Local DTC, select Properties from the context menu, and then from the Local DTC Properties window, click on the Security tab.
  7. You should now see the Security Configuration for DTC on this machine.Set it as in the following screenshot.
  • Remember you have to make these changes for both your local and remote machines.
  • You have to restart the MSDTC service after you have changed your MSDTC settings, for the changes to take effect.Also, to simplify our example, we have chosen the No Authentication Required option. You should be aware that not needing authentication is a serious security issue in production. For more information about WCF security, you can go to the MSDN WCF security website at this address:MSDN Library.

    Configuring the firewall

    Even though Distributed Transaction Coordinator has been enabled the distributed transaction may still fail if the firewall is turned on and hasn't been set up properly for MSDTC.

    To set up the firewall for MSTC, follow these steps:

    1. Open the Windows Firewall window from the Control Panel.
    2. If the firewall is not turned on you can skip this section.
    3. Go to the Allow a program or feature through Windows Firewall window(for Windows XP, you need to allow exceptions and go to the Exceptions tab on the Windows Firewall window).
    4. Add Distributed Transaction Coordinator to the program list (windows\system32\msdtc.exe) if it is not already on the list. Make sure the checkbox before this item is checked.
    5. Again you need to change your firewall setting for both your local and remote machines.

    Now the firewall will allow msdtc.exe to go through so our next test won't fail due to the firewall restrictions.

    You may have to restart IIS after you have changed your firewall settings. In some cases you may also have to stop and then restart your fi rewall for the changes to take effect.

    Propagating a transaction from the client to the WCF service

    Now we have the services and MSDTC ready. In this article, we will rerun the distributed test client and verify the distributed transaction support of the enhanced WCF service.

    Testing distributed transaction support with one database

    First we will test the distributed transaction support of the WCF service within one database. We will try to update two products (30 and 31). The first update will succeed but the second update will fail. 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 fi rst update should not be committed to the database.

    Now follow these steps to do this test:

    1. Press Ctrl + F5 to start the client application.
    2. Enter 30 and 31 as product IDs in the top two textboxes.
    3. Make sure Get and Update 2nd Product in Remote Database is not checked.
    4. Click on the Get Product Details button. The prices for these two products should be 29.89 and 13.5, respectively.
    5. Enter 30.89 and -14.5 as new prices in the middle two textboxes.
    6. Click on the Update Price button.
    7. Click on the Get Product Details button to refresh the product details so that we can verify the results.

    From the output window we can see that the prices of both products remain the same which proves that the first update has been rolled back. From this output we know that both service calls are within a distributed transaction and the WCF service now fully supports the distributed transaction within one database.

    Testing distributed transaction support with two databases

    Next we will test the distributed transaction support of the WCF service with two databases or machines involved. As mentioned before, this is a true distributed transaction test as MSDTC will be activated only when the machine boundary is crossed.

    In this test, we will try to update two products (product 30 and 31). But this time the second product (product 31) is in a remote database on another machine. As in the previous test the first update will succeed but the second update will fail. 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. The fi rst update should fi nally not be committed to the database.

    Now follow these steps to carry out this test:

    1. Press Ctrl + F5 to start the client application.
    2. Enter 30 and 31 as product IDs in the top two textboxes.
    3. Make sure Get and Update 2nd Product in Remote Database is checked.
    4. Click on the Get Product Details button. The prices for these two products should be 29.89 and 12.5, respectively.
    5. Enter 30.89 and -14.5 as new prices in the middle two textboxes.
    6. Click on the Update Price.
    7. Then click on the Get Product Details button to refresh the product details so that we can verify the results.

    From the output window we can see that the prices of both products remain the same which proves that the first update has been rolled back. From this output we know that both service calls are within a distributed transaction and the WCF service now fully supports the distributed transaction with multiple databases involved.

    With the previous tests, you might not get an output as shown here, but instead a message like Exception: The underlying provider failed to Open in your Update Result textbox. If you debug your code, inside the UpdatePrice or UpdateRemotePrice method, you may see 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

    This might be because you haven't set your Distributed Transaction Coordinator or firewall correctly. In this case you need to follow the instructions in the previous article to configure these settings, then come back and redo these tests.

    Summary

    In this article, we have discussed how to enable distributed transaction support for a WCF service. Now we can wrap sequential WCF service calls within one transaction scope and flow the distributed transaction into the WCF services. We can also update multiple databases on different computers all within one single distributed transaction.

    The key points discussed in this article include:

    • MSDTC network access must be enabled for distributed transaction support among multiple computers
    • The firewall has to be configured to allow msdtc.exe for a distributed transaction to succeed

    If you have read this article you may be interested to view:

    Books to Consider

    comments powered by Disqus