예제 #1
0
 /// <summary>
 /// 校验被代理的对象的类型
 /// </summary>
 /// <param name="service"></param>
 protected virtual void Check(ServiceAbstract service)
 {
     if (service == null)
     {
         throw new ServiceException("服务增强类 AdviceAbstractGeneric 只能用于 MyBatisServiceAbstract类型的子类型 ");
     }
 }
예제 #2
0
 /// <summary>
 /// 执行Lock加锁调用
 /// </summary>
 /// <param name="target"></param>
 /// <param name="callMessage"></param>
 /// <returns></returns>
 protected virtual IMessage LockInvoke(ServiceAbstract target, IMethodCallMessage callMessage)
 {
     lock (target.GetLockObject())
     {
         return(CatchAdviceInvoke(target, callMessage));
     }
 }
예제 #3
0
        public sealed override IMessage Invoke(MarshalByRefObject target, IMethodCallMessage callMessage)
        {
            ServiceAbstract service = target as ServiceAbstract;

            // 服务类型校验 其抛出的异常不会被捕获
            Check(service);

            return(LockInvoke(service, callMessage));
        }
예제 #4
0
        /// <summary>
        /// 执行Try...Catch增强调用
        /// </summary>
        /// <param name="target"></param>
        /// <param name="callMessage"></param>
        /// <returns></returns>
        protected virtual IMessage CatchAdviceInvoke(ServiceAbstract target, IMethodCallMessage callMessage)
        {
            try
            {
                BeforeInvokeBeProxy(target);

                IMessage message = DelayProxyUtil.InvokeBeProxy(target, callMessage);

                AfterInvokeBeProxy(target);

                return(message);
            }
            // 调用方法时,内部抛出的异常
            catch (TargetInvocationException targetEx)
            {
                string msg = string.Empty;

                if (!(targetEx.InnerException is ServiceException))
                {
                    if (targetEx.InnerException is DbException)
                    {
                        msg = "数据异常:";
                    }
                    else if (targetEx.InnerException is T)
                    {
                        msg = "服务异常:";
                    }
                    else
                    {
                        msg = "系统异常:";
                    }
                }

                return(ReturnError(msg + targetEx.InnerException.Message, targetEx.InnerException, target, callMessage));
            }
            catch (ServiceException sEx)
            {
                return(ReturnError(sEx.Message, sEx, target, callMessage));
            }
            catch (DbException dbEx)
            {
                return(ReturnError("数据异常:" + dbEx.Message, dbEx, target, callMessage));
            }
            catch (T tEx)
            {
                return(ReturnError("服务异常:" + tEx.Message, tEx, target, callMessage));
            }
            catch (Exception ex)
            {
                return(ReturnError("系统异常:" + ex.Message, ex, target, callMessage));
            }
        }
예제 #5
0
        /// <summary>
        /// 调用被代理对象方法前执行
        /// </summary>
        /// <param name="target"></param>
        protected virtual void BeforeInvokeBeProxy(ServiceAbstract target)
        {
            target.ResetError();

            this.CurrentKeepConnection = false;
            this.CurrentUseTransaction = false;

            if (!this.KeepConnection && !this.UseTransaction)
            {
                return;
            }

            // 已经开启了事务
            if (this.HasBeginTransaction())
            {
                // 不需要在当前方法的增强中进行任何处理
                return;
            }

            // 已经打开了连接
            if (this.HasOpenConnection())
            {
                if (this.UseTransaction)
                {
                    this.BeginTransaction(true);
                    this.CurrentUseTransaction = true;
                    return;
                }

                return;
            }


            // 即没有开启事务,又没有打开连接
            if (this.UseTransaction)
            {
                this.BeginTransaction(false);
                this.CurrentKeepConnection = true;
                this.CurrentUseTransaction = true;
            }
            else if (this.KeepConnection)
            {
                this.OpenConnection();
                this.CurrentKeepConnection = true;
            }
        }
예제 #6
0
 /// <summary>
 /// 调用被代理对象方法后执行
 /// </summary>
 /// <param name="target"></param>
 protected virtual void AfterInvokeBeProxy(ServiceAbstract target)
 {
     // 当前增强 只打开了连接
     if (this.CurrentKeepConnection && !this.CurrentUseTransaction)
     {
         this.CloseConnection();
     }
     // 当前增强 只开启了事务
     else if (!this.CurrentKeepConnection && this.CurrentUseTransaction)
     {
         this.CommitTransaction(true);
     }
     // 当前增强 既打开了连接,又开启了事务
     else if (this.CurrentKeepConnection && this.CurrentUseTransaction)
     {
         this.CommitTransaction(false);
     }
 }
예제 #7
0
        /// <summary>
        /// 返回错误信息
        /// <para>拦截所有异常,将错误信息存储到 ExtensionServiceAbstract 对象中,并返回被调用方法的默认值</para>
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ex"></param>
        /// <param name="target"></param>
        /// <param name="callMessage"></param>
        /// <returns></returns>
        protected virtual IMessage ReturnError(string msg, Exception ex,
                                               ServiceAbstract target, IMethodCallMessage callMessage)
        {
            try
            {
                // 当前增强 只打开了连接
                if (this.CurrentKeepConnection && !this.CurrentUseTransaction)
                {
                    this.CloseConnection();
                }
                // 当前增强 只开启了事务
                else if (!this.CurrentKeepConnection && this.CurrentUseTransaction)
                {
                    this.RollBackTransaction(true);
                }
                // 当前增强 既打开了连接,又开启了事务
                else if (this.CurrentKeepConnection && this.CurrentUseTransaction)
                {
                    this.RollBackTransaction(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            // 如果 逻辑上下文中已经进行了Try...Catch调用,
            // 则   将捕获的异常向上层抛出
            //if (this.HasTryCatch)
            //{
            //    return DelayProxyUtil.ReturnExecption(ex, callMessage);
            //}

            target.SetError(msg, ex);

            // 记录日志
            WriteLog(ex);

            return(DelayProxyUtil.ReturnDefaultValue(target, callMessage));
        }