转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
ASP.NET创建Web服务之使用事务         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:808 更新时间:2009/4/23 10:33:01
   支持XML Web服务的事务利用公共语言运行期中的支持,其是基于Microsoft Transaction Server ( MTS)和COM+ Services中相同的分布式事务模型。该模型基于明确的判断一个对象是否参与一个事务,而不是编写特定的代码用来处理委托和回调一个事务。对于一个使用ASP.NET创建的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 Server、Microsoft 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" %>
  把一个汇编指令加到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 Class157


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台