Chapter 10. Distributed Transaction Support of WCF

In previous chapters, we created a WCF service by using LINQ to Entities in the data access layer. Next, we will apply settings so that this WCF service will be a distributed service, which means that it can participate in distributed client transactions, if there are any. Client applications will control the transaction scope and decide whether a service should commit or rollback its transaction.

In this chapter, we will first verify that the LINQNorthwind WCF service, which we built in the previous chapter, does not support distributed transaction processing. We will then learn how to enhance this WCF service to support the distributed transaction processing and how to configure all related computers to enable the distributed transaction support. To demonstrate this, we will propagate a transaction from the client to the WCF service and verify that all sequential calls to the WCF service are within one single distributed transaction. We will also explore the multiple database support of the WCF service and discuss how to configure Microsoft Distributed Transaction Coordinator (MSDTC) and the firewall for the distributed WCF service.

We will cover the following topics in this chapter:

  • Creating the solution files
  • Testing the transaction behavior of the DistNorthwind WCF service
  • Enabling transaction flow in the service bindings
  • Modifying the service operation contract to allow transaction flow
  • Modifying the service operation implementation to require a transaction scope
  • Configuring Distributed Transaction Coordinator for the distributed WCF service
  • 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

Creating the DistNorthwind solution

In this chapter, we will create a new solution based on the LINQNorthwind solution. We will copy all of the source code from the LINQNorthwind directory to a new directory and then customize it to suit our needs.

Follow these steps to create the new solution:

  1. Create a new directory named DistNorthwind under the existing C:SOAwithWCFandLINQProjects directory.
  2. Copy all of the files under the C:SOAwithWCFandLINQProjectsLINQNorthwind directory to the C:SOAwithWCFandLINQProjectsDistNorthwind directory.
  3. Remove the LINQNorthwindClient folder. We will create a new client for this solution.
  4. Change the solution file's name from LINQNorthwind.sln to DistNorthwind.sln and also from LINQNorthwind.v11.suo to DistNorthwind.v11.suo.

Now, we have the file structures ready for the new solution. Here we will re-use the old service to demonstrate how to plug in transaction support to an existing WCF service.

First, we need to make sure this old service in the new solution still works and we also need to make a small change to the service implementation so that we can test transaction support with it. Once we have the service up and running, we will create a new client to test this new service by using the following steps:

  1. Start Visual Studio and open the C:SOAWithWCFandLINQProjectsDistNorthwindDistNorthwind.sln solution.
  2. Click on the OK button to close the projects were not loaded correctly warning dialog.
  3. From the Solution Explorer, remove the LINQNorthwindClient project.
  4. Open the ProductService.cs file in the LINQNorthwindService project in this new solution and remove the condition check for negative price check in the UpdateProduct method. Later we will try to update a product's price to be negative to test the distributed transaction support of WCF.

    Note

    If this check is not removed, when we try to update a price to be negative later in this chapter, the operation will stop right at this validation check. It won't be able to reach the DAL where LINQ to Entities will be used, and the remote database will get involved, which is the key part for a distributed transaction support test.

The following screenshot shows the final structure of the new solution DistNorthwind:

Creating the DistNorthwind solution

Now, we have finished creating the solution. If you build the solution now, you should see no errors. You can set the service project as the startup project, run the program, and the output should be the same as seen in the Testing the service with the WCF Test Client section in Chapter 9, Applying LINQ to Entities to a WCF Service.

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

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