Beispiel #1
0
        private static bool IsCurrentContextOK(RuntimeType serverType, object[] props, ref ConstructorCallMessage ctorCallMsg)
        {
            object[] callSiteActivationAttributes = PeekActivationAttributes(serverType);
            if (callSiteActivationAttributes != null)
            {
                PopActivationAttributes(serverType);
            }
            object[] womAttr = new object[] { GetGlobalAttribute() };
            object[] contextAttributesForType = GetContextAttributesForType(serverType);
            Context  currentContext           = Thread.CurrentContext;

            ctorCallMsg           = new ConstructorCallMessage(callSiteActivationAttributes, womAttr, contextAttributesForType, serverType);
            ctorCallMsg.Activator = new ConstructionLevelActivator();
            bool flag = QueryAttributesIfContextOK(currentContext, ctorCallMsg, womAttr);

            if (flag)
            {
                flag = QueryAttributesIfContextOK(currentContext, ctorCallMsg, callSiteActivationAttributes);
                if (flag)
                {
                    flag = QueryAttributesIfContextOK(currentContext, ctorCallMsg, contextAttributesForType);
                }
            }
            return(flag);
        }
        internal static void PropagateOutParameters(IMessage msg, object[] outArgs, object returnValue)
        {
            Message message = msg as Message;

            if (message == null)
            {
                ConstructorCallMessage message2 = msg as ConstructorCallMessage;
                if (message2 != null)
                {
                    message = message2.GetMessage();
                }
            }
            if (message == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Remoting_Proxy_ExpectedOriginalMessage"));
            }
            RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(message.GetMethodBase());

            if ((outArgs != null) && (outArgs.Length > 0))
            {
                object[]        args       = message.Args;
                ParameterInfo[] parameters = reflectionCachedData.Parameters;
                foreach (int num in reflectionCachedData.MarshalRequestArgMap)
                {
                    ParameterInfo info = parameters[num];
                    if ((info.IsIn && info.ParameterType.IsByRef) && !info.IsOut)
                    {
                        outArgs[num] = args[num];
                    }
                }
                if (reflectionCachedData.NonRefOutArgMap.Length > 0)
                {
                    foreach (int num2 in reflectionCachedData.NonRefOutArgMap)
                    {
                        Array destinationArray = args[num2] as Array;
                        if (destinationArray != null)
                        {
                            Array.Copy((Array)outArgs[num2], destinationArray, destinationArray.Length);
                        }
                    }
                }
                int[] outRefArgMap = reflectionCachedData.OutRefArgMap;
                if (outRefArgMap.Length > 0)
                {
                    foreach (int num3 in outRefArgMap)
                    {
                        ValidateReturnArg(outArgs[num3], parameters[num3].ParameterType);
                    }
                }
            }
            if ((message.GetCallType() & 15) != 1)
            {
                Type returnType = reflectionCachedData.ReturnType;
                if (returnType != null)
                {
                    ValidateReturnArg(returnValue, returnType);
                }
            }
            message.PropagateOutParameters(outArgs, returnValue);
        }
