Example #1
0
        /// <summary>
        /// 调用被代理对象
        /// <para>支持 out ref 参数</para>
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage msg)
        {
            if (this.delay && this.target == null)
            {
                lock (objLock)
                {
                    if (this.delay && this.target == null)
                    {
                        T instance = Activator.CreateInstance(typeof(T)) as T;

                        // 自动装配属性
                        // 为属性对象启用代理,并延迟初始化被代理的对象
                        // DelayProxyUtil.AutowiredProperties(instance);

                        this.target = instance;
                    }
                }
            }

            IMethodCallMessage callMessage = (IMethodCallMessage)msg;

            AdviceAttribute attri = ReflectionUtil.GetCustomAttribute <AdviceAttribute>(callMessage.MethodBase);

            if (attri != null && attri.Advice != null)
            {
                return(attri.Advice.Invoke(this.target, callMessage));
            }

            return(DelayProxyUtil.InvokeBeProxy(this.target, callMessage));
        }
        /// <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));
            }
        }