标签:aop change add wcf ext water summary dtc term
1.在教师服务端,我们需要在WCF的接口上加上WCF分布式事务特性:[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
bool AddTeacher(TeacherViewModel vmTeacher);
? ? ??在教师服务端的B层实现上加上WCF分布式事务特性:
/// <summary>
/// 增加学生
/// </summary>
/// <param name="vmTeacher">教师ViewModel</param>
/// <returns>布尔值</returns>
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public bool AddTeacher(TeacherViewModel vmTeacher)
{
//创建转换规则
Mapper.CreateMap<TeacherViewModel, TeacherEntity>();
//转换实体
TeacherEntity enStudent = Mapper.Map<TeacherEntity>(vmTeacher);
//调用dal层增加方法
this.CurrentDal.Add(enStudent);
//提交事务,返回结果
return this.DbSession.SaveChanges() > 0;
}
?? 2.在学生服务端的B层采用system.transactions进行分布式事务处理:
```
public void AddStudentTeacher(StudentViewModel vmStudent)
{
//创建转换规则
Mapper.CreateMap<StudentViewModel, StudentEntity>();
//转换实体
StudentEntity enStudent = Mapper.Map<StudentEntity>(vmStudent);
//调用dal层增加方法
this.CurrentDal.Add(enStudent);
this.DbSession.SaveChanges();
//调用老师的添加方法
ITeacherContracts teacherContracts = ServiceFactory.GetTeacherService();
TeacherViewModel vmTeacher = new TeacherViewModel { TeacherID = "123", TeacherName = "11" };
teacherContracts.AddTeacher(vmTeacher);
}
? ? ? ?3.由于每个事务处理都需要using TransactionScope、?trans.Complete,所以我们用Spring.Net的AOP管理这些相同的处理:
public class AroundAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
object result;
using (TransactionScope trans = new TransactionScope())
{
result = invocation.Proceed();
trans.Complete();
}
return result;
}
}
? ?4.配置数据库服务器的MSDTC服务:
? ? ?4.1开启MSDTC服务,在DOS中输入net start msdtc;
? ? ?4.2设置MSDTC,在DOS中输入dcomcnfg.exe,按下图所示设置:
![](http://i2.51cto.com/images/blog/201810/28/9ab6fb5d815622f1cad479fe7af7375f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
4.3关闭windows防火墙;
4.4开启数据库的分布式事务支持,设置如下图所示:
5.![](http://i2.51cto.com/images/blog/201810/28/a1fc537db968195eef6d55adc98c5400.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
6. 当前版本是用system.transactions实现的分布式事务,用Spring.Net的AOP切了一下
标签:aop change add wcf ext water summary dtc term
原文地址:http://blog.51cto.com/13981400/2309975