ASP. NET supports XML Web Service methods. transactions are supported by common language runtime. They are based on the same Distributed Transaction Model in Microsoft Transaction Server (MTS) and COM + Services. This model clearly determines whether an object participates in a transaction, rather than writing specific code to process the delegate and callback of a transaction. For an ASP. NET, you can declare the transaction behavior of an XML Web Service by setting the TransactionOption attribute of the WebMethod attribute applied to an XML Web Service method. If an exception is thrown when the XML Web Service method is executed, the transaction ends automatically. On the contrary, if no exception occurs, the transaction is automatically delegated.
The TransactionOption attribute of the WebMethod attribute specifies how an XML Web Service method participates in a transaction. Although this declaration level represents a transaction logic, it is a step to eliminate the actual transaction. When a transaction object accesses a data source, such as a database or message queue, the actual transaction is generated. The transactions associated with this object are automatically routed to appropriate resource management programs. Like. NET Framework Data Provider for SQL Server or OLE DB. NET Framework data provider searches for transactions in the context of the object and cataloguing the transactions through Distributed Transaction Coordinator (DTC, Distributed Transaction Coordination Program. All transactions are automatically generated.
The XML Web Service method can only participate in one transaction as the root of the new transaction. As the root of a new transaction, all the servers running Microsoft SQL Server, Microsoft Message Queuing, and Microsoft Host Integration Server with the Resource Manager) to maintain the ACID properties of distributed applications. The XML Web Service method that calls other XML Web Service Methods participates in different transactions because the transactions do not flow through the XML Web Service methods.
ASP. NET transactions using XML Web Service Methods
Declare an XML Web Service.
- <%@WebServiceLanguage="C#"Class="Orders"%>
- <%@Assemblyname="System.EnterpriseServices,Version=1.0.3300.0,
Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"%>
- usingSystem;
- usingSystem.Data;
- usingSystem.Data.SqlClient;
- usingSystem.Web.Services;
- usingSystem.EnterpriseServices;
-
- publicclassOrders:WebService
- {
- [WebMethod(TransactionOptionTransactionOption=TransactionOption.RequiresNew)]
- publicintDeleteAuthor(stringlastName)
- {
- StringdeleteCmd="DELETEFROMauthorsWHEREau_lname='"+
- lastName+"'";
- StringexceptionCausingCmdSQL="DELETEFROMNonExistingTableWHERE
- au_lname='"+lastName+"'";
-
- SqlConnectionsqlConn=newSqlConnection(
- "PersistSecurityInfo=False;IntegratedSecurity=SSPI;database=pubs;server=myserver");
-
- SqlCommanddeleteCmd=newSqlCommand(deleteCmdSQL,sqlConn);
- SqlCommandexceptionCausingCmd=new
- SqlCommand(exceptionCausingCmdSQL,sqlConn);
-
- //Thiscommandshouldexecuteproperly.
- deleteCmd.Connection.Open();
- deleteCmd.ExecuteNonQuery();
-
- //Thiscommandresultsinanexception,sothefirstcommandis
- //automaticallyrolledback.SincetheXMLWebservicemethodis
- //participatinginatransaction,andanexceptionoccurs,ASP.NET
- //automaticallyabortsthetransaction.ThedeleteCmdthat
- //executedproperlyisrolledback.
-
- intcmdResult=exceptionCausingCmd.ExecuteNonQuery();
-
- sqlConn.Close();
-
- returncmdResult;
- }
- }
- [VisualBasic]
- <%@WebServiceLanguage="VB"Class="Orders"%>
- <%@assemblyname="System.EnterpriseServices"%>
-
- ImportsSystem
- ImportsSystem.Data
- ImportsSystem.Data.SqlClient
- ImportsSystem.Web.Services
- ImportsSystem.Web.Util
- ImportsSystem.EnterpriseServices
-
- PublicClassOrders
-
- <WebMethod(TransactionOptionTransactionOption:=TransactionOption.RequiresNew)>_
- PublicFunctionDeleteAuthor(lastNameasString)asInteger
-
- DimdeleteCmdSQLAsString="DELETEFROMauthorsWHEREau_lname='"+_
- lastName+"'"
- DimexceptionCausingCmdSQLAsString="DELETEFROM"+_
- "NonExistingTableWHEREau_lname='"+lastName+"'"
-
- DimsqlConnAsSqlConnection=NewSqlConnection(_
- "PersistSecurityInfo=False;IntegratedSecurity=SSPI;database=pubs;server=myserver")
-
- DimdeleteCmdAsSqlCommand=NewSqlCommand(deleteCmdSQL,sqlConn)
- DimexceptionCausingCmdAsSqlCommand=New_
- SqlCommand(exceptionCausingCmdSQL,sqlConn)
-
- 'Thiscommandshouldexecuteproperly.
- deleteCmd.Connection.Open()
- deleteCmd.ExecuteNonQuery()
-
- 'Thiscommandresultsinanexception,sothefirstcommandis
- 'automaticallyrolledback.SincetheXMLWebservicemethodis
- 'participatinginatransaction,andanexceptionoccurs,ASP.NET
- 'automaticallyabortsthetransaction.ThedeleteCmdthat
- 'executedproperlyisrolledback.
-
- DimcmdResultAsInteger=exceptionCausingCmd.ExecuteNonQuery()
- sqlConn.Close()
-
- ReturncmdResult
- EndFunction
- EndClass
- XML data displayed on the ASP. NET page
- What is iframe asp. NET?
- ASP. NET lifecycle display
- Solve the Problem of ASP. net ajax script errors
- ASP. NET Applications