XML Web Service Method of ASP. NET

Source: Internet
Author: User

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.

 
 
  1. <%@WebServiceLanguage="C#"Class="Orders"%> 
  2. <%@Assemblyname="System.EnterpriseServices,Version=1.0.3300.0,
    Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"%> 
  3. usingSystem;  
  4. usingSystem.Data;  
  5. usingSystem.Data.SqlClient;  
  6. usingSystem.Web.Services;  
  7. usingSystem.EnterpriseServices;  
  8.  
  9. publicclassOrders:WebService  
  10. {  
  11. [WebMethod(TransactionOptionTransactionOption=TransactionOption.RequiresNew)]  
  12. publicintDeleteAuthor(stringlastName)  
  13. {  
  14. StringdeleteCmd="DELETEFROMauthorsWHEREau_lname='"+  
  15. lastName+"'";  
  16. StringexceptionCausingCmdSQL="DELETEFROMNonExistingTableWHERE  
  17. au_lname='"+lastName+"'";  
  18.  
  19. SqlConnectionsqlConn=newSqlConnection(  
  20. "PersistSecurityInfo=False;IntegratedSecurity=SSPI;database=pubs;server=myserver");  
  21.  
  22. SqlCommanddeleteCmd=newSqlCommand(deleteCmdSQL,sqlConn);  
  23. SqlCommandexceptionCausingCmd=new 
  24. SqlCommand(exceptionCausingCmdSQL,sqlConn);  
  25.  
  26. //Thiscommandshouldexecuteproperly.  
  27. deleteCmd.Connection.Open();  
  28. deleteCmd.ExecuteNonQuery();  
  29.  
  30. //Thiscommandresultsinanexception,sothefirstcommandis  
  31. //automaticallyrolledback.SincetheXMLWebservicemethodis  
  32. //participatinginatransaction,andanexceptionoccurs,ASP.NET  
  33. //automaticallyabortsthetransaction.ThedeleteCmdthat  
  34. //executedproperlyisrolledback.  
  35.  
  36. intcmdResult=exceptionCausingCmd.ExecuteNonQuery();  
  37.  
  38. sqlConn.Close();  
  39.  
  40. returncmdResult;  
  41. }  
  42. }  
  43. [VisualBasic]  
  44. <%@WebServiceLanguage="VB"Class="Orders"%> 
  45. <%@assemblyname="System.EnterpriseServices"%> 
  46.  
  47. ImportsSystem  
  48. ImportsSystem.Data  
  49. ImportsSystem.Data.SqlClient  
  50. ImportsSystem.Web.Services  
  51. ImportsSystem.Web.Util  
  52. ImportsSystem.EnterpriseServices  
  53.  
  54. PublicClassOrders  
  55.  
  56. <WebMethod(TransactionOptionTransactionOption:=TransactionOption.RequiresNew)>_  
  57. PublicFunctionDeleteAuthor(lastNameasString)asInteger  
  58.  
  59. DimdeleteCmdSQLAsString="DELETEFROMauthorsWHEREau_lname='"+_  
  60. lastName+"'"  
  61. DimexceptionCausingCmdSQLAsString="DELETEFROM"+_  
  62. "NonExistingTableWHEREau_lname='"+lastName+"'"  
  63.  
  64. DimsqlConnAsSqlConnection=NewSqlConnection(_  
  65. "PersistSecurityInfo=False;IntegratedSecurity=SSPI;database=pubs;server=myserver")  
  66.  
  67. DimdeleteCmdAsSqlCommand=NewSqlCommand(deleteCmdSQL,sqlConn)  
  68. DimexceptionCausingCmdAsSqlCommand=New_ 
  69. SqlCommand(exceptionCausingCmdSQL,sqlConn)  
  70.  
  71. 'Thiscommandshouldexecuteproperly.  
  72. deleteCmd.Connection.Open()  
  73. deleteCmd.ExecuteNonQuery()  
  74.  
  75. 'Thiscommandresultsinanexception,sothefirstcommandis  
  76. 'automaticallyrolledback.SincetheXMLWebservicemethodis  
  77. 'participatinginatransaction,andanexceptionoccurs,ASP.NET  
  78. 'automaticallyabortsthetransaction.ThedeleteCmdthat  
  79. 'executedproperlyisrolledback.  
  80.  
  81. DimcmdResultAsInteger=exceptionCausingCmd.ExecuteNonQuery()  
  82. sqlConn.Close()  
  83.  
  84. ReturncmdResult  
  85. EndFunction  
  86. EndClass 
  1. XML data displayed on the ASP. NET page
  2. What is iframe asp. NET?
  3. ASP. NET lifecycle display
  4. Solve the Problem of ASP. net ajax script errors
  5. ASP. NET Applications

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.