May 26, 2020
Hot Topics:

Develop Transactional .NET Web Services

  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

With distributed applications becoming pervasive, you need tools and techniques to create, destroy, and monitor transactions across multiple platforms and servers. Having these tools and techniques available within a single architecture such as the .NET Framework makes it easy to develop distributed applications and handle transactions. This article looks at the transaction support the .NET Framework provides, which you can use to develop transactional .NET Web services.

Introduction to Transactions

Transaction processing is easy as long as the transaction involves only a single database. Unfortunately, a company rarely stores its data in a single database. In many cases, companies spread the information out over many databases. For example, an online retailer might store customer account information in a SQL Server database at the corporate office. But, it may store order-fulfillment information in an Oracle database at a partner company's warehouse. When a customer places an order, the online retailer should debit the customer's account in the SQL Server database at the corporate office and write an order record to the partner company's Oracle database. So in this case, the retailer needs to perform the updates on both of these databases as a single transaction. If either operation fails, each resource manager will have to roll back its part of the transaction. This type of transaction is called a distributed transaction.

Considering the number of entities that may be involved in a distributed transaction, it may get really complex and require a lot of code for its support. After seeing the amount of time developers spent writing this type of plumbing code, Microsoft decided to build distributed transaction capabilities into the .NET Web services API. As its name suggests, this API provides for most of the plumbing code required to automate distributed transactions across .NET Web services.

The .NET Framework encapsulates most of the plumbing required for creating distributed, transaction-based Web services in easy-to-use, very flexible classes. These classes are contained in the System.EnterpriseServices namespace, and they rely on the services COM+ provides for supporting automatic transactions.

If you have done any MTS/COM+ programming using Visual Studio 5, you know that for a COM component to use COM+ services, it needs to have a reference to the ObjectContext interface that encapsulates most of the core COM+ functionality. The .NET framework has a class named ContextUtil that acts as a wrapper around the ObjectContext class. It also is contained in the System.EnterpriseServices namespace. The methods in the ContextUtil class are the primary means by which applications written in managed code communicate with Microsoft Distributed Transaction Coordinator (MSDTC).

One of the most important features the ContextUtil class provides is Transaction Processing Monitoring (TPM). The MSDTC internally maintains two flags through COM+, done bit and consistency bit, which you use not only to vote for the outcome of a transaction but also to control the deactivation of objects. Use the following methods to explore the combinations of done and consistency bits and indicate to the COM+ runtime the state of the transaction and whether the object can be deactivated:

  • SetComplete: This method sets both done and consistency flags to true, which indicates that the current transaction can be committed and the object deactivated.
  • SetAbort: This method sets the done bit to true and consistency flag to false, which indicates that the current transaction has to be rolled back, and the object deactivated.
  • EnableCommit: This method sets the done bit to false and consistency flag to true.
  • DisableCommit: This method sets both done and consistency flags to false.

The above-mentioned methods allow you to control both the transactional and the activation behavior of an object. However, you sometimes may want to individually manipulate the done bit and consistency bit for a finer level of control over the transactions. The ContextUtil class exposes the following two properties through which you can deal with the done bit and consistency bits individually:

  • MyTransactionVote: a property get and set through which you can either get or set the value of the consistency bit
  • DeactivateOnReturn: a property get and set that you use to either get or set the value of the done bit

You can programmatically specify whether you want to commit or rollback the transaction in a Web service in the following two ways:

  • Using the utility methods (explained above) that the ContextUtil class supplies
  • Using the AutoComplete attribute class

You will see both of these methods in action later.

This article demonstrates how to take advantage of the built-in transaction capabilities that .NET Web services provides when creating managed applications in the .NET Framework. It is sectioned into two parts:

  • The first part shows you how to create two transactional Web service methods. Within these methods, you will use the ContextUtil class to control transactions.
  • The second part shows you how to create Web service clients that will access your transactional Web services. While creating this Web service client, you also will learn how a transaction initiated by a Web service or a Web page can be used to control the transactional behavior of the consumed Web services.

Page 1 of 4

This article was originally published on July 26, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date