public override IMessage Invoke(IMessage msg)
            {
                IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
                MethodBase         method        = methodMessage.MethodBase;
                object             returnValue   = null;
                ReturnMessage      returnMessage = null;

                try
                {
                    MethodBase targetMethod = GetTargetMethodFromInterfaceMethod(method);
                    if (targetMethod == null)
                    {
                        throw new NotImplementedException(string.Format("Method {0} was not implemented by the target", method.Name));
                    }
                    returnValue   = targetMethod.Invoke(m_Target, methodMessage.Args);
                    returnMessage = new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage);
                }
                catch (Exception ex)
                {
                    if ((ex is RemotingException || ex is TargetInvocationException) &&
                        ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }

                    returnMessage = new ReturnMessage(ex, methodMessage);
                }

                return(returnMessage);
            }
Exemple #2
0
            /// <inheritdoc />
            public override IMessage Invoke(IMessage msg)
            {
                MethodCallMessageWrapper callMessageWrapper = new MethodCallMessageWrapper((IMethodCallMessage)msg);
                MethodInfo methodBase = callMessageWrapper.MethodBase as MethodInfo;

                if (methodBase == null)
                {
                    return(null);
                }

                var instance = Express.Instance;
                var type     = instance.GetType();

                Type[] argumentTypeList;
                if (callMessageWrapper.Args?.Any() is true)
                {
                    argumentTypeList = callMessageWrapper.Args.Select(temp => temp.GetType()).ToArray();
                }
                else
                {
                    argumentTypeList = Type.EmptyTypes;
                }

                var method = type.GetMethod(methodBase.Name, argumentTypeList);

                if (method == null)
                {
                    throw new ArgumentException("调用方法不匹配,找不到" + methodBase + "方法");
                }

                return(new ReturnMessage(
                           method.Invoke(instance, callMessageWrapper.Args),
                           callMessageWrapper.Args, callMessageWrapper.ArgCount, callMessageWrapper.LogicalCallContext,
                           callMessageWrapper));
            }
        // Messages will be intercepted here and redirected
        // to another object.
        public override IMessage Invoke(IMessage msg)
        {
            if (msg is IConstructionCallMessage)
            {
                IActivator remActivator        = (IActivator)RemotingServices.Connect(typeof(IActivator), "tcp://localhost:1234/RemoteActivationService.rem");
                IConstructionReturnMessage crm = remActivator.Activate((IConstructionCallMessage)msg);
                return(crm);
            }
            else
            {
                methodOverloaded = RemotingServices.IsMethodOverloaded((IMethodMessage)msg);

                _mthBase = RemotingServices.GetMethodBaseFromMethodMessage((IMethodMessage)msg);
                MethodCallMessageWrapper mcm = new MethodCallMessageWrapper((IMethodCallMessage)msg);
                mcm.Uri = RemotingServices.GetObjectUri((MarshalByRefObject)target);
                MarshalByRefObject objRem = (MarshalByRefObject)Activator.CreateInstance(GetProxiedType());
                RemotingServices.ExecuteMessage((MarshalByRefObject)objRem, (IMethodCallMessage)msg);
                IMessage rtnMsg = null;

                try
                {
                    rtnMsg = _sink.SyncProcessMessage(msg);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                return(rtnMsg);
            }
        }
Exemple #4
0
        public override IMessage Invoke(IMessage msg)
        {
            object             returnValue   = null;
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);

            if (methodMessage.MethodBase.DeclaringType == typeof(object))
            {
                switch (methodMessage.MethodName)
                {
                case "GetType":
                    returnValue = this.proxyType;
                    break;

                case "Equals":
                    returnValue = this.Equals(methodMessage.GetArg(0));
                    break;

                case "GetHashCode":
                    returnValue = this.GetHashCode();
                    break;

                case "ToString":
                    returnValue = this.proxyType.FullName;
                    break;
                }
            }
            else
            {
                var parameters = this.BuildParameters(methodMessage);
                returnValue = this.Call(parameters, methodMessage);
            }
            return(new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage));
        }