Beispiel #3
0
        internal static MarshalByRefObject CreateInstance(RuntimeType serverType)
        {
            MarshalByRefObject     transparentProxy = null;
            ConstructorCallMessage ctorCallMsg      = null;
            RemotingProxy          realProxy;
            bool flag = IsCurrentContextOK(serverType, null, ref ctorCallMsg);

            if (flag && !serverType.IsContextful)
            {
                return(RemotingServices.AllocateUninitializedObject(serverType));
            }
            transparentProxy = (MarshalByRefObject)ConnectIfNecessary(ctorCallMsg);
            if (transparentProxy == null)
            {
                realProxy        = new RemotingProxy(serverType);
                transparentProxy = (MarshalByRefObject)realProxy.GetTransparentProxy();
            }
            else
            {
                realProxy = (RemotingProxy)RemotingServices.GetRealProxy(transparentProxy);
            }
            realProxy.ConstructorMessage = ctorCallMsg;
            if (!flag)
            {
                ContextLevelActivator activator = new ContextLevelActivator {
                    NextActivator = ctorCallMsg.Activator
                };
                ctorCallMsg.Activator = activator;
                return(transparentProxy);
            }
            ctorCallMsg.ActivateInContext = true;
            return(transparentProxy);
        }
        private static bool IsCurrentContextOK(RuntimeType serverType, object[] props, ref ConstructorCallMessage ctorCallMsg)
        {
            object[] objArray1 = ActivationServices.PeekActivationAttributes((Type)serverType);
            if (objArray1 != null)
            {
                ActivationServices.PopActivationAttributes((Type)serverType);
            }
            object[] objArray2 = new object[1] {
                (object)ActivationServices.GetGlobalAttribute()
            };
            object[] objArray3      = (object[])ActivationServices.GetContextAttributesForType((Type)serverType);
            Context  currentContext = Thread.CurrentContext;

            ctorCallMsg           = new ConstructorCallMessage(objArray1, objArray2, objArray3, serverType);
            ctorCallMsg.Activator = (IActivator) new ConstructionLevelActivator();
            bool flag = ActivationServices.QueryAttributesIfContextOK(currentContext, (IConstructionCallMessage)ctorCallMsg, objArray2);

            if (flag)
            {
                flag = ActivationServices.QueryAttributesIfContextOK(currentContext, (IConstructionCallMessage)ctorCallMsg, objArray1);
                if (flag)
                {
                    flag = ActivationServices.QueryAttributesIfContextOK(currentContext, (IConstructionCallMessage)ctorCallMsg, objArray3);
                }
            }
            return(flag);
        }
        private static bool IsCurrentContextOK(RuntimeType serverType, object[] props, ref ConstructorCallMessage ctorCallMsg)
        {
            object[] array = ActivationServices.PeekActivationAttributes(serverType);
            if (array != null)
            {
                ActivationServices.PopActivationAttributes(serverType);
            }
            object[] array2 = new object[]
            {
                ActivationServices.GetGlobalAttribute()
            };
            object[] contextAttributesForType = ActivationServices.GetContextAttributesForType(serverType);
            Context  currentContext           = Thread.CurrentContext;

            ctorCallMsg           = new ConstructorCallMessage(array, array2, contextAttributesForType, serverType);
            ctorCallMsg.Activator = new ConstructionLevelActivator();
            bool flag = ActivationServices.QueryAttributesIfContextOK(currentContext, ctorCallMsg, array2);

            if (flag)
            {
                flag = ActivationServices.QueryAttributesIfContextOK(currentContext, ctorCallMsg, array);
                if (flag)
                {
                    flag = ActivationServices.QueryAttributesIfContextOK(currentContext, ctorCallMsg, contextAttributesForType);
                }
            }
            return(flag);
        }
        internal static MarshalByRefObject CreateInstance(RuntimeType serverType)
        {
            ConstructorCallMessage ctorCallMsg = (ConstructorCallMessage)null;
            bool flag = ActivationServices.IsCurrentContextOK(serverType, (object[])null, ref ctorCallMsg);
            MarshalByRefObject marshalByRefObject;

            if (flag && !serverType.IsContextful)
            {
                marshalByRefObject = RemotingServices.AllocateUninitializedObject(serverType);
            }
            else
            {
                marshalByRefObject = (MarshalByRefObject)ActivationServices.ConnectIfNecessary((IConstructionCallMessage)ctorCallMsg);
                RemotingProxy remotingProxy;
                if (marshalByRefObject == null)
                {
                    remotingProxy      = new RemotingProxy((Type)serverType);
                    marshalByRefObject = (MarshalByRefObject)remotingProxy.GetTransparentProxy();
                }
                else
                {
                    remotingProxy = (RemotingProxy)RemotingServices.GetRealProxy((object)marshalByRefObject);
                }
                remotingProxy.ConstructorMessage = ctorCallMsg;
                if (!flag)
                {
                    ctorCallMsg.Activator = (IActivator) new ContextLevelActivator()
                    {
                        NextActivator = ctorCallMsg.Activator
                    }
                }
                ;
                else
                {
                    ctorCallMsg.ActivateInContext = true;
                }
            }
            return(marshalByRefObject);
        }
