public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage                  call = (IMethodCallMessage)msg;
            IConstructionCallMessage            ctor = call as IConstructionCallMessage;
            List <Action <IMethodCallMessage> > OnMethodExecuting = new List <Action <IMethodCallMessage> >();
            List <Action <IMethodCallMessage, IMethodReturnMessage> > OnMethodExecuted = new List <Action <IMethodCallMessage, IMethodReturnMessage> >();

            IMethodReturnMessage returnMessage = null;

            if (ctor != null)
            {
                //构造函数
                //获取最底层的默认真实代理
                RealProxy default_proxy = RemotingServices.GetRealProxy(this.target);
                default_proxy.InitializeServerObject(ctor);
                MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy(); //自定义的透明代理 this
                returnMessage = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp);
            }
            else
            {
                //方法
                object returnObject = null;
                if (!Intercept(call, ref returnObject, ref OnMethodExecuting, ref OnMethodExecuted))
                {
                    if (returnObject is IMethodReturnMessage)
                    {
                        returnMessage = (IMethodReturnMessage)returnObject;
                    }
                    else
                    {
                        if (returnObject == null)
                        {
                            var method     = (MethodInfo)call.MethodBase;
                            var returnType = method.ReturnType;
                            if (returnType != typeof(void))
                            {
                                returnObject = returnType.IsValueType ? Activator.CreateInstance(returnType) : null;
                            }
                        }
                        returnMessage = new ReturnMessage(returnObject, call.Args, call.ArgCount, call.LogicalCallContext, call);
                    }
                }
                else
                {
                    OnMethodExecuting.ForEach((method) => { method(call); });
                    OnMethodExecuting.Clear();
                    try
                    {
                        returnMessage = RemotingServices.ExecuteMessage(this.target, call);
                    }
                    catch (Exception e)
                    {
                        returnMessage = new ReturnMessage(e, call);
                    }
                    OnMethodExecuted.ForEach((method) => { method(call, returnMessage); });
                    OnMethodExecuted.Clear();
                }
            }
            return(returnMessage);
        }
示例#2
0
        /// <summary>
        /// 實現Invoke方法
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage msg)
        {
            IMethodReturnMessage methodReturnMessage = null;
            IMethodCallMessage   methodCallMessage   = msg as IMethodCallMessage;

            if (methodCallMessage != null)
            {
                IConstructionCallMessage constructionCallMessage = methodCallMessage as IConstructionCallMessage;
                if (constructionCallMessage != null)
                {
                    RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);
                    defaultProxy.InitializeServerObject(constructionCallMessage);
                    methodReturnMessage = EnterpriseServicesHelper.CreateConstructionReturnMessage(constructionCallMessage, (MarshalByRefObject)GetTransparentProxy());
                }
                else
                {
                    _interception.PreInvoke(methodCallMessage);
                    try
                    {
                        methodReturnMessage = RemotingServices.ExecuteMessage(_target, methodCallMessage);
                    }
                    catch { }
                    if (methodReturnMessage.Exception != null)
                    {
                        _interception.ExceptionHandle(methodReturnMessage);
                    }
                    else
                    {
                        _interception.PostInvoke(methodReturnMessage);
                    }
                }
            }
            return(methodReturnMessage);
        }
示例#3
0
        //覆写Invoke,处理RealProxy截获的各种消息,
        //此种方式最简捷,但不能截获远程对象的激活,好在我们并不是真的要Remoting
        public override IMessage Invoke(IMessage msg)
        {
            var call = msg as IMethodCallMessage;
            IConstructionCallMessage ctr  = call as IConstructionCallMessage;
            IMethodReturnMessage     back = null;

            //构造函数,只有ContextBoundObject(Inherit from MarshalByRefObject)对象才能截获构造函数
            if (ctr != null)
            {
                RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);
                //如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
                //这样的话,会导致没有直正构建对象。
                defaultProxy.InitializeServerObject(ctr);
                //本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
                back = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctr, (MarshalByRefObject)GetTransparentProxy());
            }
            //MarshalByRefObject对象就可截获普通的调用消息,
            //MarshalByRefObject对象告诉编译器,不能将其内部简单的成员函数优化成内联代码,
            //这样才能保证函数调用都能截获。
            else
            {
                IDictionary <string, object> dic = new Dictionary <string, object>();
                dic = call.Args[0] as IDictionary <string, object>;
                if (dic != null & dic.Count > 0)
                {
                    foreach (var m in dic)
                    {
                    }
                }
                back = RemotingServices.ExecuteMessage(_target, call);
            }
            return(back);
        }