Exemple #5
0
        public override IMessage Invoke(IMessage msg)
        {
            var head = new Header()
            {
                Transaction = PaoTransaction.Current, UserToken = SecurityPublic.CurrentUser
            };
            var headString = RemotePublic.Serialize(head);

            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            var inArgs = methodMessage.InArgs;

            byte[] argString = null;
            if (!inArgs.IsNull())
            {
                argString = RemotePublic.Serialize(inArgs);
            }

            var resultBinary = RemoteService.CallService(RemotePublic.Serialize(ServiceName)
                                                         , RemotePublic.Serialize(methodMessage.MethodName)
                                                         , headString, argString);
            var result = RemotePublic.Deserialize <object>(resultBinary);

            // Create the return message (ReturnMessage)
            return(new ReturnMessage(result, null, 0, methodMessage.LogicalCallContext, methodMessage));
        }
Exemple #6
0
        public override IMessage Invoke(IMessage msg)
        {
            var mc     = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            var method = mc.MethodBase as MethodInfo;

            if (method == null)
            {
                throw new NullReferenceException("Method is null");
            }

            var    args = ((method.Attributes & MethodAttributes.SpecialName) == MethodAttributes.SpecialName) ? mc.InArgs : mc.Args;
            object res;

            var argumentkey = cacheManager.GetArgumentKey(cacheName, method, args);
            var key         = argumentkey ?? cacheManager.GetKey(method.Name, ApplicationArgs(args));

            if (IsCached(key, out res))
            {
                return(CreateReturnMessage(mc, res));
            }
            using (cacheManager.AcquireLock(cacheName, key))
            {
                if (IsCached(key, out res))
                {
                    return(CreateReturnMessage(mc, res));
                }
                res = InvokeMethod(method, args);
                AddToCache(key, res);
            }

            return(CreateReturnMessage(mc, res));
        }
Exemple #7
0
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);

            ExecuteType executeType = GetExecuteType(methodMessage);

            if (executeType != ExecuteType.None)
            {
                profiler.ExecuteStart(instance, executeType);
            }

            object returnValue = methodMessage.MethodBase.Invoke(instance, methodMessage.Args);

            if (executeType == ExecuteType.Reader)
            {
                returnValue = new ProfiledDbDataReader((DbDataReader)returnValue, instance.Connection, profiler);
            }

            IMessage returnMessage = new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount,
                                                       methodMessage.LogicalCallContext, methodMessage);

            if (executeType == ExecuteType.Reader)
            {
                profiler.ExecuteFinish(instance, executeType, (DbDataReader)returnValue);
            }
            else if (executeType != ExecuteType.None)
            {
                profiler.ExecuteFinish(instance, executeType, null);
            }

            return(returnMessage);
        }
