CapSupport 的使用

CapSupport 是在CAP的操作上做了一些封装

目的是让事务同时执行或者同时回滚

startup

  services.AddCapSupport((optaion) =>
            {
                optaion.SuccessTransactionfulName = "CapTest.ReceiveSuccessful";
                optaion.RollbackTransactionfulName = "CapTest.ReceiveRollback";
                optaion.CommitTransaction = (tran) =>
                {
                    Console.WriteLine("事务执行成功");
                    ((MsSqlContext)tran).Session.CommitTransaction();
                };
                optaion.RollbackTransaction = (tran) =>
                {
                    Console.WriteLine("事务执行失败");
                    ((MsSqlContext)tran).Session.RollbackTransaction();
                };
            });

 

Controller01

 MsSqlContext dbContext = new MsSqlContext("");
            dbContext.Session.BeginTransaction();
            try
            {
                var count = dbContext.Insert<AAA>(new AAA() { ID = 1, Name = "AAA" });
                var action = _publisher.AsyncPerform(1, dbContext,
                  new Person { Name = "Foo", Age = 11 }, "CapTest05.Subscribe");
                var result = await action;
                Console.WriteLine(result.TransactionResult);

              
            }
            catch (Exception e)
            {
                _publisher.RollbackMandatory(1);
            }

 

Controller02

 [CapSubscribe("CapTest05.Subscribe")]
        public async Task Subscribe(TransactionContent person)
        {
           
            Console.WriteLine("触发");
            MsSqlContext dbContext = new MsSqlContext("");
            dbContext.Session.BeginTransaction();
            try
            {
                var count = dbContext.Insert<CCC>(new CCC() { ID = 1, Name = "CCC" });
                var task = _publisher.AsyncPerform(3, dbContext, new Person { Name = "Foo", Age = 11 }, "CapTest04.Subscribe");
                var result = await task;
                Console.WriteLine(count);
                switch (result.TransactionResult)
                {
                    case SubscriptionServiceEnum.Successful:
                        _publisher.TransactionSuccessful(person);
                        break;
                    case SubscriptionServiceEnum.Failure:
                        _publisher.TransactionRollback(person);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                _publisher.RollbackMandatory(3);
                _publisher.TransactionRollback(person);
            }
        
         
         
           

        }

Controller03

 

[CapSubscribe("CapTest04.Subscribe")]
        public void Subscribe(TransactionContent person)
        {
            MsSqlContext dbContext = new MsSqlContext("");
            dbContext.Session.BeginTransaction();
            try
            {
                var count = dbContext.Insert<BBB>(new BBB() { ID = 1, Name = "BBB" });
                Console.WriteLine("触发");
                //throw new Exception("测试异常");
                _publisher.TransactionSuccessful(person, dbContext);
            }
            catch (Exception e)
            {
                _publisher.TransactionRollback(person, dbContext);
            }
           
        }

 

他可以在执行第一个事务中执行另一个事务,并且在另一个事务执行完毕后决定第一个事务是否成功

可以等待一个订阅完成

 

NUGET

GITHUB

posted @ 2018-11-25 21:57  AnAng  阅读(377)  评论(0编辑  收藏  举报