示例#4
0
        public override IMessage Invoke(IMessage msg)
        {
            if (msg != null)
            {
                if (msg is IConstructionCallMessage)
                {
                    IConstructionCallMessage constructCallMsg = msg as IConstructionCallMessage;

                    RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);

                    // 如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
                    // 这样的话,会导致没有直正构建对象。
                    defaultProxy.InitializeServerObject(constructCallMsg);

                    // 本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
                    return(System.Runtime.Remoting.Services.EnterpriseServicesHelper.CreateConstructionReturnMessage(constructCallMsg, (MarshalByRefObject)GetTransparentProxy()));
                }
                else if (msg is IMethodCallMessage)
                {
                    IMethodCallMessage callMsg = msg as IMethodCallMessage;
                    object[]           args    = callMsg.Args;

                    // System.Windows.Forms.MessageBox.Show(callMsg.MethodBase.ToString());

                    if (callMsg.MethodName.StartsWith(" set_ ") && args.Length == 1)
                    {
                        method.Invoke(_target, new object[] { callMsg.MethodName.Substring(4), args[0] }); // 对属性进行调用
                                            
                    }
                    return(RemotingServices.ExecuteMessage(_target, callMsg));
                }
            }
            return(msg);
        }
示例#5
0
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage       call   = msg as IMethodCallMessage;
            IConstructionCallMessage ctor   = msg as IConstructionCallMessage;
            IMethodReturnMessage     result = null;

            if (ctor != null)               // 呼び出し関数がコンストラクタの場合

            {
                _behavior.PreInitializer(ctor);

                RealProxy rp = RemotingServices.GetRealProxy(_target);
                rp.InitializeServerObject(ctor);
                MarshalByRefObject tp = this.GetTransparentProxy() as MarshalByRefObject;
                result = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp);

                _behavior.PostInitializer(ctor);
            }
            else if (call != null)                 // 通常関数の場合

            {
                _behavior.PreCallMethod(call);

                result = RemotingServices.ExecuteMessage(this._target, call);

                _behavior.PostCallMethod(call);
            }
            else
            {
                Console.WriteLine($"メソッドの実行に失敗しました:{msg}");
                //throw new Exception($"メソッドの実行に失敗しました:{msg}");
            }

            return(result);
        }
示例#6
0
        public override IMessage Invoke(IMessage msg)
        {
            var ctr = msg as IConstructionCallMessage;

            if (ctr != null)
            {
                Console.WriteLine("ctr");
                RealProxy _proxy = RemotingServices.GetRealProxy(this._target);
                _proxy.InitializeServerObject(ctr);
                MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy();
                return(EnterpriseServicesHelper.CreateConstructionReturnMessage(ctr, tp));
            }

            if (_intercepts != null)
            {
                foreach (var _intercept in _intercepts)
                {
                    _intercept.Do();
                }
            }
            var call = msg as IMethodCallMessage;

            Console.WriteLine(string.Format("proxy method:{0}", call.MethodName));
            var result = call.MethodBase.Invoke(this._target, call.Args);

            return(new ReturnMessage(result, new object[0], 0, null, call));
        }