Exemple #8
0
        /// <summary>
        /// This reflective method for invoking methods. Overriden from RealProxy.
        /// Handles the object invocation. This method wrapps an instance call to the hessian
        /// requests, sends it to the hessian service and translates the reply of this call to the C# - data type
        /// <see cref="System.Runtime.Remoting.Proxies.RealProxy"/>
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage msg)
        {
            // Convert to a MethodCallMessage
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            MethodInfo         methodInfo    = this.GetMethodInfoForMethodBase(methodMessage);

            Type[] argumentTypes  = CHessianMethodCaller.GetArgTypes(methodMessage.Args);
            object objReturnValue = null;

            if (methodInfo != null)
            {
                if (methodInfo.Name.Equals("Equals") && argumentTypes != null &&
                    argumentTypes.Length == 1 && argumentTypes[0].IsAssignableFrom((typeof(Object))))
                {
                    Object value = methodMessage.Args[0];
                    if (value == null)
                    {
                        objReturnValue = false;
                    }
                    else if (value.GetType().Equals(typeof(CHessianProxy)) ||
                             value.GetType().IsAssignableFrom(typeof(CHessianProxy)))
                    {
                        objReturnValue = this.m_methodCaller.URI.Equals(((CHessianProxy)value).URI);
                    }
                    else
                    {
                        objReturnValue = false;
                    }
                }
                else if (methodInfo.Name.Equals("GetHashCode") && argumentTypes.Length == 0)
                {
                    objReturnValue = this.m_methodCaller.URI.GetHashCode();
                }
                else if (methodInfo.Name.Equals("GetHessianURL"))
                {
                    objReturnValue = this.m_methodCaller.URI.ToString();
                }
                else if (methodInfo.Name.Equals("ToString") && argumentTypes.Length == 0)
                {
                    objReturnValue = "[HessianProxy " + this.m_methodCaller.URI + "]";
                }
                else if (methodInfo.Name.Equals("GetType") && argumentTypes.Length == 0)
                {
                    objReturnValue = this.m_proxyType;
                }
                else
                {
                    objReturnValue = this.m_methodCaller.DoHessianMethodCall(methodMessage.Args, methodInfo);
                }
            }
            else
            {
                if (methodMessage.MethodName.Equals("GetType") && (methodMessage.ArgCount == 0))
                {
                    objReturnValue = this.m_proxyType;
                }
            }
            // Create the return message (ReturnMessage)
            return(new ReturnMessage(objReturnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage));
        }
        public override IMessage Invoke(IMessage message)
        {
            if (message is IConstructionCallMessage)
            {
                IConstructionReturnMessage myIConstructionReturnMessage =
                    this.InitializeServerObject((IConstructionCallMessage)message);
                ConstructionResponse constructionResponse = new
                                                            ConstructionResponse(null, (IMethodCallMessage)message);
                return(constructionResponse);
            }
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)message);

            MethodBase method = methodMessage.MethodBase;

            object returnValue = null;

            if (method.DeclaringType == typeof(IDynamicProxy))
            {
                returnValue = method.Invoke(this, methodMessage.Args);
            }
            else
            {
                returnValue = InvocationHandler(ProxyTargetTyped, method, methodMessage.Args);
            }

            ReturnMessage returnMessage = new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage);

            return(returnMessage);
        }
        /// <summary>
        /// The reflective method for invoking methods. See documentation for RealProxy.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage message)
        {
            // Convert to a MethodCallMessage
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)message);

            // Extract the method being called
            System.Reflection.MethodBase method = methodMessage.MethodBase;

            // Perform the call
            object returnValue = null;

            if (method.DeclaringType == typeof(IDynamicProxy))
            {
                // Handle IDynamicProxy interface calls on this instance instead of on the proxy target instance
                returnValue = method.Invoke(this, methodMessage.Args);
            }
            else
            {
                // Delegate to the invocation handler
                returnValue = invocationHandler(proxyTarget, method, methodMessage.Args);
            }

            // Create the return message (ReturnMessage)
            ReturnMessage returnMessage = new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage);

            return(returnMessage);
        }
Exemple #11
0
        void InvokeEnd(Task response, IClientChannel channel, MethodCallMessageWrapper methodCallWrapper)
        {
            Exception  exception = null;
            MethodInfo method    = methodCallWrapper.MethodBase as MethodInfo;

            try
            {
                FlowContextForTest(methodCallWrapper);

                if (response.IsFaulted)
                {
                    exception = EvaluateException(response.Exception);
                    throw exception;
                }
            }
            finally
            {
                OnPostInvoke(method, exception);
                if (channel.State != CommunicationState.Closed && channel.State != CommunicationState.Faulted)
                {
                    try
                    {
                        channel.Close();
                    }
                    catch
                    {
                        channel.Abort();
                    }
                }
                channel = null;
            }
        }
        /// <inheritdoc />
        /// <summary>
        /// 实际执行的拦截,并根据装饰属性进行定制处理
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage msg)
        {
            var caller = new MethodCallMessageWrapper((IMethodCallMessage)msg);

            //提取实际宿主对象
            var method = (MethodInfo)caller.MethodBase;

            if (method == null)
            {
                throw new NullReferenceException(nameof(method));
            }

            var target = (T)GetUnwrappedServer();

            var attributes = (DecoratorAttributeBase[])method.GetCustomAttributes(typeof(DecoratorAttributeBase), true);

            if (attributes.Length > 0)
            {
                foreach (var attribute in attributes)
                {
                    attribute.Intercept(caller);
                }
            }

            var ret = method.Invoke(target, caller.Args);

            //拦截处理后,继续回到宿主对象的调用过程
            return(new ReturnMessage(ret, caller.Args, caller.ArgCount, caller.LogicalCallContext, caller));
        }
