asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET创建Web服务之使用事务


发布日期:2019年05月29日
 
ASP.NET创建Web服务之使用事务

支持XML Web服务的事务利用公共语言运行期中的支持其是基于Microsoft Transaction Server ( MTS)和COM+ Services中相同的分布式事务模型该模型基于明确的判断一个对象是否参与一个事务而不是编写特定的代码用来处理委托和回调一个事务对于一个使用ASPNET创建的XML Web服务你可以通过设置其应用到一个XML Web服务方法上的WebMethod属性的TransactionOption属性来声明一个XML Web服务的事务行为如果该XML Web服务方法执行的时候抛出一个异常那么该事务自动地结束相反如果没有发生异常该事务自动委托

WebMethod属性的TransactionOption属性规定一个XML Web服务方法如何参与一个事务虽然这个声明级别表示一个事务逻辑但是它是消除实际事务的一个步骤当事物对象访问数据源(如数据库或消息队列)时实际事务产生关联该对象的事务自动流向适当的资源管理程序NET Framework Data Provider(用于SQL Server或OLE DB)这样的NET Framework数据提供者在对象的上下文中查找事务并通过Distributed Transaction Coordinator (DTC分布式事务协调程序)编目事务全部的事务自动产生

XML Web服务方法只能参与一个作为新事务的根的事务作为一个新事务的根所有的与资源管理器(像运行Microsoft SQL ServerMicrosoft Message Queuing和Microsoft Host Integration Server的服务器)的相互作用维护需要运行健壮的分布式应用程序的ACID性质调用其他的XML Web服务方法的XML Web服务方法参与不同的事务因为事务不流经XML Web服务方法

使用来自XML Web服务方法的事务

声明一个XML Web服务

[C#]

<%@ WebService Language=C# Class=Orders %>

[Visual Basic]

<%@ WebService Language=VB Class=Orders %>

把一个汇编指令加到SystemEnterpriseServices上

<%@ Assembly name=SystemEnterpriseServicesVersion=Culture=neutralPublicKeyToken=bfffdaa %>

添加引用到SystemWebServices和SystemEnterpriseServices域名空间

[C#]

using SystemWebServices;

using SystemEnterpriseServices;

[Visual Basic]

Imports SystemWebServices

Imports SystemEnterpriseServices

声明一个XML Web服务方法设置WebMethod属性的TransactionOption属性为TransactionOptionRequiresNew

[C#]

[ WebMethod(TransactionOption=TransactionOptionRequiresNew)]

public int DeleteAuthor(string lastName)

[Visual Basic]

< WebMethod(TransactionOption:=TransactionOptionRequiresNew)> _

Public Function DeleteAuthor(lastName As String) As Integer

下面的代码示例显示一个使用单个XML Web服务方法的XML Web服务调用DeleteDatabase这个XML Web服务方法执行一个事务范围内的数据库操作如果该数据库操作抛出一个异常该事务自动地停止否则该事务自动地委托

[C#]

<%@ WebService Language=C# Class=Orders %>

<%@ Assembly name=SystemEnterpriseServicesVersion=Culture=neutralPublicKeyToken=bfffdaa %>

using System;

using SystemData;

using SystemDataSqlClient;

using SystemWebServices;

using SystemEnterpriseServices;

public class Orders : WebService

{

[ WebMethod(TransactionOption=TransactionOptionRequiresNew)]

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(deleteCmdSQLsqlConn);

SqlCommand exceptionCausingCmd = new

SqlCommand(exceptionCausingCmdSQLsqlConn);

// This command should execute properly

deleteCmdConnectionOpen();

deleteCmdExecuteNonQuery();

// This command results in an exception so the first command is

// automatically rolled back Since the XML Web service method is

// participating in a transaction and an exception occurs ASPNET

// automatically aborts the transaction The deleteCmd that

// executed properly is rolled back

int cmdResult = exceptionCausingCmdExecuteNonQuery();

sqlConnClose();

return cmdResult;

}

}

[Visual Basic]

<%@ WebService Language=VB Class=Orders %>

<%@ assembly name=SystemEnterpriseServices %>

Imports System

Imports SystemData

Imports SystemDataSqlClient

Imports SystemWebServices

Imports SystemWebUtil

Imports SystemEnterpriseServices

Public Class Orders

<WebMethod(TransactionOption:=TransactionOptionRequiresNew)> _

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(deleteCmdSQLsqlConn)

Dim exceptionCausingCmd As SqlCommand = New _

SqlCommand(exceptionCausingCmdSQLsqlConn)

This command should execute properly

deleteCmdConnectionOpen()

deleteCmdExecuteNonQuery()

This command results in an exception so the first command is

automatically rolled back Since the XML Web service method is

participating in a transaction and an exception occurs ASPNET

automatically aborts the transaction The deleteCmd that

executed properly is rolled back

Dim cmdResult As Integer = exceptionCausingCmdExecuteNonQuery()

sqlConnClose()

Return cmdResult

End Function

End Class

               

上一篇:ASP.NET开发中关于Web标准的几点建议

下一篇:解读ASP.NET Portal Starter Kit(2)