示例#7
0
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage   callMsg = msg as IMethodCallMessage;
            IMethodReturnMessage retMsg  = null;
            bool fLog = false;

            //메서드 시작 로그
            fLog = MethodEnterLog(callMsg);

            // 생성자 메서드 일경우
            if (msg is IConstructionCallMessage)
            {
                // 객체 생성 메시지 처리
                IConstructionCallMessage ctorMsg = (IConstructionCallMessage)msg;
                RealProxy proxy = RemotingServices.GetRealProxy(target);
                // 실제 객체를 생성한다.
                proxy.InitializeServerObject(ctorMsg);
                // 객체 생성 결과를 ‘만들어’ 반환한다.
                retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctorMsg, (MarshalByRefObject)this.GetTransparentProxy());
            }
            //일반 메서드
            else if (msg is IMethodCallMessage)
            {
                /*
                 * // 파라메터 있는 메서드와 아닌 메서드 나눌때
                 * if (callMsg.ArgCount > 0)
                 * {
                 *  // 파라메터 리스트 작성
                 *  Console.WriteLine("Parameter list :");
                 *
                 *  for (int i = 0; i < callMsg.ArgCount; i++)
                 *  {
                 *      Console.WriteLine("{0} | {1}: {2}", i, callMsg.GetArgName(i), callMsg.GetArg(i));
                 *  }
                 *  retMsg = RemotingServices.ExecuteMessage(target, callMsg);
                 * }
                 * else
                 * {
                 *  //메소드 실행 코드
                 *  retMsg = RemotingServices.ExecuteMessage(target, callMsg);
                 * }
                 */
                retMsg = RemotingServices.ExecuteMessage(target, callMsg);
            }

            if (retMsg.Exception != null)
            {
                Console.WriteLine(retMsg.MethodName + " 에서 " + retMsg.Exception.Message);
                //Environment.Exit(0);
            }

            // 메소드 종료 로그
            if (fLog)
            {
                MethodLeaveLog(retMsg);
            }

            return(retMsg);
        }
示例#8
0
        public override IMessage Invoke(IMessage msg)
        {
            //throw new NotImplementedException();

            IMethodCallMessage       call = (IMethodCallMessage)msg;
            IConstructionCallMessage ctor = call as IConstructionCallMessage;
            IMethodReturnMessage     back = null;

            //filter method
            object[] attrs       = call.MethodBase.GetCustomAttributes(typeof(BusinessMethodAttribute), true);
            bool     isAopMethod = attrs.Length > 0;

            if (ctor != null)
            {
                //构造开始时
                //do something
                if (this.EventConstructionBegin != null)
                {
                    this.EventConstructionBegin(msg, back, this._target, null);
                }

                //执行远程对象构造方法
                MarshalByRefObject mbro      = this.GetTransparentProxy() as MarshalByRefObject;
                RealProxy          realProxy = RemotingServices.GetRealProxy(this._target);
                realProxy.InitializeServerObject(ctor);
                back = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, mbro);

                //构造结束时
                //do something
                if (this.EventConstructionEnd != null)
                {
                    this.EventConstructionEnd(msg, back, this._target, null);
                }

                return(back);
            }


            //方法执行开始时
            //do something
            if (this.EventMethodBegin != null && isAopMethod)
            {
                this.EventMethodBegin(msg, back, this._target, attrs[0] as BusinessMethodAttribute);
            }

            //执行远程对象方法
            back = RemotingServices.ExecuteMessage(this._target, call);

            //方法执行结束时
            //do something
            if (this.EventMethodEnd != null && isAopMethod)
            {
                this.EventMethodEnd(msg, back, this._target, attrs[0] as BusinessMethodAttribute);
            }
            return(back);
        }