Exemple #13
0
        /// <summary>
        /// 实际执行的拦截,并根据装饰属性进行定制处理。
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage msg)
        {
            MethodCallMessageWrapper caller =
                new MethodCallMessageWrapper((IMethodCallMessage)msg);
            // 提取实际宿主对象
            MethodInfo method = (MethodInfo)caller.MethodBase;
            T          target = (T)GetUnwrappedServer();

            if (target == null)
            {
                return(msg);
            }

            DecoratorAttributeBase[] attributes = (DecoratorAttributeBase[])
                                                  method.GetCustomAttributes(typeof(DecoratorAttributeBase), true);
            if (attributes.Length > 0)
            {
                foreach (DecoratorAttributeBase attribute in attributes)
                {
                    attribute.Intercept(caller);
                }
            }
            object ret = method.Invoke(target, caller.Args);

            // 拦截处理后,继续回到宿主对象的调用过程
            return(new ReturnMessage(ret, caller.Args, caller.ArgCount,
                                     caller.LogicalCallContext, caller));
        }
Exemple #14
0
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            var    methodInfo     = GetMethods(_proxyType).FirstOrDefault(item => item.ToString() == methodMessage.MethodBase.ToString());
            object objReturnValue = null;

            if (methodMessage.MethodName.Equals("GetType") && (methodMessage.ArgCount == 0))
            {
                objReturnValue = _proxyType;
            }
            else if (methodInfo != null)
            {
                if (methodInfo.Name.Equals("Equals") ||
                    methodInfo.Name.Equals("GetHashCode") ||
                    methodInfo.Name.Equals("ToString") ||
                    methodInfo.Name.Equals("GetType"))
                {
                    throw new Exception();
                }
                if (_unProxyMethods.All(item => item != methodInfo.Name))
                {
                    objReturnValue = methodInfo.Name + "abc";
                }
            }
            return(new ReturnMessage(objReturnValue, methodMessage.Args, methodMessage.ArgCount,
                                     methodMessage.LogicalCallContext, methodMessage));
        }
