Asp. NET create a Web service usage transaction

Source: Internet
Author: User
Tags add object end sql net string version web services
Asp.net|web|web Services | Creating a transaction that supports XML Web services leverages the support of the common language runtime, which is based on the same distributed transaction model as in Microsoft Transaction Server (MTS) and COM + services. The model is based on a clear judgement of whether an object participates in a transaction, rather than writing a specific code to handle the delegate and callback a transaction. For an XML Web service created using ASP.net, you can declare the transaction behavior of an XML Web service by setting the TransactionOption property of the WebMethod property that it applies to an XML Web service method. If an exception is thrown when the XML Web service method executes, the transaction automatically ends, and conversely, if no exception occurs, the transaction is automatically delegated.

The TransactionOption property of the WebMethod property stipulates how an XML Web service method participates in a transaction. Although this declaration level represents a transaction logic, it is a step towards eliminating the actual transaction. A real transaction occurs when a thing object accesses a data source, such as a database or message queue. The transaction that associates the object automatically flows to the appropriate resource management program. A. NET Framework data provider such as the. NET Framework Provider (for SQL Server or OLE DB) finds transactions in the context of an object and passes distributed Transaction Coordinator (DTC, distributed Transaction Coordinator) cataloging transactions. All transactions are generated automatically.

XML Web service methods can only participate in a transaction that is the root of a new transaction. As the root of a new transaction, all with the resource manager (like running Microsoft SQL Server, Microsoft Message Queuing, and Microsoft Host integration Server servers) to maintain the acid properties of a robust distributed application that needs to be run. XML Web service methods that invoke other XML Web service methods participate in different transactions because transactions do not flow through the XML Web service method.

   using transactions from the XML Web service method

Declares an XML Web service.

[C #]
<%@ WebService language= "C #" class= "Orders"%>
[Visual Basic]
<%@ WebService language= "VB" class= "Orders"%>

Add an assembly instruction to the System.EnterpriseServices.

<%@ Assembly name= "System.enterpriseservices,version=1.0.3300.0,culture=neutral,

PUBLICKEYTOKEN=B03F5F7F11D50A3A "%>


Add references to the System.Web.Services and System.EnterpriseServices domain namespace.

[C #]
Using System.Web.Services;
Using System.EnterpriseServices;
[Visual Basic]
Imports System.Web.Services
Imports System.EnterpriseServices

Declares an XML Web service method that sets the TransactionOption property of the WebMethod property to Transactionoption.requiresnew.

[C #]
[WebMethod (Transactionoption=transactionoption.requiresnew)]
public int Deleteauthor (string lastName)
[Visual Basic]
WebMethod (Transactionoption:=transactionoption.requiresnew) > _
Public Function Deleteauthor (LastName as String) as Integer

The following code example shows an XML Web service that uses a single XML Web service method to invoke DeleteDatabase. This XML Web service method performs a transaction-scoped database operation. If the database operation throws an exception, the transaction is automatically stopped, otherwise the transaction is automatically delegated.

[C #]
<%@ WebService language= "C #" class= "Orders"%>
<%@ Assembly name= "System.enterpriseservices,version=1.0.3300.0,culture=neutral,

PUBLICKEYTOKEN=B03F5F7F11D50A3A "%>
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Web.Services;
Using System.EnterpriseServices;

public class Orders:webservice
{
[WebMethod (Transactionoption=transactionoption.requiresnew)]
public int Deleteauthor (string lastName)
{
String deletecmd = "DELETE from authors WHERE au_lname= '" +
LastName + "'";
String exceptioncausingcmdsql = "DELETE from nonexistingtable WHERE
Au_lname= ' "+ lastName +" ' ";

SqlConnection sqlconn = new SqlConnection (
"Persist security info=false;integrated security=sspi;database=pubs;server=myserver");

SqlCommand deletecmd = new SqlCommand (deletecmdsql,sqlconn);
SqlCommand exceptioncausingcmd = new
SqlCommand (Exceptioncausingcmdsql,sqlconn);

This command should execute properly.
DeleteCmd.Connection.Open ();
Deletecmd.executenonquery ();

This command results in a exception
Automatically rolled back. Since the XML Web service method is
Participating in a transaction, and a exception occurs, asp.net
Automatically aborts the transaction. The deletecmd that
Executed properly is rolled.

int cmdresult = Exceptioncausingcmd.executenonquery ();

Sqlconn.close ();

return cmdresult;
}
}
[Visual Basic]
<%@ 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

<webmethod (Transactionoption:=transactionoption.requiresnew) > _
Public Function Deleteauthor (LastName as String) as Integer

Dim Deletecmdsql as String = "DELETE from authors WHERE au_lname= '" + _
LastName + "'"
Dim Exceptioncausingcmdsql as String = "DELETE from" + _
"Nonexistingtable WHERE au_lname= '" + lastName + "'"

Dim sqlconn As SqlConnection = New SqlConnection (_
"Persist security info=false;integrated Security=sspi;database=pubs;server=myserver")

Dim deletecmd as SqlCommand = New SqlCommand (deletecmdsql,sqlconn)
Dim exceptioncausingcmd as SqlCommand = New _
SqlCommand (Exceptioncausingcmdsql,sqlconn)

' This command should execute properly.
DeleteCmd.Connection.Open ()
Deletecmd.executenonquery ()

' This command results in a exception, so the '
' Automatically rolled back. Since the XML Web service method is
' Participating in a transaction, and a exception occurs, asp.net
' Automatically aborts the transaction. The deletecmd that
' Executed properly is rolled.

Dim Cmdresult as Integer = Exceptioncausingcmd.executenonquery ()
Sqlconn.close ()

Return Cmdresult
End Function
End Class



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.