示例#9
0
        public override IMessage Invoke(IMessage msg)
        {
            if (msg != null)
            {
                if (msg is IConstructionCallMessage)
                {
                    IConstructionCallMessage constructCallMsg = msg as IConstructionCallMessage;
                    //IConstructionReturnMessage constructionReturnMessage = this.InitializeServerObject((IConstructionCallMessage)msg);
                    //RealProxy.SetStubData(this, constructionReturnMessage.ReturnValue);
                    //return constructionReturnMessage;

                    RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);

                    //如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
                    //这样的话,会导致没有直正构建对象。
                    defaultProxy.InitializeServerObject(constructCallMsg);

                    //本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
                    return(EnterpriseServicesHelper.CreateConstructionReturnMessage(constructCallMsg, (MarshalByRefObject)GetTransparentProxy()));
                }
                else if (msg is IMethodCallMessage)
                {
                    IMethodCallMessage callMsg = msg as IMethodCallMessage;
                    object[]           args    = callMsg.Args;

                    //System.Windows.Forms.MessageBox.Show(callMsg.MethodBase.ToString());

                    if (callMsg.MethodName.StartsWith("set_") && args.Length == 1)
                    {
                        method.Invoke(_target, new object[] { callMsg.MethodName.Substring(4), args[0] });//对属性进行调用
                    }
                    return(RemotingServices.ExecuteMessage(_target, callMsg));

                    //IMessage message = null;
                    //try
                    //{

                    //    MarshalByRefObject obj = GetUnwrappedServer();
                    //    if (callMsg.MethodName.StartsWith("set_") && args.Length == 1)
                    //    {
                    //        method.Invoke(obj, new object[] { callMsg.MethodName.Substring(4), args[0] });//对属性进行调用
                    //    }

                    //    object o = callMsg.MethodBase.Invoke(obj, args);
                    //    message = new ReturnMessage(o, args, args.Length, callMsg.LogicalCallContext, callMsg);
                    //}
                    //catch (Exception e)
                    //{
                    //    message = new ReturnMessage(e, callMsg);
                    //}
                    //return message;
                }
            }
            return(msg);
        }
示例#10
0
        /// <summary>
        /// Вызов конструктора и инициализация
        /// </summary>
        public void Initialize()
        {
            // валидация
            if (validationOnInit != null)
            {
                validationOnInit(callMessage as IConstructionCallMessage);
            }

            //  Вызов InitializeServerObject создаст
            //  экземпляр "серверного" объекта
            //  и свяжет TransparentProxy с текущим контекстом
            ReturnMessage = proxy.InitializeServerObject(
                (IConstructionCallMessage)callMessage);
        }
示例#11
0
        public override IMessage Invoke(IMessage msg)
        {
            bool useAspect          = false;
            IMethodCallMessage call = (IMethodCallMessage)msg;

            //查询目标方法是否使用了启用AOP的AopSwitcherAttribute
            //默认为使用Aspect
            var attr = CoreHelper.GetMemberAttribute <AopSwitcherAttribute>(call.MethodBase);

            if (attr != null)
            {
                useAspect = attr.UseAspect;
            }
            else
            {
                useAspect = true;
            }

            if (useAspect)
            {
                this.PreProceed(call);
            }

            //如果触发的是构造函数,此时target的构建还未开始
            IConstructionCallMessage ctor = call as IConstructionCallMessage;

            if (ctor != null)
            {
                //获取最底层的默认真实代理
                RealProxy default_proxy = RemotingServices.GetRealProxy(this.target);

                default_proxy.InitializeServerObject(ctor);
                MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy(); //自定义的透明代理 this

                return(EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp));
            }

            IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target, call); //将消息转化为堆栈,并执行目标方法,方法完成后,再将堆栈转化为消息

            if (useAspect)
            {
                this.PostProceed(call, ref result_msg);
            }

            return(result_msg);
        }
示例#12
0
        public override IMessage Invoke(IMessage message)
        {
            IMethodCallMessage caller = (IMethodCallMessage)message;
            // Get proxy able
            bool proxyAble = _proxy;

            if (!proxyAble)
            {
                foreach (AProxyMethodAttribute attribute in caller.MethodBase.GetCustomAttributes(typeof(AProxyMethodAttribute), false))
                {
                    if (attribute.IsProxy)
                    {
                        proxyAble = true;
                        break;
                    }
                }
            }
            // Process before
            if (proxyAble)
            {
                ProcessBefore(caller);
            }
            // Process
            IMethodReturnMessage     response    = null;
            IConstructionCallMessage constructor = caller as IConstructionCallMessage;

            if (constructor != null)
            {
                // Constructor
                RealProxy defaultProxy = RemotingServices.GetRealProxy(_object);
                defaultProxy.InitializeServerObject(constructor);
                MarshalByRefObject transparentProxy = (MarshalByRefObject)this.GetTransparentProxy();
                response = EnterpriseServicesHelper.CreateConstructionReturnMessage(constructor, transparentProxy);
            }
            else
            {
                // Method
                response = RemotingServices.ExecuteMessage(_object, caller);
            }
            // Process after
            if (proxyAble)
            {
                ProcessAfter(caller, response);
            }
            return(response);
        }
