4.WCF事务【Transaction】
生活随笔
收集整理的這篇文章主要介紹了
4.WCF事务【Transaction】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
契約:
namespace Rhythmk.Contracts{
[ServiceContract(Namespace="http://wwww.wangkun.com")]
public interface ICalculate
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
void OperationTransaction(int i);
/*
TransactionFlow - 指定服務操作是否愿意接受來自客戶端的傳入事務
NotAllowed - 禁止事務。默認值
Allowed - 允許事務
Mandatory - 強制事務
*/
[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
void OperationTransactionExe(int i);
}
}
服務:
using System.ServiceModel;using Rhythmk.Contracts;
namespace Rhythmk.Services
{
/// <summary>
/// 計算器
/// </summary>
public class Calculate : ICalculate
{
#region ICalculate 成員
/// OperationBehavior - 指定服務方法的本地執行行為
/// 1、TransactionScopeRequired - 如果方法需要事務范圍才能執行,則為 true;否則為 false。默認值為 false
/// 將 TransactionScopeRequired 設置為 true,可以要求操作在事務范圍內執行。如果流事務可用,則操作會在該事務內執行。如果流事務不可用,則會創建一個新事務并使用它來執行操作
/// 2、TransactionAutoComplete - 默認值為 true
/// true - 當方法完成執行時,將把該事務標志為完成(自動提交事務)
/// false - 需要調用OperationContext.Current.SetTransactionComplete()方法來手工配置該事務的正確完成;否則,該事務將被標志為失敗(手動提交事務)
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void OperationTransaction(int i)
{
string sql = "INSERT INTO TB_TEST (name,txt,addtime) VALUES ( 'OperationTransaction','"+(i*i).ToString()+"',GETDATE())";
SqlHelp.ExcuteSqlServer(sql);
}
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void OperationTransactionExe(int i)
{
if (i % 10 == 0)
{
throw new Exception("測試事務異常!");
}
string sql = "INSERT INTO TB_TEST (name,txt,addtime) VALUES ( 'OperationTransactionExe','" + (i * i).ToString() + "',GETDATE())";
SqlHelp.ExcuteSqlServer(sql);
}
#endregion
}
}
寄宿
class Program{
static void Main(string[] args)
{
CreateCalculateService();
}
static void CreateCalculateService()
{
Console.WriteLine("----------CreateCalculateService---Star---------");
using (ServiceHost host = new ServiceHost(typeof(Calculate)))
{
host.Opened += delegate { Console.WriteLine("CalculateService已經啟動,按任意鍵終止服務!"); };
host.Open();
Console.Read();
}
}
}
服務端配置:
? 備注:??????? <binding name="TransactionConfig" transactionFlow="true"></binding>? 需要在配置中開啟事務流
<?xml version="1.0" encoding="utf-8"?><configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metaBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Rhythmk.Services.Calculate" behaviorConfiguration="metaBehavior" >
<endpoint address=""
binding="wsHttpBinding" bindingConfiguration="TransactionConfig" contract="Rhythmk.Contracts.ICalculate" >
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:1234/Rhythmk.Services.Calculate"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="TransactionConfig" transactionFlow="true"></binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
通過服務引用調用事務:
public void ExcuteTransaction(int i){
RhythmWCF.ICalculate proxy = new RhythmWCF.CalculateClient();
System.Transactions.TransactionOptions tran = new System.Transactions.TransactionOptions();
//設置事務超時時間
tran.Timeout = new TimeSpan(3000);
//設置事務的隔離級別
tran.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
using (var ts = new System.Transactions.TransactionScope())
{
try
{
proxy.OperationTransaction(i);
// 輸入為10的倍數 將會報告異常 .
proxy.OperationTransactionExe(i);
ts.Complete(); //提交事務
}
catch (Exception ex)
{
lb.Text = ex.ToString();
}
}
}
總結
以上是生活随笔為你收集整理的4.WCF事务【Transaction】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php与java安全之争
- 下一篇: 转:PostgreSQL角色、用户、权限