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

Exclusive offer: get 50% off this eBook here
WCF 4.0 Multi-tier Services Development with LINQ to Entities

WCF 4.0 Multi-tier Services Development with LINQ to Entities — Save 50%

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

$29.99    $15.00
by Mike Liu | June 2010 | Enterprise Articles Microsoft

In the first article we have seen how to enhance this WCF service to support distributed transaction processing and how to configure all related computers to enable distributed transaction support

In this article by Mike Liu, author of WCF 4.0 Multi-tier Services Development with LINQ to EntitiesWe will cover the following topics in this article:

  • Configuring the firewall for the distributed WCF service
  • Propagating a transaction from the client to the WCF service
  • Testing the multiple database support of the distributed WCF service

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.

    WCF 4.0 Multi-tier Services Development with LINQ to Entities Build SOA applications on the Microsoft platform with this hands-on WCF 4.0 book and eBook guide updated for VS2010
    Published: June 2010
    eBook Price: $29.99
    Book Price: $49.99
    See more
    Select your format and quantity:

    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:

    WCF 4.0 Multi-tier Services Development with LINQ to Entities Build SOA applications on the Microsoft platform with this hands-on WCF 4.0 book and eBook guide updated for VS2010
    Published: June 2010
    eBook Price: $29.99
    Book Price: $49.99
    See more
    Select your format and quantity:

    About the Author :


    Mike Liu

    Mike Liu studied Mathematics and Software Engineering at Nanjing University and Brandeis University, graduated with a bachelor’s degree and a master's degree respectively. He is a Sun Certified Java Programmer (SCJP), Microsoft Certified Solution Developer for Visual Studio 6.0 and Microsoft Certified Solution Developer for .NET. He has been working as a software Engineer/architect on various platforms (DOS, Unix, and Windows) using C/C++, Java, VB/VB.NET, and C#.

    Mike started using C# for production development back in 2001 when C# was still in beta stage and he is now working as a senior software engineer for an investment management firm in Boston, Mass.

    Mike had his first book,MITT: Multi-user Integrated Table-processing Tool Under Unix,published in 1993, and a second book,Advanced C# Programming,published in 2003. The previous two versions of this book,WCF Multi-tier Services Development with LINQ and WCF 4.0 Multi-tier Services Development with LINQ to Entities,were published in 2008 and 2010.

    Books From Packt


    Joomla! 1.5: Beginner's Guide
    Joomla! 1.5: Beginner's Guide

    Refactoring with Microsoft Visual Studio 2010
    Refactoring with Microsoft Visual Studio 2010

    Microsoft Silverlight 4 Data and Services Cookbook
    Microsoft Silverlight 4 Data and Services Cookbook

    Microsoft Silverlight 4 Business Application Development: Beginner’s Guide
    Microsoft Silverlight 4 Business Application Development: Beginner’s Guide

    Programming Microsoft Dynamics NAV 2009
    Programming Microsoft Dynamics NAV 2009

    Microsoft Dynamics AX 2009 Programming: Getting Started
    Microsoft Dynamics AX 2009 Programming: Getting Started

    Oracle JRockit: The Definitive Guide
    Oracle JRockit: The Definitive Guide

    Microsoft Dynamics AX 2009 Development Cookbook
    Microsoft Dynamics AX 2009 Development Cookbook


    Your rating: None Average: 5 (2 votes)
    This article shows a solution. by
    This article shows exactly What I need to do to solve MSDTC Error. Thanks. It helps a lot.

    Post new comment

    CAPTCHA
    This question is for testing whether you are a human visitor and to prevent automated spam submissions.
    r
    N
    m
    4
    W
    R
    Enter the code without spaces and pay attention to upper/lower case.
    Code Download and Errata
    Packt Anytime, Anywhere
    Register Books
    Print Upgrades
    eBook Downloads
    Video Support
    Contact Us
    Awards Voting Nominations Previous Winners
    Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
    Resources
    Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software