Project background: ITOO.Teacher.Service provides a set of WCF services to access the teacher database, ITOO.Student.Service provides a set of WCF services to access the student database. The B-tier of the student service needs to invoke the teacher's WCF service when doing business processing, we need to add distributed transaction processing to the B-tier of the student service, update the data to the teacher Library and student library, either succeed or not succeed.
1. on the teacher service side, we need to add WCF distributed transaction features on the WCF interface:
[OperationContract] [TransactionFlow (transactionflowoption.allowed)]bool addteacher (Teacherviewmodel vmTeacher);
add WCF Distributed transaction features to the B-tier implementation on the teacher service side:
<summary>//Add students///</summary>// <param name= "Vmteacher" > Teacher viewmodel</ Param> //<returns> boolean </returns> [OperationBehavior (transactionscoperequired = True, TransactionAutoComplete = true)] public bool Addteacher (Teacherviewmodel vmteacher) { //create translation Rule Mapper.createmap<teacherviewmodel, teacherentity> (); Convert entity teacherentity enstudent = mapper.map<teacherentity> (vmteacher); Call the DAL layer to increase the method this . Currentdal.add (enstudent); Commits the transaction and returns the result return this . Dbsession.savechanges () > 0; }
2. Use System.Transactions for distributed transactions on the B-tier of student services:
public void Addstudentteacher (Studentviewmodel vmstudent) { //Create conversion rule mapper.createmap< Studentviewmodel, studententity> (); Convert entity studententity enstudent = mapper.map<studententity> (vmstudent); Call the DAL layer to increase the method this . Currentdal.add (enstudent); This. Dbsession.savechanges (); Call the teacher's Add method iteachercontracts teachercontracts = Servicefactory.getteacherservice (); Teacherviewmodel Vmteacher = new Teacherviewmodel {Teacherid = "123", TeacherName = "One"}; Teachercontracts.addteacher (Vmteacher); }
3. Because each transaction requires using TransactionScope, Trans.complete, we manage these same processes with 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. Configure the MSDTC service for the database server:
4.1 Open the MSDTC service and enter the net start MSDTC in DOS;
4.2 Set MSDTC, enter Dcomcnfg.exe in DOS , set as shown:
4.3 Shut down Windows Firewall;
4.4 Open the Distributed transaction support for the database, as shown in the following settings:
The current version is a distributed transaction implemented with System.Transactions, cut with Spring.net AOP, and whether Spring.net's declarative transactions can be managed for WCF distributed transactions remains to be studied.
Frame Source: Http://pan.baidu.com/s/1hqvOC6K (preferably open with VS2012).
SPRING.NET+WCF Implementing distributed Transactions