http://www.cnblogs.com/webabcd/archive/2008/06/24/1229049.html

 

化零为整WCF(14) - 事务(Transaction)



作者:webabcd


介绍
WCF(Windows Communication Foundation) - 事务(Transaction):
    ·对契约方法使用TransactionFlowAttribute声明(设置TransactionFlowOption参数),以指定服务操作的事务流策略
    ·对
服务方法是用OperationBehaviorAttribute声明(设置TransactionScopeRequired参数),以指定方法是否在事务范围(TransactionScope)内执行
    ·
配置host和client的binding节点的transactionFlow属性,以指定绑定是否支持流事务


示例
1、服务
Hello.cs

【转载】化零为整WCF(14) - 事务(Transaction)using System;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Collections.Generic;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Linq;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Text;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
using System.ServiceModel;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
namespace WCF.ServiceLib.Transaction


Hi.cs

【转载】化零为整WCF(14) - 事务(Transaction)using System;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Collections.Generic;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Linq;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Text;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
using System.ServiceModel;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
namespace WCF.ServiceLib.Transaction


Result.cs

【转载】化零为整WCF(14) - 事务(Transaction)using System;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Collections.Generic;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Linq;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Text;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
using System.ServiceModel;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
namespace WCF.ServiceLib.Transaction



2、宿主
Hello.svc

%>


Hi.svc

%>


Result.svc

%>


Web.config

【转载】化零为整WCF(14) - 事务(Transaction)<?xml version="1.0"?>
【转载】化零为整WCF(14) - 事务(Transaction)
<configuration>
【转载】化零为整WCF(14) - 事务(Transaction)    
<system.serviceModel>
【转载】化零为整WCF(14) - 事务(Transaction)        
<behaviors>
【转载】化零为整WCF(14) - 事务(Transaction)            
<serviceBehaviors>
【转载】化零为整WCF(14) - 事务(Transaction)                
<behavior name="TransactionBehavior">
【转载】化零为整WCF(14) - 事务(Transaction)                    
<!--httpGetEnabled - 指示是否发布服务元数据以便使用 HTTP/GET 请求进行检索,如果发布 WSDL,则为 true,否则为 false,默认值为 false-->
【转载】化零为整WCF(14) - 事务(Transaction)                    
<serviceMetadata httpGetEnabled="true" />
【转载】化零为整WCF(14) - 事务(Transaction)                    
<serviceDebug includeExceptionDetailInFaults="true"/>
【转载】化零为整WCF(14) - 事务(Transaction)                
</behavior>
【转载】化零为整WCF(14) - 事务(Transaction)            
</serviceBehaviors>
【转载】化零为整WCF(14) - 事务(Transaction)        
</behaviors>
【转载】化零为整WCF(14) - 事务(Transaction)        
<services>
【转载】化零为整WCF(14) - 事务(Transaction)            
<!--name - 提供服务的类名-->
【转载】化零为整WCF(14) - 事务(Transaction)            
<!--behaviorConfiguration - 指定相关的行为配置-->
【转载】化零为整WCF(14) - 事务(Transaction)            
<service name="WCF.ServiceLib.Transaction.Hello" behaviorConfiguration="TransactionBehavior">
【转载】化零为整WCF(14) - 事务(Transaction)                
<!--address - 服务地址-->
【转载】化零为整WCF(14) - 事务(Transaction)                
<!--binding - 通信方式-->
【转载】化零为整WCF(14) - 事务(Transaction)                
<!--contract - 服务契约-->
【转载】化零为整WCF(14) - 事务(Transaction)                
<!--bindingConfiguration - 指定相关的绑定配置-->
【转载】化零为整WCF(14) - 事务(Transaction)                
<endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.Transaction.IHello" bindingConfiguration="TransactionConfiguration" />
【转载】化零为整WCF(14) - 事务(Transaction)            
</service>
【转载】化零为整WCF(14) - 事务(Transaction)            
<service name="WCF.ServiceLib.Transaction.Hi" behaviorConfiguration="TransactionBehavior">
【转载】化零为整WCF(14) - 事务(Transaction)                
<endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.Transaction.IHi" bindingConfiguration="TransactionConfiguration" />
【转载】化零为整WCF(14) - 事务(Transaction)            
</service>
【转载】化零为整WCF(14) - 事务(Transaction)            
<service name="WCF.ServiceLib.Transaction.Result" behaviorConfiguration="TransactionBehavior">
【转载】化零为整WCF(14) - 事务(Transaction)                
<endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.Transaction.IResult" />
【转载】化零为整WCF(14) - 事务(Transaction)            
</service>
【转载】化零为整WCF(14) - 事务(Transaction)        
</services>
【转载】化零为整WCF(14) - 事务(Transaction)        
<bindings>
【转载】化零为整WCF(14) - 事务(Transaction)            
<wsHttpBinding>
【转载】化零为整WCF(14) - 事务(Transaction)                
<!--transactionFlow - 指定该绑定是否应支持流事务-->
【转载】化零为整WCF(14) - 事务(Transaction)                
<binding name="TransactionConfiguration" transactionFlow="true" />
【转载】化零为整WCF(14) - 事务(Transaction)            
</wsHttpBinding>
【转载】化零为整WCF(14) - 事务(Transaction)        
</bindings>
【转载】化零为整WCF(14) - 事务(Transaction)    
</system.serviceModel>
【转载】化零为整WCF(14) - 事务(Transaction)
</configuration>
【转载】化零为整WCF(14) - 事务(Transaction)



