要使用分布式事务 写了个例子 配置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"
我之前遇到类似的毛病,我的强类型DataSet里的表超过37个,分布式事务就会没有任何征兆的失灵,我个人感觉是客户端事务不能传播到服务。你把表现弄少点试试,这究竟是为什么我也不清楚。