Beispiel #7
0
        private void PrivateInvoke(ref MessageData msgData, int type)
        {
            IMessage      message1      = (IMessage)null;
            CallType      callType      = (CallType)type;
            IMessage      message2      = (IMessage)null;
            int           msgFlags      = -1;
            RemotingProxy remotingProxy = (RemotingProxy)null;

            if (CallType.MethodCall == callType)
            {
                Message     message3 = new Message();
                MessageData msgData1 = msgData;
                message3.InitFields(msgData1);
                message1 = (IMessage)message3;
                msgFlags = message3.GetCallType();
            }
            else if (CallType.ConstructorCall == callType)
            {
                msgFlags      = 0;
                remotingProxy = this as RemotingProxy;
                bool flag = false;
                ConstructorCallMessage constructorCallMessage1;
                if (!this.IsRemotingProxy())
                {
                    constructorCallMessage1 = new ConstructorCallMessage((object[])null, (object[])null, (object[])null, (RuntimeType)this.GetProxiedType());
                }
                else
                {
                    constructorCallMessage1 = remotingProxy.ConstructorMessage;
                    Identity identityObject = remotingProxy.IdentityObject;
                    if (identityObject != null)
                    {
                        flag = identityObject.IsWellKnown();
                    }
                }
                if (constructorCallMessage1 == null | flag)
                {
                    ConstructorCallMessage constructorCallMessage2 = new ConstructorCallMessage((object[])null, (object[])null, (object[])null, (RuntimeType)this.GetProxiedType());
                    constructorCallMessage2.SetFrame(msgData);
                    message1 = (IMessage)constructorCallMessage2;
                    if (flag)
                    {
                        remotingProxy.ConstructorMessage = (ConstructorCallMessage)null;
                        if (constructorCallMessage2.ArgCount != 0)
                        {
                            throw new RemotingException(Environment.GetResourceString("Remoting_Activation_WellKnownCTOR"));
                        }
                    }
                    message2 = (IMessage) new ConstructorReturnMessage((MarshalByRefObject)this.GetTransparentProxy(), (object[])null, 0, (LogicalCallContext)null, (IConstructionCallMessage)constructorCallMessage2);
                }
                else
                {
                    constructorCallMessage1.SetFrame(msgData);
                    message1 = (IMessage)constructorCallMessage1;
                }
            }
            ChannelServices.IncrementRemoteCalls();
            if (!this.IsRemotingProxy() && (msgFlags & 2) == 2)
            {
                message2 = RealProxy.EndInvokeHelper(message1 as Message, true);
            }
            if (message2 == null)
            {
                Thread             currentThread      = Thread.CurrentThread;
                LogicalCallContext logicalCallContext = currentThread.GetMutableExecutionContext().LogicalCallContext;
                this.SetCallContextInMessage(message1, msgFlags, logicalCallContext);
                logicalCallContext.PropagateOutgoingHeadersToMessage(message1);
                message2 = this.Invoke(message1);
                this.ReturnCallContextToThread(currentThread, message2, msgFlags, logicalCallContext);
                Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext.PropagateIncomingHeadersToCallContext(message2);
            }
            if (!this.IsRemotingProxy() && (msgFlags & 1) == 1)
            {
                Message     m           = message1 as Message;
                AsyncResult asyncResult = new AsyncResult(m);
                IMessage    msg         = message2;
                asyncResult.SyncProcessMessage(msg);
                // ISSUE: variable of the null type
                __Null local1       = null;
                int    outArgsCount = 0;
                // ISSUE: variable of the null type
                __Null  local2   = null;
                Message message3 = m;
                message2 = (IMessage) new ReturnMessage((object)asyncResult, (object[])local1, outArgsCount, (LogicalCallContext)local2, (IMethodCallMessage)message3);
            }
            RealProxy.HandleReturnMessage(message1, message2);
            if (CallType.ConstructorCall != callType)
            {
                return;
            }
            IConstructionReturnMessage constructionReturnMessage = message2 as IConstructionReturnMessage;

            if (constructionReturnMessage == null)
            {
                throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_BadReturnTypeForActivation"));
            }
            ConstructorReturnMessage constructorReturnMessage = constructionReturnMessage as ConstructorReturnMessage;
            MarshalByRefObject       marshalByRefObject;

            if (constructorReturnMessage != null)
            {
                marshalByRefObject = (MarshalByRefObject)constructorReturnMessage.GetObject();
                if (marshalByRefObject == null)
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Activation_NullReturnValue"));
                }
            }
            else
            {
                marshalByRefObject = (MarshalByRefObject)RemotingServices.InternalUnmarshal((ObjRef)constructionReturnMessage.ReturnValue, this.GetTransparentProxy(), true);
                if (marshalByRefObject == null)
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Activation_NullFromInternalUnmarshal"));
                }
            }
            if (marshalByRefObject != (MarshalByRefObject)this.GetTransparentProxy())
            {
                throw new RemotingException(Environment.GetResourceString("Remoting_Activation_InconsistentState"));
            }
            if (!this.IsRemotingProxy())
            {
                return;
            }
            remotingProxy.ConstructorMessage = (ConstructorCallMessage)null;
        }