3、客户端
Sample.aspx


Sample.aspx.cs

【转载】化零为整WCF(14) - 事务(Transaction)using System;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Collections;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Configuration;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Data;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Linq;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Web;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Web.Security;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Web.UI;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Web.UI.HtmlControls;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Web.UI.WebControls;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Web.UI.WebControls.WebParts;
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Xml.Linq;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
using System.Threading;
【转载】化零为整WCF(14) - 事务(Transaction)
【转载】化零为整WCF(14) - 事务(Transaction)
public partial class Transaction_Sample : System.Web.UI.Page


Web.config

【转载】化零为整WCF(14) - 事务(Transaction)<?xml version="1.0"?>
【转载】化零为整WCF(14) - 事务(Transaction)
<configuration>
【转载】化零为整WCF(14) - 事务(Transaction)    
<system.serviceModel>
【转载】化零为整WCF(14) - 事务(Transaction)        
<client>
【转载】化零为整WCF(14) - 事务(Transaction)            
<!--address - 服务地址-->
【转载】化零为整WCF(14) - 事务(Transaction)            
<!--binding - 通信方式-->
【转载】化零为整WCF(14) - 事务(Transaction)            
<!--contract - 服务契约-->
【转载】化零为整WCF(14) - 事务(Transaction)            
<endpoint address="http://localhost:3502/ServiceHost/Transaction/Hello.svc" binding="wsHttpBinding" contract="TransactionSvc.Hello.IHello" bindingConfiguration="TransactionBindingConfiguration" />
【转载】化零为整WCF(14) - 事务(Transaction)            
<endpoint address="http://localhost:3502/ServiceHost/Transaction/Hi.svc" binding="wsHttpBinding" contract="TransactionSvc.Hi.IHi" bindingConfiguration="TransactionBindingConfiguration" />
【转载】化零为整WCF(14) - 事务(Transaction)            
<endpoint address="http://localhost:3502/ServiceHost/Transaction/Result.svc" binding="basicHttpBinding" contract="TransactionSvc.Result.IResult" />
【转载】化零为整WCF(14) - 事务(Transaction)        
</client>
【转载】化零为整WCF(14) - 事务(Transaction)        
<bindings>
【转载】化零为整WCF(14) - 事务(Transaction)            
<wsHttpBinding>
【转载】化零为整WCF(14) - 事务(Transaction)                
<!--transactionFlow - 指定该绑定是否应支持流事务-->
【转载】化零为整WCF(14) - 事务(Transaction)                
<binding name="TransactionBindingConfiguration" transactionFlow="true" />
【转载】化零为整WCF(14) - 事务(Transaction)            
</wsHttpBinding>
【转载】化零为整WCF(14) - 事务(Transaction)        
</bindings>
【转载】化零为整WCF(14) - 事务(Transaction)    
</system.serviceModel>
【转载】化零为整WCF(14) - 事务(Transaction)
</configuration>
【转载】化零为整WCF(14) - 事务(Transaction)



运行结果:
单击"btnSubmit"按钮后,可以发现,两个数据库插入操作,要么都执行,要么都不执行

相关文章:

  • 2022-02-22
  • 2021-10-16
  • 2021-09-09
  • 2021-10-21
  • 2021-10-28
  • 2021-11-13
  • 2021-10-01
猜你喜欢
  • 2021-12-05
  • 2021-09-26
  • 2021-11-06
  • 2021-08-26
  • 2021-07-17
  • 2021-06-23
相关资源
相似解决方案