示例#13
0
        public override IMessage Invoke(IMessage msg)
        {
            if (msg is IConstructionCallMessage)
            {
                var       constructCallMsg = msg as IConstructionCallMessage;
                RealProxy defaultProxy     = RemotingServices.GetRealProxy(_target);
                defaultProxy.InitializeServerObject(constructCallMsg);
                return(EnterpriseServicesHelper.CreateConstructionReturnMessage(constructCallMsg, (MarshalByRefObject)GetTransparentProxy()));
            }

            if (msg is IMethodCallMessage)
            {
                var      callMsg = msg as IMethodCallMessage;
                object[] args    = callMsg.Args;
                IMessage message;
                try
                {
                    if (callMsg.MethodName.StartsWith("set_") && args.Length == 1)
                    {
                        _currentType.InvokeMember("InvokeSetProperty", BindingFlags.InvokeMethod, null, _target, new[] { callMsg.MethodName.Replace("set_", ""), args[0] });
                    }
                    else if (callMsg.MethodName.StartsWith("get_") && args.Length == 0)
                    {
                        _currentType.InvokeMember("InvokeGetProperty", BindingFlags.InvokeMethod, null, _target, new object[] { callMsg.MethodName.Replace("get_", "") });
                    }
                    else
                    {
                        _currentType.InvokeMember("InvokeMethod", BindingFlags.InvokeMethod, null, _target, new object[] { callMsg.MethodName, args });
                    }

                    return(RemotingServices.ExecuteMessage(_target, callMsg));
                }
                catch (Exception e)
                {
                    message = new ReturnMessage(e, callMsg);
                }
                return(message);
            }

            return(msg);
        }
示例#14
0
        public virtual object GetInstance(IConstructionCallMessage ctorCallMessage, ref IMethodMessage returnMsg)
        {
            lock (_padLock)
            {
                if (!_ctorMsgs.ContainsKey(ctorCallMessage.ActivationType))
                {
                    RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);
                    var       x            = defaultProxy.InitializeServerObject(ctorCallMessage);
                    IConstructionReturnMessage response = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctorCallMessage, (MarshalByRefObject)this.GetTransparentProxy());

                    _ctorMsgs.SetValue(ctorCallMessage.ActivationType, response);
                    _instanceMsgs.SetValue(ctorCallMessage.ActivationType, DoObjectInitialize(_target));

                    //_ctorMsgs.SetValue(ctorCallMessage.ActivationType, InitializeServerObject(ctorCallMessage));
                    //_instanceMsgs.SetValue(ctorCallMessage.ActivationType, DoObjectInitialize(GetUnwrappedServer()));
                }

                returnMsg = _ctorMsgs.GetValue(ctorCallMessage.ActivationType);
                return(_instanceMsgs.GetValue(ctorCallMessage.ActivationType));
            }
        }
示例#15
0
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage       call      = (IMethodCallMessage)msg;
            IConstructionCallMessage ctor      = call as IConstructionCallMessage;
            IMethodReturnMessage     returnMsg = null;

            if (ctor != null)
            {
                //获取最底层的默认真实代理
                RealProxy defaultRealProxy = RemotingServices.GetRealProxy(target);

                defaultRealProxy.InitializeServerObject(ctor);
                MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy();

                returnMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp);
            }
            else
            {
                IMethodMessage method         = (IMethodMessage)msg;
                Type           aopHandlerType = null;
                foreach (Attribute attr in call.MethodBase.GetCustomAttributes(false))
                {
                    AOPMethodAttribute methodAopAttr = attr as AOPMethodAttribute;
                    if (methodAopAttr != null)
                    {
                        aopHandlerType = methodAopAttr.AOPHandlerType;
                        break;
                    }
                }

                if (aopHandlerType != null)
                {
                    IAOPHandler proxyHandler = (IAOPHandler)Activator.CreateInstance(aopHandlerType);
                    AOPArgs     args         = new AOPArgs();
                    args.Args       = method.Args;
                    args.CallMethod = method.MethodBase;
                    args.ExecuteID  = Guid.NewGuid();
                    if (!proxyHandler.OnEntry(args))
                    {
                        return(returnMsg);
                    }
                    returnMsg = RemotingServices.ExecuteMessage(target, call);
                    if (returnMsg.Exception != null)
                    {
                        args.Exception = returnMsg.Exception;
                        proxyHandler.OnException(args);
                    }
                    else
                    {
                        args.ReturnValue = returnMsg.ReturnValue;
                        proxyHandler.OnExit(args);
                    }
                }
                else
                {
                    returnMsg = RemotingServices.ExecuteMessage(target, call);
                }
            }

            return(returnMsg);
        }
