SPRING.NET+WCF Implementing distributed Transactions

Source: Internet
Author: User

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

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.