Use transaction processing in WCF

Source: Internet
Author: User

WCF implements the standard wsatomictranscation (WS-AT) protocol and Microsoft proprietary oletx protocol in predefined bindings, which can be used to add transaction state information to messages. We can specify to put the code of an operation in the transaction scope for execution.
We need to specify in the binding configuration section,Transcationflow = true:

<bindings>  <wsHttpBinding>    <binding name="TranscationBindingConfig" transactionFlow="true">      <security mode="None" />    </binding>  </wsHttpBinding></bindings>

Note: any operation (operationcontract) that must be performed within a transaction range cannot be marked as one-way (isoneway = true) because at the end of the operation, the transaction status information must be returned to the caller. Developers can also point out that when no exception occurs, WCF should automatically commit transactions for the operation.

namespace WcfTranServiceLib{    [ServiceContract]    public interface IDAOService    {        [OperationContract(IsOneWay=false)]        [TransactionFlow(TransactionFlowOption.Mandatory)]        void InsAndUpdProduct(Products product);        [OperationContract]        [TransactionFlow(TransactionFlowOption.Mandatory)]        void InsAndUpdSupplier(Suppliers supplier);        [OperationContract]        List<Products> GetProducts(string productID);        [OperationContract]        List<Suppliers> GetSuppliers(string supplierID);          }}

 

If you need to submit the transaction yourself for the operations implemented by the service, you can use the static system. servicemodel. operationcontext object of WCF:

Public class myservicetype {[operationbehavior (transcationscoperequire = true, trancationautocomplete = false)] void mymethod () {// Other business logic operationcontext. Current. settranscationcomplete ();}}

Client developers can use the syntax provided by the system. transcation namespace to put service operations into a transaction range.

using(TranscationScope ts = new TranscationScope(TranscationScopeOption.RequireNew)){      client.DoSomething();      ...      ts.Complete();}client.Close();

 

If a service operation supports transactions and supports binding the transaction state, the client transaction will not be submitted until the service operation decides to commit (the complete of transcationscope is called ). On the contrary, any operations performed by the service on the transaction resource within the transaction scope of the client will be rolled back. (Any exceptions occurred during client processing)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Transactions;namespace WcfTranClient{    class Program    {        static void Main(string[] args)        {            try            {                var client = new DaoSvc.DAOServiceClient();                using (var ts = new TransactionScope())                {                    DaoSvc.Suppliers s = new DaoSvc.Suppliers();                    s.SupplierID = "S02";                    s.CompanyName = "CompanyName1";                    s.Address = "Address";                    s.Phone = "123456";                    client.InsAndUpdSupplier(s);                    DaoSvc.Products p = new DaoSvc.Products();                    p.ProductID = "P02";                    p.ProductName = "ProductName1";                    p.UnitPrice = 5;                    p.SupplierID = "S02";                    client.InsAndUpdProduct(p);                    //throw new Exception("Test Exception");                    ts.Complete();                }                var ps = client.GetProducts(null);                Console.WriteLine("Count of products: {0}", ps.Count);                var ss = client.GetSuppliers(null);                Console.WriteLine("Count of suppliers: {0}", ss.Count);                client.Close();            }            catch (Exception ex)            {                Console.WriteLine(ex.ToString());            }            finally            {                Console.Read();            }        }    }}

 

 

 

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.