Exemple #15
0
        /// In the Invoke Method we do our interception work.
        public override IMessage Invoke(IMessage msg)
        {
            IMethodMessage returnMsg = null;

            if (msg is IConstructionCallMessage)
            {
                _realObject = GetInstance(msg as IConstructionCallMessage, ref returnMsg);
                _objType    = ((IConstructionCallMessage)msg).ActivationType;
                //SetStubData(this, _realObject);
            }
            else
            {
                if (!_isInit)
                {
                    DoObjectInitialize(_target);
                    _isInit = true;
                }

                ///the MethodCallMessageWrapper provides read/write access to the method call arguments.
                MethodCallMessageWrapper methodCallMsg = new MethodCallMessageWrapper((IMethodCallMessage)msg);
                ///This is the reflected method base of the called method.
                MethodInfo methodInfo = (MethodInfo)methodCallMsg.MethodBase;

                ///This is the object we are proxying.
                MarshalByRefObject owner = _target;
                //MarshalByRefObject owner = _realObject as MarshalByRefObject;
                ///Some basic initializations for later use
                if (owner != null)
                {
                    ChoMemberInfo memberInfo = new ChoMemberInfo(owner, _objType, methodCallMsg);
                    if (!ChoType.IsValidObjectMember(memberInfo.Info))
                    {
                        //|| ChoType.GetAttribute<ChoPropertyInfoAttribute>(memberInfo.Info, false) == null)
                        return(RemotingServices.ExecuteMessage(owner, methodCallMsg));
                    }

                    return(ExecuteMethod(ref returnMsg, methodCallMsg, owner, memberInfo));

                    //if (methodCallMsg.LogicalCallContext.GetData("CallerId") != null)
                    //    return ExecuteMethod(ref returnMsg, methodCallMsg, owner, memberInfo);
                    //else
                    //{
                    //    methodCallMsg.LogicalCallContext.SetData("CallerId", ChoRandom.NextRandom());
                    //    IChoAsyncResult result = ChoQueuedExecutionService.Global.Enqueue(() =>
                    //        {
                    //            return ExecuteMethod(ref returnMsg, methodCallMsg, owner, memberInfo);
                    //        });

                    //    return (IMessage)result.EndInvoke();
                    //}
                }
                else
                {
                    return(new ReturnMessage(new NullReferenceException("Missing target object."), methodCallMsg));
                }
            }

            return(returnMsg);
        }
Exemple #16
0
        /// <summary>
        /// This reflective method for invoking methods. Overriden from RealProxy.
        /// Handles the object invocation. This method wrapps an instance call to the service
        /// requests, sends it to the service and translates the reply of this call to the C# - data type
        /// <see cref="System.Runtime.Remoting.Proxies.RealProxy"/>
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public override IMessage Invoke(IMessage msg)
        {
            // Convert to a MethodCallMessage
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            MethodInfo         methodInfo    = this.GetMethodInfoForMethodBase(methodMessage);

            var ps = methodInfo.GetParameters();

            Type[] argumentTypes = new Type[ps.Length];
            for (int i = 0; i < ps.Length; i++)
            {
                argumentTypes[i] = ps[i].ParameterType;
            }
            object objReturnValue = null;

            if (methodInfo != null)
            {
                if (methodInfo.Name.Equals("Equals") && argumentTypes != null &&
                    argumentTypes.Length == 1 && argumentTypes[0].IsAssignableFrom((typeof(Object))))
                {
                    Object value = methodMessage.Args[0];
                    if (value == null)
                    {
                        objReturnValue = false;
                    }
                    else
                    {
                        objReturnValue = false;
                    }
                }
                else if (methodInfo.Name.Equals("GetHashCode") && argumentTypes.Length == 0)
                {
                    objReturnValue = msg.GetHashCode();
                }
                else if (methodInfo.Name.Equals("ToString") && argumentTypes.Length == 0)
                {
                    objReturnValue = this.ToString();
                }
                else if (methodInfo.Name.Equals("GetType") && argumentTypes.Length == 0)
                {
                    objReturnValue = this.m_proxyType;
                }
                else
                {
                    objReturnValue = this.m_methodCaller.DoMethodCall(methodMessage.Args, methodInfo);
                }
            }
            else
            {
                if (methodMessage.MethodName.Equals("GetType") && (methodMessage.ArgCount == 0))
                {
                    objReturnValue = this.m_proxyType;
                }
            }

            // Create the return message (ReturnMessage)
            return(new ReturnMessage(objReturnValue, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage));
        }
 public override IMessage Invoke(IMessage message)
 {
     if ((message as IMethodMessage).MethodName.Equals("get_Id"))
     {
         MethodCallMessageWrapper methodCallWrapper = new MethodCallMessageWrapper((IMethodCallMessage)message);
         return(new ReturnMessage(m_ActorId, null, 0, methodCallWrapper.LogicalCallContext, methodCallWrapper));
     }
     return(base.Invoke(message));
 }
