Spring. Net + WCF for distributed transactions

Source: Internet
Author: User

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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.