Spring. Net + WCF for distributed transactions
Project Background: ITOO. Teacher. Service provides a set of WCF services for accessing the instructor database. ITOO. Student. Service provides a set of WCF services for accessing the Student database. The student server needs to call the teacher's WCF Service for business processing at Layer B of the student server. We need to add distributed transaction processing at Layer B of the student server to update data to the teacher library and student library, either success or failure.
1. On the instructor's server, we need to add the WCF Distributed Transaction feature to the WCF interface:
[OperationContract][TransactionFlow(TransactionFlowOption.Allowed)]bool AddTeacher(TeacherViewModel vmTeacher);
Add the following features to the B-layer Implementation of the instructor Server:
////// Add students //////Instructor ViewModel ///
Boolean Value
[OperationBehavior (TransactionScopeRequired = true, TransactionAutoComplete = true)] public bool AddTeacher (TeacherViewModel vmTeacher) {// create a conversion rule Mapper. CreateMap
(); // Converts the entity TeacherEntity enStudent = Mapper. Map
(VmTeacher); // call the dal layer to Add the method this. CurrentDal. Add (enStudent); // submit the transaction and return the result return this. DbSession. SaveChanges ()> 0 ;}
2. Use system. transactions for Distributed Transaction processing on the B layer of the student Server:
Public void AddStudentTeacher (StudentViewModel vmStudent) {// create conversion rule Mapper. CreateMap
(); // Convert the entity StudentEntity enStudent = Mapper. Map
(VmStudent); // call the dal layer to add the method this. currentDal. add (enStudent); this. dbSession. saveChanges (); // call the instructor's add method ITeacherContracts teacherContracts = ServiceFactory. getTeacherService (); TeacherViewModel vmTeacher = new TeacherViewModel {TeacherID = 123, TeacherName = 11}; teacherContracts. addTeacher (vmTeacher );}
3. Because every transaction needs using TransactionScope and trans. Complete, we use Spring. Net's AOP to manage these identical processes:
public class AroundAdvice : IMethodInterceptor { public object Invoke(IMethodInvocation invocation) { object result; using (TransactionScope trans = new TransactionScope()) { result = invocation.Proceed(); trans.Complete(); } return result; } }
4. Configure the MSDTC Service of the database server:
4.1 enable MSDTC and input net start msdtc in DOS;
4.2set msdtc, input dcomcnfg.exe in dos, and set as follows:
4.3 Disable windows Firewall;
4.4 enable distributed transaction support for the database, as shown in the following figure:
The current version uses system. distributed transactions implemented by transactions, using Spring. net AOP, as for Spring. whether the declarative transactions of Net can manage the distributed transactions of WCF remains to be studied.