Beispiel #8
0
        internal static void PropagateOutParameters(IMessage msg, object[] outArgs, object returnValue)
        {
            Message message = msg as Message;

            if (message == null)
            {
                ConstructorCallMessage constructorCallMessage = msg as ConstructorCallMessage;
                if (constructorCallMessage != null)
                {
                    message = constructorCallMessage.GetMessage();
                }
            }
            if (message == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Remoting_Proxy_ExpectedOriginalMessage"));
            }
            RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(message.GetMethodBase());

            if (outArgs != null && outArgs.Length != 0)
            {
                object[]        args       = message.Args;
                ParameterInfo[] parameters = reflectionCachedData.Parameters;
                foreach (int marshalRequestArg in reflectionCachedData.MarshalRequestArgMap)
                {
                    ParameterInfo parameterInfo = parameters[marshalRequestArg];
                    if (parameterInfo.IsIn && parameterInfo.ParameterType.IsByRef && !parameterInfo.IsOut)
                    {
                        outArgs[marshalRequestArg] = args[marshalRequestArg];
                    }
                }
                if (reflectionCachedData.NonRefOutArgMap.Length != 0)
                {
                    foreach (int nonRefOutArg in reflectionCachedData.NonRefOutArgMap)
                    {
                        Array array = args[nonRefOutArg] as Array;
                        if (array != null)
                        {
                            Array sourceArray      = (Array)outArgs[nonRefOutArg];
                            Array destinationArray = array;
                            int   length           = destinationArray.Length;
                            Array.Copy(sourceArray, destinationArray, length);
                        }
                    }
                }
                int[] outRefArgMap = reflectionCachedData.OutRefArgMap;
                if (outRefArgMap.Length != 0)
                {
                    foreach (int index in outRefArgMap)
                    {
                        RealProxy.ValidateReturnArg(outArgs[index], parameters[index].ParameterType);
                    }
                }
            }
            if ((message.GetCallType() & 15) != 1)
            {
                Type returnType = reflectionCachedData.ReturnType;
                if (returnType != (Type)null)
                {
                    RealProxy.ValidateReturnArg(returnValue, returnType);
                }
            }
            message.PropagateOutParameters(outArgs, returnValue);
        }
        private void PrivateInvoke(ref MessageData msgData, int type)
        {
            IMessage      reqMsg   = null;
            CallType      type2    = (CallType)type;
            IMessage      retMsg   = null;
            int           msgFlags = -1;
            RemotingProxy proxy    = null;

            if (CallType.MethodCall == type2)
            {
                Message message3 = new Message();
                message3.InitFields(msgData);
                reqMsg   = message3;
                msgFlags = message3.GetCallType();
            }
            else if (CallType.ConstructorCall == type2)
            {
                msgFlags = 0;
                proxy    = this as RemotingProxy;
                ConstructorCallMessage ccm = null;
                bool flag = false;
                if (!this.IsRemotingProxy())
                {
                    ccm = new ConstructorCallMessage(null, null, null, (RuntimeType)this.GetProxiedType());
                }
                else
                {
                    ccm = proxy.ConstructorMessage;
                    Identity identityObject = proxy.IdentityObject;
                    if (identityObject != null)
                    {
                        flag = identityObject.IsWellKnown();
                    }
                }
                if ((ccm == null) || flag)
                {
                    ccm = new ConstructorCallMessage(null, null, null, (RuntimeType)this.GetProxiedType());
                    ccm.SetFrame(msgData);
                    reqMsg = ccm;
                    if (flag)
                    {
                        proxy.ConstructorMessage = null;
                        if (ccm.ArgCount != 0)
                        {
                            throw new RemotingException(Environment.GetResourceString("Remoting_Activation_WellKnownCTOR"));
                        }
                    }
                    retMsg = new ConstructorReturnMessage((MarshalByRefObject)this.GetTransparentProxy(), null, 0, null, ccm);
                }
                else
                {
                    ccm.SetFrame(msgData);
                    reqMsg = ccm;
                }
            }
            ChannelServices.IncrementRemoteCalls();
            if (!this.IsRemotingProxy() && ((msgFlags & 2) == 2))
            {
                Message message5 = reqMsg as Message;
                retMsg = EndInvokeHelper(message5, true);
            }
            if (retMsg == null)
            {
                LogicalCallContext cctx          = null;
                Thread             currentThread = Thread.CurrentThread;
                cctx = currentThread.GetLogicalCallContext();
                this.SetCallContextInMessage(reqMsg, msgFlags, cctx);
                cctx.PropagateOutgoingHeadersToMessage(reqMsg);
                retMsg = this.Invoke(reqMsg);
                this.ReturnCallContextToThread(currentThread, retMsg, msgFlags, cctx);
                CallContext.GetLogicalCallContext().PropagateIncomingHeadersToCallContext(retMsg);
            }
            if (!this.IsRemotingProxy() && ((msgFlags & 1) == 1))
            {
                Message     m   = reqMsg as Message;
                AsyncResult ret = new AsyncResult(m);
                ret.SyncProcessMessage(retMsg);
                retMsg = new ReturnMessage(ret, null, 0, null, m);
            }
            HandleReturnMessage(reqMsg, retMsg);
            if (CallType.ConstructorCall == type2)
            {
                MarshalByRefObject         obj2     = null;
                IConstructionReturnMessage message7 = retMsg as IConstructionReturnMessage;
                if (message7 == null)
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_BadReturnTypeForActivation"));
                }
                ConstructorReturnMessage message8 = message7 as ConstructorReturnMessage;
                if (message8 != null)
                {
                    obj2 = (MarshalByRefObject)message8.GetObject();
                    if (obj2 == null)
                    {
                        throw new RemotingException(Environment.GetResourceString("Remoting_Activation_NullReturnValue"));
                    }
                }
                else
                {
                    obj2 = (MarshalByRefObject)RemotingServices.InternalUnmarshal((ObjRef)message7.ReturnValue, this.GetTransparentProxy(), true);
                    if (obj2 == null)
                    {
                        throw new RemotingException(Environment.GetResourceString("Remoting_Activation_NullFromInternalUnmarshal"));
                    }
                }
                if (obj2 != ((MarshalByRefObject)this.GetTransparentProxy()))
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Activation_InconsistentState"));
                }
                if (this.IsRemotingProxy())
                {
                    proxy.ConstructorMessage = null;
                }
            }
        }