Exemple #18
0
 private static ReturnMessage CreateReturnMessage(MethodCallMessageWrapper mc, object res)
 {
     if (mc == null)
     {
         throw new ArgumentNullException("mc");
     }
     return(mc.Args == null ?
            new ReturnMessage(res, mc.Args, 0, mc.LogicalCallContext, mc) :
            new ReturnMessage(res, mc.Args, mc.Args.Length, mc.LogicalCallContext, mc));
 }
    public override IMessage Invoke(IMessage message)
    {
        IMethodCallMessage methodMessage =
            new MethodCallMessageWrapper((IMethodCallMessage)message);
        // Obtain the actual method definition that is being called.
        MethodBase method = methodMessage.MethodBase;

        Type[] genericArgs = method.GetGenericArguments(); //This is what you want
        return(new ReturnMessage(...));
    }
        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            string             method        = (string)msg.Properties["__MethodName"];

            object[] args   = (object[])msg.Properties["__Args"];
            object   retObj = _Instance.GetType().InvokeMember(method, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, _Instance, args);

            return(new ReturnMessage(retObj, methodMessage.Args, methodMessage.ArgCount, methodMessage.LogicalCallContext, methodMessage));
        }
Exemple #21
0
        private ReturnMessage ExecuteMethod(ref IMethodMessage returnMsg, MethodCallMessageWrapper methodCallMsg, MarshalByRefObject owner, ChoMemberInfo memberInfo)
        {
            try
            {
                PreMethodInvoke(owner, methodCallMsg, memberInfo);

                returnMsg = RemotingServices.ExecuteMessage(owner, methodCallMsg);
                memberInfo.ReturnMessage = returnMsg as ReturnMessage;

                if (memberInfo.Info != null)
                {
                    memberInfo.MethodMsg = returnMsg;
                }

                PostMethodInvoke(owner, methodCallMsg, memberInfo);

                return(memberInfo.ReturnMessage);
            }
            catch (TargetInvocationException ex)
            {
                return(new ReturnMessage(ex.InnerException, methodCallMsg));
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                //Trace.TraceError(ex.ToString());
                ChoInterceptableObject interceptableObject = owner as ChoInterceptableObject;
                if (interceptableObject != null && interceptableObject.Initialized)
                {
                    if (interceptableObject.IsConfigObjectSilent)
                    {
                        ChoConfigurationObjectErrorManagerService.SetObjectMemberError(interceptableObject, memberInfo.Name,
                                                                                       String.Format(Resources.ConfigConstructMsg, ChoString.ToString(memberInfo.Value), ex.Message));

                        //PostMethodInvoke(owner, methodCallMsg, memberInfo);

                        //return new ReturnMessage(null, methodCallMsg);
                    }
                }
                string msg = "{0}: {1}".FormatString(memberInfo.Name, String.Format(Resources.ConfigConstructMsg, ChoString.ToString(memberInfo.Value), ex.Message));
                ChoValidationException ex1 = new ChoValidationException(msg);
                if (OnMemberError(owner, memberInfo, ref ex))
                {
                    return(new ReturnMessage(null, null, 0, null, methodCallMsg));
                }
                else
                {
                    return(new ReturnMessage(ex1, methodCallMsg));
                }
            }
        }
        public override IMessage Invoke(IMessage msg)
        {
            Exception InnerException;

            msg.Properties["__Uri"] = _TcpUrl;
            IMessage retMsg = _messageSinks[0].SyncProcessMessage(msg);

            // Handle construction call message differently than other method
            // call messages.
            if (msg is IConstructionCallMessage)
            {
                // need to finish CAO activation manually.
                string url = GetUrlForCAO(_serverType);
                if (url.Length > 0)
                {
                    ActivatedCAO((IConstructionCallMessage)msg, url);
                }

                IConstructionReturnMessage crm = EnterpriseServicesHelper.CreateConstructionReturnMessage((IConstructionCallMessage)msg, (MarshalByRefObject)this.GetTransparentProxy());
                return(crm);
            }
            else
            {
                if (retMsg is IMethodReturnMessage)
                {
                    IMethodReturnMessage mrm = (IMethodReturnMessage)retMsg;
                    if (mrm.Exception == null)
                    {
                        return(retMsg);
                    }
                    else
                    {
                        InnerException = mrm.Exception;
                    }
                }
                else
                {
                    MethodCallMessageWrapper mcm = new MethodCallMessageWrapper((IMethodCallMessage)msg);
                    mcm.Uri = RemotingServices.GetObjectUri((MarshalByRefObject)_targetTcp);
                    return(RemotingServices.GetEnvoyChainForProxy((MarshalByRefObject)_targetTcp).SyncProcessMessage(msg));
                }
                msg.Properties["__Uri"] = _HttpUrl;
                retMsg = _messageSinks[1].SyncProcessMessage(msg);
                if (retMsg is IMethodReturnMessage)
                {
                    IMethodReturnMessage mrm = (IMethodReturnMessage)retMsg;
                }
                return(retMsg);
            }
        }
