Automatic Transactions and XML Web Services
ASP.NET provides built-in support for creating and exposing XML Web services using a programming abstraction that is consistent and familiar to Web Forms. The resulting model is scalable, extensible, and embraces HTTP, XML, SOAP, and WSDL open Internet standards, among others. By supporting open standards, XML Web services can be accessed and consumed by any client or Internet-enabled device.
XML Web services provide to you the option of running your code within the scope of an automatic transaction. A transaction ensures that all interactions with resource managers such as SQL Servers, Message Queuing, Oracle Servers, and SNA Servers maintain the ACID Properties required to run robust distributed applications.
You can declare an automatic transaction by using the TransactionOption property of the WebMethodAttribute attribute class. Setting the TransactionOption property to TransactionOption.RequiresNew begins a new transaction each time an XML Web service client calls the XML Web service method.
The following code fragment shows a service that exposes a single XML Web service method, called DeleteAuthor
. This XML Web service method performs a database operation that is scoped within an automatic transaction.
<%@ WebService Language="VB" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Util
Imports System.EnterpriseServices
Public Class Orders
Inherits WebService
<WebMethod(TransactionOption := TransactionOption.RequiresNew)> _
Public Function DeleteAuthor(lastName As String) As Integer
Dim deleteCmd As [String] = "DELETE FROM authors2 where au_lname='"
& lastName & "'"
Dim sqlConn As New SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver")
Dim myCommand As New SqlCommand(deleteCmd, sqlConn)
' If a XML Web service method is participating in a transaction and
' an exception occurs, ASP.NET automatically aborts the transaction.
' Likewise, if no exception occurs, then the transaction is
' automatically committed.
myCommand.Connection.Open()
Return myCommand.ExecuteNonQuery()
End Function
End Class
[C#]<%@ WebService Language="C#" Class="Orders" %>
<%@ assembly name="System.EnterpriseServices" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.Util;
using System.EnterpriseServices;
public class Orders : WebService
{
[ WebMethod(TransactionOption=TransactionOption.RequiresNew)]
public int DeleteAuthor(string lastName)
{
String deleteCmd = "DELETE FROM authors2
where au_lname='" + lastName + "'" ;
SqlConnection sqlConn = new SqlConnection("Integrated Security=SSPI;database=pubs;server=myserver");
SqlCommand myCommand = new SqlCommand(deleteCmd,sqlConn);
// If a XML Web service method is participating in a transaction and an
// exception occurs, ASP.NET automatically aborts the transaction.
// Likewise, if no exception occurs, then the transaction is
// automatically committed.
myCommand.Connection.Open();
return myCommand.ExecuteNonQuery();
}
}
Note A transaction begins only when the activating XML Web service method, which is the method called from the client, has transaction metadata. If the activating XML Web service method does not carry the appropriate transaction metadata, subsequent XML Web service methods can neither participate in an existing transaction nor begin a new transaction.
See Also
Automatic Transactions | Distributed Transactions | Participating in Transactions in XML Web Services Created Using ASP.NET