Beispiel #10
0
        [System.Security.SecurityCritical]  // auto-generated
        private void PrivateInvoke(ref MessageData msgData, int type)
        {
            IMessage reqMsg   = null;
            CallType callType = (CallType)type;
            IMessage retMsg   = null;
            int      msgFlags = -1;

            // Used only for Construction case
            RemotingProxy rp = null;

            // Create a message object based on the type of call
            if (CallType.MethodCall == callType)
            {
                Message msg = new Message();
                msg.InitFields(msgData);
                reqMsg   = msg;
                msgFlags = msg.GetCallType();
            }
            else if (CallType.ConstructorCall == (CallType)callType)
            {
                // We use msgFlags to handle CallContext around
                // the virtual call to Invoke()
                msgFlags = Message.Sync;

                rp = this as RemotingProxy;
                ConstructorCallMessage ctorMsg = null;
                bool bIsWellKnown = false;
                if (!IsRemotingProxy())
                {
                    // Create a new constructor call message
                    // <

                    ctorMsg = new ConstructorCallMessage(null, null, null, (RuntimeType)GetProxiedType());
                }
                else
                {
                    // Extract the constructor message set in the first step of activation.
                    ctorMsg = rp.ConstructorMessage;
                    // If the proxy is a wellknown client proxy, we don't
                    // need to run the c'tor.
                    Identity id = rp.IdentityObject;
                    if (id != null)
                    {
                        bIsWellKnown = id.IsWellKnown();
                    }
                }

                if ((null == ctorMsg) || bIsWellKnown)
                {
                    // This is also used to short-circuit the activation path
                    // when we have a well known proxy that has already been
                    // initialized (there's a race condition if we don't do this).
                    //

                    // This is a special case, where we have a remoting proxy
                    // but the constructormessage hasn't been setup.
                    // so let us just bail out..
                    // this is currently used by ServicedComponent's for cross appdomain
                    // pooling: <EMAIL>Microsoft</EMAIL>
                    //
                    ctorMsg = new ConstructorCallMessage(null, null, null, (RuntimeType)GetProxiedType());
                    // Set the constructor frame info in the CCM
                    ctorMsg.SetFrame(msgData);
                    reqMsg = ctorMsg;

                    // If this was the default ctor, check that default .ctor was called.
                    if (bIsWellKnown)
                    {
                        Contract.Assert(rp != null, "RemotingProxy expected here!");
                        // Clear any cached ctorMsg on the RemotingProxy
                        rp.ConstructorMessage = null;

                        // We did execute a Connect. Throw if the client
                        // code is also trying to use a non-default constructor at
                        // the same time.
                        if (ctorMsg.ArgCount != 0)
                        {
                            throw new RemotingException(
                                      Environment.GetResourceString(
                                          "Remoting_Activation_WellKnownCTOR"));
                        }
                    }

                    // Create a constructor return message
                    retMsg =
                        new ConstructorReturnMessage((MarshalByRefObject)GetTransparentProxy(),
                                                     null,
                                                     0,
                                                     null,
                                                     ctorMsg);
                }
                else
                {
                    // Set the constructor frame info in the CCM
                    ctorMsg.SetFrame(msgData);
                    reqMsg = ctorMsg;
                }
            }
            else
            {
                Contract.Assert(false, "Unknown call type");
            }

            // Make sure that outgoing remote calls are counted.
            ChannelServices.IncrementRemoteCalls();

            // For non-remoting proxies, EndAsync should not call Invoke()
            // because the proxy cannot support Async and the call has already
            // finished executing in BeginAsync
            if (!IsRemotingProxy() &&
                ((msgFlags & Message.EndAsync) == Message.EndAsync))
            {
                Message msg = reqMsg as Message;
                retMsg = EndInvokeHelper(msg, true);
                Contract.Assert(null != retMsg, "null != retMsg");
            }

            // Invoke
            Contract.Assert(null != reqMsg, "null != reqMsg");
            if (null == retMsg)
            {
                // NOTE: there are cases where we setup a return message
                // and we don't want the activation call to go through
                // refer to the note above for ServicedComponents and Cross Appdomain
                // pooling

                LogicalCallContext cctx          = null;
                Thread             currentThread = Thread.CurrentThread;
                // Pick up or clone the call context from the thread
                // and install it in the reqMsg as appropriate
                cctx = currentThread.GetMutableExecutionContext().LogicalCallContext;
                SetCallContextInMessage(reqMsg, msgFlags, cctx);

                // Add the outgoing "Header"'s to the message.
                cctx.PropagateOutgoingHeadersToMessage(reqMsg);
                retMsg = Invoke(reqMsg);

                // Get the call context returned and set it on the thread
                ReturnCallContextToThread(currentThread, retMsg, msgFlags, cctx);

                // Pull response "Header"'s out of the message
                Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext.PropagateIncomingHeadersToCallContext(retMsg);
            }

            if (!IsRemotingProxy() &&
                ((msgFlags & Message.BeginAsync) == Message.BeginAsync))
            {
                // This was a begin-async on a non-Remoting Proxy. For V-1 they
                // cannot support Async and end up doing a Sync call. We need
                // to fill up here to make the call look like async to
                // the caller.
                // Create the async result to return
                Message     msg = reqMsg as Message;
                AsyncResult ar  = new AsyncResult(msg);
                // Tell the async result that the call has actually completed
                // so it can hold on to the return message.
                ar.SyncProcessMessage(retMsg);
                // create a returnMessage to propagate just the asyncResult back
                // to the caller's stack.
                retMsg = new ReturnMessage(ar, null, 0, null /*cctx*/, msg);
            }

            // Propagate out parameters
            HandleReturnMessage(reqMsg, retMsg);

            // For constructor calls do some extra bookkeeping
            if (CallType.ConstructorCall == callType)
            {
                // NOTE: It is the responsiblity of the callee to propagate
                // the out parameters

                // Everything went well, we are ready to return
                // a proxy to the caller
                // Extract the return value
                MarshalByRefObject         retObj     = null;
                IConstructionReturnMessage ctorRetMsg = retMsg as IConstructionReturnMessage;
                if (null == ctorRetMsg)
                {
                    throw new RemotingException(
                              Environment.GetResourceString("Remoting_Proxy_BadReturnTypeForActivation"));
                }

                ConstructorReturnMessage crm = ctorRetMsg as ConstructorReturnMessage;
                if (null != crm)
                {
                    // If return message is of type ConstructorReturnMessage
                    // this is an in-appDomain activation. So no unmarshaling
                    // needed.

                    retObj = (MarshalByRefObject)crm.GetObject();
                    if (retObj == null)
                    {
                        throw new RemotingException(
                                  Environment.GetResourceString("Remoting_Activation_NullReturnValue"));
                    }
                }
                else
                {
                    // Fetch the objRef out of the returned message and unmarshal it
                    retObj = (MarshalByRefObject)RemotingServices.InternalUnmarshal(
                        (ObjRef)ctorRetMsg.ReturnValue,
                        GetTransparentProxy(),
                        true /*fRefine*/);

                    if (retObj == null)
                    {
                        throw new RemotingException(
                                  Environment.GetResourceString("Remoting_Activation_NullFromInternalUnmarshal"));
                    }
                }

                if (retObj != (MarshalByRefObject)GetTransparentProxy())
                {
                    throw new RemotingException(
                              Environment.GetResourceString(
                                  "Remoting_Activation_InconsistentState"));
                }

                if (IsRemotingProxy())
                {
                    // Clear any cached ctorMsg on the RemotingProxy
                    rp.ConstructorMessage = null;
                }
            }
        }