示例#16
0
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage lMethodMsg = msg as IMethodCallMessage;
            IMessage           lMsgOut    = null;

            if (lMethodMsg is IConstructionCallMessage)
            {
                mTargetType = lMethodMsg.MethodBase.DeclaringType;
                mMethod     = lMethodMsg.MethodBase;
                mArgs       = lMethodMsg.Args;

                RealProxy lRealProxy = RemotingServices.GetRealProxy(mTarget);
                lRealProxy.InitializeServerObject((IConstructionCallMessage)lMethodMsg);

                MarshalByRefObject lTransparentProxy = (MarshalByRefObject)GetTransparentProxy();
                IMessage           lReturnMessage    = EnterpriseServicesHelper.CreateConstructionReturnMessage((IConstructionCallMessage)lMethodMsg, lTransparentProxy);

                return(lReturnMessage);
            }
            else
            {
                mMethod = lMethodMsg.MethodBase;
                mArgs   = lMethodMsg.Args;

                // Initialize variables AOP
                Initprocess(msg);

                try
                {
                    // Execute Preprocess AOP
                    Preprocess(msg);

                    try
                    {
                        if (string.Compare(mMethod.Name, "FieldGetter", true) == 0)
                        {
                            FieldInfo lFieldInfo = mTarget.GetType().GetField(lMethodMsg.Args[1] as string);
                            mReturnValue = lFieldInfo.GetValue(mTarget);
                        }
                        else
                        {
                            MethodInfo lMethodInfo = mTarget.GetType().GetMethod(mMethod.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                            if (lMethodInfo == null)
                            {
                                lMethodInfo = mTarget.GetType().GetMethod(mMethod.Name, BindingFlags.Instance | BindingFlags.Public);
                            }
                            mReturnValue = lMethodInfo.Invoke(mTarget, mArgs);
                        }
                        lMsgOut = new ReturnMessage(mReturnValue, mArgs, lMethodMsg.ArgCount, lMethodMsg.LogicalCallContext, lMethodMsg);
                    }
                    catch (Exception e)
                    {
                        throw e.InnerException;
                    }

                    // Execute Preprocess AOP
                    Postprocess(msg, ref lMsgOut);
                }
                catch (Exception e)
                {
                    // Execute Preprocess AOP
                    Exceptionprocess(msg, e);

                    throw;
                }

                return(lMsgOut);
            }
        }
示例#17
0
        public override IMessage Invoke(IMessage msg)
        {
            UseAopType         useAspect = 0;
            string             uselog    = "";
            IMethodCallMessage call      = (IMethodCallMessage)msg;

            //查询目标方法是否使用了启用AOP的MethodAopSwitcherAttribute
            foreach (Attribute attr in call.MethodBase.GetCustomAttributes(false))
            {
                MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute;
                if (mehodAopAttr != null)
                {
                    useAspect = mehodAopAttr.UseAspect;
                    uselog    = mehodAopAttr.Userlog;
                    break;
                    //if (mehodAopAttr.UseAspect==1)
                    //{
                    //    useAspect = 1;
                    //    break;
                    //}
                }
            }

            if (useAspect == UseAopType.before)
            {
                this.PreProcess(msg);   //执行方法之前的操作
            }

            //如果触发的是构造函数,此时target的构建还未开始
            IConstructionCallMessage ctor = call as IConstructionCallMessage;

            if (ctor != null)
            {
                //获取最底层的默认真实代理
                RealProxy default_proxy = RemotingServices.GetRealProxy(this.target);

                default_proxy.InitializeServerObject(ctor);
                MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy(); //自定义的透明代理 this

                return(EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp));
            }
            if (useAspect == UseAopType.before)
            {
                //#region 若不想运行目标方法可以执行该代码,如果直接return null会导致异常发生
                //IMethodCallMessage callMsg = msg as IMethodCallMessage;
                //return new ReturnMessage(callMsg.InArgs, null, 0, null, callMsg);
                //#endregion

                #region 调用目标方法代码
                IMethodMessage result_msg;
                result_msg = RemotingServices.ExecuteMessage(this.target, call);
                return(result_msg);

                #endregion
            }
            else
            {
                #region 调用目标方法代码
                IMethodMessage result_msg;
                result_msg = RemotingServices.ExecuteMessage(this.target, call);
                object result = null;
                if (result_msg is ReturnMessage)
                {
                    result = (result_msg as ReturnMessage).ReturnValue;
                }
                //IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target, call);
                #endregion
                if (useAspect == UseAopType.after)
                {
                    this.PostProcess(msg, result_msg, result, uselog);      //执行方法结束后的操作
                }
                return(result_msg);
            }
        }
