コード例 #1
0
ファイル: DelayProxy.cs プロジェクト: vkonkin/BlogsDemo
        /// <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));
        }
コード例 #2
0
ファイル: DelayProxyUtil.cs プロジェクト: vkonkin/BlogsDemo
        /// <summary>
        /// 自动装配属性
        /// <para>为属性对象启用代理,并延迟初始化被代理的对象</para>
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static void AutowiredProperties(object obj)
        {
            if (obj == null)
            {
                return;
            }

            // 获取公共实例属性
            PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

            if (infos != null && infos.Length > 0)
            {
                foreach (PropertyInfo pInfo in infos)
                {
                    AutowiredAttribute autoProxy = ReflectionUtil.GetCustomAttribute <AutowiredAttribute>(pInfo);

                    if (autoProxy == null)
                    {
                        continue;
                    }

                    object pValue = DelayProxyUtil.CreateProxy(pInfo.PropertyType, autoProxy);

                    pInfo.SetValue(obj, pValue, null);
                }
            }
        }
コード例 #3
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));
            }
        }
コード例 #4
0
ファイル: DelayProxyUtil.cs プロジェクト: vkonkin/BlogsDemo
        /// <summary>
        /// 为指定类型 创建代理
        /// </summary>
        /// <returns></returns>
        public static object CreateProxy(Type type, AutowiredAttribute autoProxy)
        {
            // 为属性对象启用代理,并延迟初始化被代理的对象
            if (autoProxy.UseProxy)
            {
                return(DelayProxyUtil.GetTransparentProxy(type, null, true));
            }

            // 不启用代理,并不延迟初始化
            object instance = Activator.CreateInstance(type);

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

            return(instance);
        }
コード例 #5
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));
        }