Exemple #23
0
        IMessage InvokeBegin(IMessage message)
        {
            MethodCallMessageWrapper methodCallWrapper = new MethodCallMessageWrapper((IMethodCallMessage)message);

            FlowContextForTest(methodCallWrapper);

            IClientChannel channel = m_Factory.CreateChannel() as IClientChannel;

            channel.OperationTimeout = TimeSpan.MaxValue;
            channel.Open();

            OnPreInvoke(methodCallWrapper.MethodBase as MethodInfo);
            IMessage response = InjectContinuation(message, channel, methodCallWrapper);

            return(response);
        }
Exemple #24
0
 void FlowContextForTest(MethodCallMessageWrapper methodCallWrapper)
 {
     if (Test.TestHelper.IsUnderTest())
     {
         IEnumerable <AsyncContext> test = methodCallWrapper.Properties.Values.Cast <AsyncContext>();
         if (test.Any() == false)
         {
             methodCallWrapper.Properties.Add(ContextKey, new AsyncContext());
         }
         else
         {
             AsyncContext context = test.First();
             context.Restore();
         }
     }
 }
Exemple #25
0
        public override void Intercept(object target)
        {
            MethodCallMessageWrapper caller = (MethodCallMessageWrapper)target;

            if (caller.ArgCount == 0)
            {
                return;
            }
            foreach (object arg in caller.Args)
            {
                if (string.IsNullOrEmpty((string)arg))
                {
                    throw new ArgumentException("string is null or empty");
                }
            }
        }
Exemple #26
0
        public override void Intercept(object target)
        {
            MethodCallMessageWrapper caller = (MethodCallMessageWrapper)target;

            if (caller.ArgCount == 0)
            {
                return;
            }
            for (int i = 0; i < caller.ArgCount; i++)
            {
                object arg = caller.Args[i];
                if ((arg.GetType() != type) && (!arg.GetType().IsAssignableFrom(type)))
                {
                    throw new ArgumentException(i.ToString());
                }
            }
        }
