WCF学习笔记(八)服务模式下的简单事务实例和非事务实例对比

来源:互联网 发布:java文件上传插件下载 编辑:程序博客网 时间:2024/05/18 00:53

1.契约

[ServiceContract]
    public interface IWCFServicesTransaction
    {
        /*服务模式下,服务必须启动一个根事务,且不参与外部事务 */
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.NotAllowed)]
        bool AddData(string name);
        [OperationContract]
        bool AddDataEx(string name);
    }

2.接口实现

注意:在TransactionScope 块下的,即是在事务模式下的添加数据库操作,需要没有异常才能把数据更新到数据库,在非事务模式直接更新进数据库

public class WCFServicesTransaction : IWCFServicesTransaction
    {
        #region IWCFServicesTransaction 成员

        /// <summary>
        /// 这就是一个根事务
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [OperationBehavior(TransactionScopeRequired=true)]
        public bool AddData(string productName)
        {
            //获取事务环境
            Transaction transaction = Transaction.Current;
            //DistributedIdentifier:获取升级的事务的唯一标识符。
            //若是等于00000-0000就是个本地事务
            Debug.Assert(transaction.TransactionInformation.DistributedIdentifier == Guid.Empty);
            //LocalIdentifier:本地事务标示
            //若是断言不等后面就不执行了,进程就阻塞在这个地方
            //Debug.Assert(transaction.TransactionInformation.LocalIdentifier == Guid.Empty.ToString());
            Console.WriteLine("本事务创建时间:{0}",transaction.TransactionInformation.CreationTime);
            Console.WriteLine("本事务状态:{0}", transaction.TransactionInformation.Status);
            Console.WriteLine("本事务本地事务标示:{0}", transaction.TransactionInformation.LocalIdentifier);
            //DistributedIdentifier:全局事务
            Console.WriteLine("本事务升级的事务的标示:{0}", transaction.TransactionInformation.DistributedIdentifier);
            //创建事务执行范围
            using(TransactionScope ts=new TransactionScope())
            {
                try
                {
                    DBHelper db = new DBHelper();
                    Products p = new Products();
                    p.ProductID = 1235;
                    p.ProductName = productName;
                    Console.WriteLine("调用事务,传入的数据是:{0},长度:{1}", productName, productName.Length);
                    db.AddProducts(p);
                    if (productName.Length > 5)
                    {
                        Exception ex = new Exception();
                        ExceptionDetail exdetail = new ExceptionDetail(ex);
                        throw new FaultException<ExceptionDetail>(exdetail, "名字太长");
                    }
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    Console.WriteLine("调用服务出错:{0}", e.Message);
                    return false;
                }
                ts.Complete();
                return true;
            }
        }

        #endregion

        #region IWCFServicesTransaction 成员


        public bool AddDataEx(string productName)
        {
            try
            {
                DBHelper db = new DBHelper();
                Products p = new Products();
                p.ProductID = 1234;
                p.ProductName = productName;
                Console.WriteLine("调用事务,传入的数据是:{0},长度:{1}", productName, productName.Length);
                db.AddProducts(p);
                if (productName.Length > 5)
                {
                    Exception ex = new Exception();
                    ExceptionDetail exdetail = new ExceptionDetail(ex);
                    throw new FaultException<ExceptionDetail>(exdetail, "名字太长");
                }
            }
            catch (Exception e)
            {
               
                Console.WriteLine("调用服务出错:{0}", e.Message);
                return false;
            }
    
            return true;
        }

        #endregion
    }

3.客户端调用(调用方式见WCF学习笔记(二))

static void Main(string[] args)
        {
            WCFServicesTransactionClient client = new WCFServicesTransactionClient();
            //try
            //{
            //    client.AddData("yoyoyo");
            //    Console.WriteLine("数据添加完成");
            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine("服务调用异常:{0}",ex.Message);
            //}

            try
            {
                client.AddDataEx("yoyoyo");
                Console.WriteLine("数据添加完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine("服务调用异常:{0}", ex.Message);
            }
            Console.ReadKey();
        }

 

原创粉丝点击