Beispiel #11
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static void PropagateOutParameters(IMessage msg,
                                                    Object[] outArgs,
                                                    Object returnValue)
        {
            // Check for method call
            Message m = msg as Message;

            // Check for constructor call
            if (null == m)
            {
                ConstructorCallMessage ccm = msg as ConstructorCallMessage;
                if (null != ccm)
                {
                    m = ccm.GetMessage();
                }
            }

            if (null == m)
            {
                throw new ArgumentException(
                          Environment.GetResourceString("Remoting_Proxy_ExpectedOriginalMessage"));
            }

            MethodBase mb = m.GetMethodBase();
            RemotingMethodCachedData cache =
                InternalRemotingServices.GetReflectionCachedData(mb);

            if (outArgs != null && outArgs.Length > 0)
            {
                Object[] args = m.Args; // original arguments

                // If a byref parameter is marked only with [In], we need to copy the
                //   original value from the request message into outargs, so that the
                //   value won't be bashed by CMessage::PropagateOutParameters below.
                ParameterInfo[] parameters = cache.Parameters;
                foreach (int index in cache.MarshalRequestArgMap)
                {
                    ParameterInfo param = parameters[index];
                    if (param.IsIn && param.ParameterType.IsByRef)
                    {
                        if (!param.IsOut)
                        {
                            outArgs[index] = args[index];
                        }
                    }
                }

                // copy non-byref arrays back into the same instance
                if (cache.NonRefOutArgMap.Length > 0)
                {
                    foreach (int index in cache.NonRefOutArgMap)
                    {
                        Array arg = args[index] as Array;
                        if (arg != null)
                        {
                            Array.Copy((Array)outArgs[index], arg, arg.Length);
                        }
                    }
                }

                // validate by-ref args (This must be done last)
                int[] outRefArgMap = cache.OutRefArgMap;
                if (outRefArgMap.Length > 0)
                {
                    foreach (int index in outRefArgMap)
                    {
                        ValidateReturnArg(outArgs[index], parameters[index].ParameterType);
                    }
                }
            }

            // validate return value
            //   (We don't validate Message.BeginAsync because the return value
            //    is always an IAsyncResult and the method base is the one that
            //    represents the underlying synchronous method).
            int callType = m.GetCallType();

            if ((callType & Message.CallMask) != Message.BeginAsync)
            {
                Type returnType = cache.ReturnType;
                if (returnType != null)
                {
                    ValidateReturnArg(returnValue, returnType);
                }
            }

            m.PropagateOutParameters(outArgs, returnValue);
        } // PropagateOutParameters