示例#18
0
        /// <summary>
        /// 代理调用方法
        /// </summary>
        /// <param name="msg">方法信息</param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage call = (IMethodCallMessage)msg;

            #region 检索AOP特性标记
            AopMethodConfigAttribute methodAopAttr = null;
            var attrs = call.MethodBase.GetCustomAttributes(typeof(AopMethodConfigAttribute), false);
            if (attrs.Length > 0)
            {
                methodAopAttr = attrs[0] as AopMethodConfigAttribute;
            }
            #endregion

            #region 方法执行前拦截
            if (methodAopAttr != null)
            {
                var provider = GetOperatorProvider(methodAopAttr.InvokingProvider);
                if (provider != null)
                {
                    provider.Invoking(msg);
                }
                if (methodAopAttr.UseDefaultBeginAop)
                {
                    this.Invoking(msg);
                }
            }
            #endregion

            #region 执行方法
            IMethodReturnMessage returnMsg = null;
            //如果触发的是构造函数,此时target的构建还未开始
            IConstructionCallMessage ctor = call as IConstructionCallMessage;
            if (ctor != null)
            {
                //获取最底层的默认真实代理
                RealProxy default_proxy = RemotingServices.GetRealProxy(this.target);
                default_proxy.InitializeServerObject(ctor);
                MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy(); //自定义的透明代理 this
                return(EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp));
            }
            else
            {
                returnMsg = RemotingServices.ExecuteMessage(this.target, call);  //将消息转化为堆栈,并执行目标方法,方法完成后,再将堆栈转化为消息
            }
            #endregion

            #region 异常处理
            if (returnMsg.Exception != null)
            {
                IAopOperator provider = null;
                if (methodAopAttr != null)
                {
                    provider = GetOperatorProvider(methodAopAttr.OnExceptionProvider);
                }
                if (provider != null)
                {
                    provider.OnException(msg, returnMsg.Exception);
                }
                else if ((target as IException) != null)
                {
                    ((IException)target).OnException(msg, returnMsg.Exception);
                }
                else
                {
                    this.OnException(msg, returnMsg.Exception);
                }
                object value      = null;
                var    methodInfo = (System.Reflection.MethodInfo)returnMsg.MethodBase;
                if (methodInfo.ReturnType != typeof(void) && methodAopAttr != null && methodAopAttr.ReturnExceptionDefaultValue)
                {
                    value = Activator.CreateInstance(methodInfo.ReturnType, methodAopAttr.ExceptionDefaultValue);
                }
                returnMsg = new ReturnMessage(value, returnMsg.OutArgs, returnMsg.OutArgCount, returnMsg.LogicalCallContext, call);
            }
            #endregion

            #region 方法执行后拦截
            else if (methodAopAttr != null)
            {
                var provider = GetOperatorProvider(methodAopAttr.InvokedProvider);
                if (provider != null)
                {
                    provider.Invoked(msg, returnMsg);
                }
                if (methodAopAttr.UseDefaultEndAop)
                {
                    this.Invoked(msg, returnMsg);
                }
            }
            #endregion

            return(returnMsg);
        }