首页 新闻 会员 周边 捐助

WCF 分布式事务 不会RollBack

0
悬赏园豆:40 [已解决问题] 解决于 2013-07-01 21:00

要使用分布式事务 写了个例子  配置MSDTC  但事务不会回滚啊
using System.ServiceModel;
namespace School.Wcf.Contract
{
[ServiceContract]
public interface IStudentService
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void Add(Student entity);

}

 

using System.ServiceModel;
using System.Transactions;
using School.Entity;
using School.DAL;
using School.Wcf.Contract;
namespace School.Wcf.Service
{
public class StudentService:IStudentService
{

[OperationBehavior(TransactionScopeRequired=true)]
void IStudentService.Add(Student entity)
{
Transaction tran = Transaction.Current;

DalCreater.CreateStudentDal().Add(entity);
}

}

 

namespace ClientForCAHost
{
public class StudentMng
{
StudentServiceClient client;

public StudentMng()
{
client = new StudentServiceClient();

~StudentMng()
{
client.Close();
}

public void Add(Student entity)
{
client.Add(entity);
}

}

 

protected void btnException_Click(object sender, EventArgs e)
{
Student s1 = new Student();
s1.Name = txtName1.Text.Trim();
s1.Remark = "有异常添加";

Student s2 = new Student();
s2.Name = txtName2.Text.Trim();
s2.Remark = "有异常添加";

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
Transaction tran = Transaction.Current;
(new StudentMng()).Add(s1);

//throw new Exception("异常啦");
(new StudentMng()).Add(s2);

scope.Complete();
}
catch (Exception)
{
Transaction.Current.Rollback();
//throw;
}
}

Bind();


}

 

<netTcpBinding>
<binding name="NetTcpBinding_IStudentService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="true" transferMode="Buffered" transactionProtocol="OleTransactions"

iDream的主页 iDream | 初学一级 | 园豆:121
提问于:2012-04-11 12:41
< >
分享
最佳答案
0
收获园豆:40
dudu | 高人七级 |园豆:29817 | 2012-04-11 18:35

对了, 正常情况下只要不调用 scope.Complete();就会回滚。

clcvc | 园豆:62 (初学一级) | 2013-07-01 15:00
其他回答(1)
0

我之前遇到类似的毛病,我的强类型DataSet里的表超过37个,分布式事务就会没有任何征兆的失灵,我个人感觉是客户端事务不能传播到服务。你把表现弄少点试试,这究竟是为什么我也不清楚。

clcvc | 园豆:62 (初学一级) | 2013-07-01 14:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册