Beispiel #12
0
        // Private method invoked by the transparent proxy
        private void PrivateInvoke(ref MessageData msgData, int type)
        {
            IMessage reqMsg   = null;
            CallType callType = (CallType)type;
            IMessage retMsg   = null;
            int      msgFlags = -1;

            // Create a message object based on the type of call
            if (CallType.MethodCall == callType)
            {
                Message msg = new Message();
                msg.InitFields(msgData);
                reqMsg   = msg;
                msgFlags = msg.GetCallType();
            }
            else if (CallType.ConstructorCall == (CallType)callType)
            {
                // We use msgFlags to handle CallContext around
                // the virtual call to Invoke()
                msgFlags = Message.Sync;

                ConstructorCallMessage ctorMsg = null;
                RemotingProxy          rp      = this as RemotingProxy;
                if (null == rp)
                {
                    // Create a new constructor call message
                    ctorMsg = new ConstructorCallMessage(null, null, null, GetProxiedType());
                }
                else
                {
                    // Extract the constructor message set in the first step of activation.
                    ctorMsg = rp.ConstructorMessage;
                }

                // If the proxy is a wellknown client proxy, we don't need to run the c'tor.
                bool bIsWellKnown = false;
                if (rp != null)
                {
                    Identity id = rp.IdentityObject;
                    if (id != null)
                    {
                        bIsWellKnown = id.IsWellKnown();
                    }
                }

                if ((null == ctorMsg) || bIsWellKnown)
                {
                    // This is also used to short-circuit the activation path
                    // when we have a well known proxy that has already been
                    // initialized (there's a race condition if we don't do this).
                    //

                    // This is a special case, where we have a remoting proxy
                    // but the constructormessage hasn't been setup.
                    // so let us just bail out..
                    // this is currently used by ServicedComponent's for cross appdomain
                    // pooling:
                    //

                    /*
                     * ProxyAttribute pa = null;
                     * BCLDebug.Assert(((pa = ActivationServices.GetProxyAttribute(GetProxiedType()) is Object) || true);
                     * BCLDebug.Assert(pa != null && pa.GetType() != typeof(ProxyAttribute));
                     */
                    ctorMsg = new ConstructorCallMessage(null, null, null, GetProxiedType());
                    // Set the constructor frame info in the CCM
                    ctorMsg.SetFrame(msgData);
                    reqMsg = ctorMsg;

                    // If this was the default ctor, check that default .ctor was called.
                    if (bIsWellKnown)
                    {
                        // We did execute a Connect. Throw if the client
                        // code is also trying to use a non-default constructor at
                        // the same time.
                        if (ctorMsg.ArgCount != 0)
                        {
                            throw new RemotingException(
                                      Environment.GetResourceString(
                                          "Remoting_Activation_WellKnownCTOR"));
                        }
                    }

                    // Create a constructor return message
                    retMsg =
                        new ConstructorReturnMessage((MarshalByRefObject)GetTransparentProxy(),
                                                     null,
                                                     0,
                                                     null,
                                                     ctorMsg);
                }
                else
                {
                    // Set the constructor frame info in the CCM
                    ctorMsg.SetFrame(msgData);
                    reqMsg = ctorMsg;
                }
            }
            else
            {
                BCLDebug.Assert(false, "Unknown call type");
            }

            // Make sure that outgoing remote calls are counted.
            ChannelServices.IncrementRemoteCalls();


            // Invoke
            BCLDebug.Assert(null != reqMsg, "null != reqMsg");
            if (null == retMsg)
            {
                // NOTE: there are cases where we setup a return message
                // and we don't want the activation call to go through
                // refer to the note above for ServicedComponents and Cross Appdomain
                // pooling

                LogicalCallContext cctx          = null;
                Thread             currentThread = Thread.CurrentThread;
                // Pick up or clone the call context from the thread
                // and install it in the reqMsg as appropriate
                cctx = currentThread.GetLogicalCallContext();
                SetCallContextInMessage(reqMsg, msgFlags, cctx);

                // Add the outgoing "Header"'s to the message.
                cctx.PropagateOutgoingHeadersToMessage(reqMsg);

                retMsg = Invoke(reqMsg);

                // Get the call context returned and set it on the thread
                ReturnCallContextToThread(currentThread, retMsg, msgFlags);

                // Pull response "Header"'s out of the message
                CallContext.GetLogicalCallContext().PropagateIncomingHeadersToCallContext(retMsg);
            }

            // Propagate out parameters
            HandleReturnMessage(reqMsg, retMsg);

            // For constructor calls do some extra bookkeeping
            if (CallType.ConstructorCall == callType)
            {
                // NOTE: It is the responsiblity of the callee to propagate
                // the out parameters

                // Everything went well, we are ready to return
                // a proxy to the caller
                // Extract the return value
                MarshalByRefObject         retObj     = null;
                IConstructionReturnMessage ctorRetMsg = retMsg as IConstructionReturnMessage;
                if (null == ctorRetMsg)
                {
                    throw new RemotingException(
                              Environment.GetResourceString("Remoting_Proxy_BadReturnTypeForActivation"));
                }

                ConstructorReturnMessage crm = ctorRetMsg as ConstructorReturnMessage;
                if (null != crm)
                {
                    retObj = (MarshalByRefObject)crm.GetObject();
                    if (retObj == null)
                    {
                        throw new RemotingException(
                                  Environment.GetResourceString("Remoting_Activation_NullReturnValue"));
                    }
                }
                else
                {
                    // Fetch the objRef out of the returned message and unmarshal it
                    retObj = (MarshalByRefObject)RemotingServices.InternalUnmarshal(
                        (ObjRef)ctorRetMsg.ReturnValue,
                        GetTransparentProxy(),
                        true /*fRefine*/);

                    if (retObj == null)
                    {
                        throw new RemotingException(
                                  Environment.GetResourceString("Remoting_Activation_NullFromInternalUnmarshal"));
                    }
                }

                if (retObj != (MarshalByRefObject)GetTransparentProxy())
                {
                    throw new RemotingException(
                              Environment.GetResourceString(
                                  "Remoting_Activation_InconsistentState"));
                }
            }
        }
