ASP.NET创建Web服务之使用事务
来源:互联网 发布:二手货如何在淘宝上卖 编辑:程序博客网 时间:2024/05/15 23:50
WebMethod属性的TransactionOption属性规定一个XML Web服务方法如何参与一个事务。虽然这个声明级别表示一个事务逻辑,但是它是消除实际事务的一个步骤。当事物对象访问数据源(如数据库或消息队列)时实际事务产生。关联该对象的事务自动流向适当的资源管理程序。像.NET Framework Data Provider(用于SQL Server或OLE DB)这样的.NET Framework数据提供者在对象的上下文中查找事务并通过Distributed Transaction Coordinator (DTC,分布式事务协调程序)编目事务。全部的事务自动产生。
XML Web服务方法只能参与一个作为新事务的根的事务。作为一个新事务的根,所有的与资源管理器(像运行Microsoft SQL Server、Microsoft Message Queuing和Microsoft Host Integration Server的服务器)的相互作用维护需要运行健壮的分布式应用程序的ACID性质。调用其他的XML Web服务方法的XML Web服务方法参与不同的事务,因为事务不流经XML Web服务方法。
声明一个XML Web服务。
[C#]
<%@ WebService Language="C#" Class="Orders" %>
[Visual Basic]
<%@ WebService Language="VB" Class="Orders" %>
把一个汇编指令加到System.EnterpriseServices上。
<%@ Assembly name="System.EnterpriseServices,Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" %>
添加引用到System.Web.Services和System.EnterpriseServices域名空间。
[C#]
using System.Web.Services;
using System.EnterpriseServices;
[Visual Basic]
Imports System.Web.Services
Imports System.EnterpriseServices
声明一个XML Web服务方法,设置WebMethod属性的TransactionOption属性为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
下面的代码示例显示一个使用单个XML Web服务方法的XML Web服务,调用DeleteDatabase。这个XML Web服务方法执行一个事务范围内的数据库操作。如果该数据库操作抛出一个异常,该事务自动地停止;否则,该事务自动地委托。
[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 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, ASP.NET
// automatically aborts the transaction. The deleteCmd that
// executed properly is rolled back.
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 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, ASP.NET
' automatically aborts the transaction. The deleteCmd that
' executed properly is rolled back.
Dim cmdResult As Integer = exceptionCausingCmd.ExecuteNonQuery()
sqlConn.Close()
Return cmdResult
End Function
End Class
- ASP.NET创建Web服务之使用事务
- ASP.NET创建Web服务之使用事务
- ASP.NET创建Web服务之使用事务
- ASP.NET创建Web服务之声明
- ASP.NET创建Web服务之异步Web服务
- ASP.NET创建Web服务之管理Web服务状态
- ASP.NET创建Web服务之异步Web服务
- ASP.NET创建Web服务之管理Web服务状态
- ASP.NET创建Web服务之异步Web
- ASP.NET创建Web服务之发布和部署
- ASP.NET创建Web服务之设计方针
- ASP.NET创建Web服务之设计方针
- ASP.NET创建Web服务之XML基础结构
- ASP.NET创建Web服务之设计方针
- ASP.NET创建Web服务之设计方针
- ASP.NET创建Web服务之XML基础结构
- ASP.NET创建Web服务之XML基础结构
- 使用 ASP.NET 创建的 XML Web 服务简介
- 在ASP.NET中实现多文件上传
- asp.net(vb.net)连接sqlserver2000数据库的连接模块(module)
- 一个简单的ASP.NETForms身份认证
- 从ASP过渡到ASP.net遗留的二十大积习
- 去除隔间,增进沟通
- ASP.NET创建Web服务之使用事务
- ASP.NET提供文件下载函数
- ASP.net生成文字图片
- 在Asp.Net中使用SmtpMail发送邮件的方法
- 转换DataSet到普通xml的新法
- 如何显示在线人数和所在位置
- asp.net实现C#代码加亮显示
- ASP.NET中使用数据处理插入数据注意的问题
- 如何取得IP/用户名等信息