Exemple #27
0
        internal ChoMemberInfo(object target, Type type, IMethodMessage methodCallMsg)
        {
            _methodMsg    = methodCallMsg;
            _target       = target;
            MethodCallMsg = _methodMsg as MethodCallMessageWrapper;

            if (_methodMsg.MethodName == "FieldSetter" || (_methodMsg.MethodName.StartsWith("set_") &&
                                                           _methodMsg.MethodName != "set_Dirty"))
            {
                _dirtyOperation = true;
            }

            if (_methodMsg.MethodName == "FieldSetter" || _methodMsg.MethodName == "FieldGetter")
            {
                _name       = _methodMsg.Args[1] as string;
                _memberType = MemberTypes.Field;
            }
            else if (_methodMsg.MethodName.StartsWith("get_"))
            {
                _name       = _methodMsg.MethodName.Replace("get_", String.Empty);
                _memberType = MemberTypes.Property;
            }
            else if (_methodMsg.MethodName.StartsWith("set_"))
            {
                _name       = _methodMsg.MethodName.Replace("set_", String.Empty);
                _memberType = MemberTypes.Property;
            }
            else
            {
                _name = _methodMsg.MethodName;
            }

            //_memberInfo = ChoType.GetMember(_target.GetType(), Name);
            _memberInfo = ChoType.GetMemberInfo(type, Name);

            if (_dirtyOperation)
            {
                //Set Converted value
                Value = ChoConvert.ConvertFrom(_target, Value, ChoType.GetMemberType(_memberInfo),
                                               ChoTypeDescriptor.GetTypeConverters(_memberInfo), ChoTypeDescriptor.GetTypeConverterParams(_memberInfo));
                //Value = ChoConvert.ConvertFrom(_target, Value, ChoType.GetMemberType(_target.GetType(), _name),
                //    ChoType.GetTypeConverters(ChoType.GetMember(_target.GetType(), _name), typeof(ChoTypeConverterAttribute)));
            }
        }
Exemple #28
0
        IMessage InjectContinuation(IMessage message, IClientChannel channel, MethodCallMessageWrapper methodCallWrapper)
        {
            Task returnTask = null;

            if ((methodCallWrapper.MethodBase as MethodInfo).ReturnType.IsGenericType == false)
            {
                returnTask = InvokeAsync(channel, methodCallWrapper);
            }
            else
            {
                Type       returnArg         = (methodCallWrapper.MethodBase as MethodInfo).ReturnType.GetGenericArguments()[0];
                MethodInfo strongInvokeAsnyc = m_InvokeAsyncGenDef.MakeGenericMethod(returnArg);
                returnTask = strongInvokeAsnyc.Invoke(this, new object[] { channel, methodCallWrapper }) as Task;
            }

            ReturnMessage taskMessage = new ReturnMessage(returnTask, null, 0, methodCallWrapper.LogicalCallContext, methodCallWrapper);

            return(taskMessage);
        }
Exemple #29
0
        /// <summary>
        /// Executes a method call represented by the <paramref name="msg"/>
        /// parameter. The CLR will call this method when a method is called
        /// on the TransparentProxy. This method runs the invocation through
        /// the call handler pipeline and finally sends it down to the
        /// target object, and then back through the pipeline.
        /// </summary>
        /// <param name="msg">An <see cref="IMessage"/> object that contains the information
        /// about the method call.</param>
        /// <returns>An <see cref="RemotingMethodReturn"/> object contains the
        /// information about the target method's return value.</returns>
        public override IMessage Invoke(IMessage msg)
        {
            ///the MethodCallMessageWrapper provides read/write access to the method call arguments.
            MethodCallMessageWrapper mc = new MethodCallMessageWrapper((IMethodCallMessage)msg);

            ///This is the reflected method base of the called method.
            MethodInfo mi = (MethodInfo)mc.MethodBase;

            ///This is the object we are proxying.
            MarshalByRefObject owner = GetUnwrappedServer();

            IMessage retval = null;
            object   outVal = null;

            /// The caller is expecting to get something out of the method invocation.  So we need to construct a return message
            /// This is pretty simple, pass back out most of the stuff that was passed into us, and hand it the outVal as well.
            retval = new ReturnMessage(outVal, mc.Args, mc.Args.Length, mc.LogicalCallContext, mc);
            return(retval);
        }
        public override IMessage Invoke(IMessage message)
        {
            var methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)message);
            var method        = methodMessage.MethodBase;

            object returnValue = null;

            if (method.DeclaringType == typeof(IDynamicProxy))
            {
                returnValue = method.Invoke(this, methodMessage.Args);
            }
            else
            {
                returnValue = invocationHandler(proxyTarget, method, methodMessage.Args);
            }

            return(new ReturnMessage(returnValue, methodMessage.Args, methodMessage.ArgCount,
                                     methodMessage.LogicalCallContext, methodMessage));
        }