Beispiel #13
0
        private void PrivateInvoke(ref MessageData msgData, int type)
        {
            IMessage      message       = null;
            IMessage      message2      = null;
            int           num           = -1;
            RemotingProxy remotingProxy = null;

            if (1 == type)
            {
                Message message3 = new Message();
                message3.InitFields(msgData);
                message = message3;
                num     = message3.GetCallType();
            }
            else if (2 == type)
            {
                num           = 0;
                remotingProxy = (this as RemotingProxy);
                bool flag = false;
                ConstructorCallMessage constructorCallMessage;
                if (!this.IsRemotingProxy())
                {
                    constructorCallMessage = new ConstructorCallMessage(null, null, null, (RuntimeType)this.GetProxiedType());
                }
                else
                {
                    constructorCallMessage = remotingProxy.ConstructorMessage;
                    Identity identityObject = remotingProxy.IdentityObject;
                    if (identityObject != null)
                    {
                        flag = identityObject.IsWellKnown();
                    }
                }
                if (constructorCallMessage == null || flag)
                {
                    constructorCallMessage = new ConstructorCallMessage(null, null, null, (RuntimeType)this.GetProxiedType());
                    constructorCallMessage.SetFrame(msgData);
                    message = constructorCallMessage;
                    if (flag)
                    {
                        remotingProxy.ConstructorMessage = null;
                        if (constructorCallMessage.ArgCount != 0)
                        {
                            throw new RemotingException(Environment.GetResourceString("Remoting_Activation_WellKnownCTOR"));
                        }
                    }
                    message2 = new ConstructorReturnMessage((MarshalByRefObject)this.GetTransparentProxy(), null, 0, null, constructorCallMessage);
                }
                else
                {
                    constructorCallMessage.SetFrame(msgData);
                    message = constructorCallMessage;
                }
            }
            ChannelServices.IncrementRemoteCalls();
            if (!this.IsRemotingProxy() && (num & 2) == 2)
            {
                Message reqMsg = message as Message;
                message2 = RealProxy.EndInvokeHelper(reqMsg, true);
            }
            if (message2 == null)
            {
                Thread             currentThread      = Thread.CurrentThread;
                LogicalCallContext logicalCallContext = currentThread.GetMutableExecutionContext().LogicalCallContext;
                this.SetCallContextInMessage(message, num, logicalCallContext);
                logicalCallContext.PropagateOutgoingHeadersToMessage(message);
                message2 = this.Invoke(message);
                this.ReturnCallContextToThread(currentThread, message2, num, logicalCallContext);
                Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext.PropagateIncomingHeadersToCallContext(message2);
            }
            if (!this.IsRemotingProxy() && (num & 1) == 1)
            {
                Message     message4    = message as Message;
                AsyncResult asyncResult = new AsyncResult(message4);
                asyncResult.SyncProcessMessage(message2);
                message2 = new ReturnMessage(asyncResult, null, 0, null, message4);
            }
            RealProxy.HandleReturnMessage(message, message2);
            if (2 == type)
            {
                IConstructionReturnMessage constructionReturnMessage = message2 as IConstructionReturnMessage;
                if (constructionReturnMessage == null)
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_BadReturnTypeForActivation"));
                }
                ConstructorReturnMessage constructorReturnMessage = constructionReturnMessage as ConstructorReturnMessage;
                MarshalByRefObject       marshalByRefObject;
                if (constructorReturnMessage != null)
                {
                    marshalByRefObject = (MarshalByRefObject)constructorReturnMessage.GetObject();
                    if (marshalByRefObject == null)
                    {
                        throw new RemotingException(Environment.GetResourceString("Remoting_Activation_NullReturnValue"));
                    }
                }
                else
                {
                    marshalByRefObject = (MarshalByRefObject)RemotingServices.InternalUnmarshal((ObjRef)constructionReturnMessage.ReturnValue, this.GetTransparentProxy(), true);
                    if (marshalByRefObject == null)
                    {
                        throw new RemotingException(Environment.GetResourceString("Remoting_Activation_NullFromInternalUnmarshal"));
                    }
                }
                if (marshalByRefObject != (MarshalByRefObject)this.GetTransparentProxy())
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Activation_InconsistentState"));
                }
                if (this.IsRemotingProxy())
                {
                    remotingProxy.ConstructorMessage = null;
                }
            }
        }