public static IConstructionReturnMessage CreateConstructionReturnMessage(IConstructionCallMessage ctorMsg, MarshalByRefObject retObj) { IConstructionReturnMessage ctorRetMsg = null; // Create the return message ctorRetMsg = new ConstructorReturnMessage(retObj, null, 0, null, ctorMsg); // NOTE: WE ALLOW ONLY DEFAULT CTORs on SERVICEDCOMPONENTS return ctorRetMsg; }
public IConstructionReturnMessage InitializeServerObject(IConstructionCallMessage ctorMsg) { IConstructionReturnMessage retMsg = null; if (_serverObject == null) { Type svrType = GetProxiedType(); if((ctorMsg != null) && (ctorMsg.ActivationType != svrType)) { throw new RemotingException( String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Proxy_BadTypeForActivation"), svrType.FullName, ctorMsg.ActivationType)); } // Create a blank object _serverObject = RemotingServices.AllocateUninitializedObject(svrType); // If the stub is the default stub, then set the server context // to be the current context. SetContextForDefaultStub(); // OK... we are all set to run the constructor call on the uninitialized object MarshalByRefObject proxy = (MarshalByRefObject)GetTransparentProxy(); IMethodReturnMessage msg = null; Exception e = null; if(null != ctorMsg) { msg = RemotingServices.ExecuteMessage(proxy, ctorMsg); e = msg.Exception; } else { try { RemotingServices.CallDefaultCtor(proxy); } catch(Exception excep) { e = excep; } } // Construct a return message if(null == e) { Object[] outArgs = (msg == null ? null : msg.OutArgs); int outLength = (null == outArgs ? 0 : outArgs.Length); LogicalCallContext callCtx = (msg == null ? null : msg.LogicalCallContext); retMsg = new ConstructorReturnMessage(proxy, outArgs, outLength, callCtx, ctorMsg); // setup identity SetupIdentity(); if (IsRemotingProxy()) { ((RemotingProxy) this).Initialized = true; } } else { // Exception occurred retMsg = new ConstructorReturnMessage(e, ctorMsg); } } return retMsg; }
[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; } } }
[System.Security.SecurityCritical] // auto-generated internal static IConstructionReturnMessage SetupConstructionReply( Object serverObj, IConstructionCallMessage ctorMsg, Exception e) { IConstructionReturnMessage replyMsg = null; if (e == null) { replyMsg = new ConstructorReturnMessage( (MarshalByRefObject)serverObj, null, // < // if ctor-s with ref/out are supported</ 0, (LogicalCallContext) ctorMsg.Properties[Message.CallContextKey], ctorMsg); } else { replyMsg = new ConstructorReturnMessage(e,null); // We have created our own message ... transfer the callcontext // from the request message. ((ConstructorReturnMessage)replyMsg).SetLogicalCallContext( (LogicalCallContext) ctorMsg.Properties[Message.CallContextKey]); } return replyMsg; }
// This function is called by ActivationServices in case // the activation needs to be within the same appdomain. These // are only for ContextBound types. // It is also called to do satisfy remote incoming requests from // the activation services. These could be for both ContextBound // and MarshalByRef types. internal static IConstructionReturnMessage DoCrossContextActivation( IConstructionCallMessage reqMsg) { bool bCtxBound = reqMsg.ActivationType.IsContextful; ContextTransitionFrame frame = new ContextTransitionFrame(); if (bCtxBound) { // If the type is context bound, we need to create // the appropriate context and activate the object inside // it. // Create a new Context Context serverContext = new Context(); ArrayList list = (ArrayList) reqMsg.ContextProperties; Assembly asm = null; for (int i=0; i<list.Count; i++) { IContextProperty prop = list[i] as IContextProperty; if (null == prop) { throw new RemotingException( Environment.GetResourceString( "Remoting_Activation_BadAttribute")); } asm = prop.GetType().Assembly; // Make a security check to ensure that the context property // is from a trusted assembly! CheckForInfrastructurePermission(asm); // This ensures that we don't try to add duplicate // attributes (eg. type attributes common on both client // and server end) if (serverContext.GetProperty(prop.Name) == null) { serverContext.SetProperty(prop); } } // No more property changes to the server context from here. serverContext.Freeze(); // (This seems like an overkill but that is how it is spec-ed) // Ask each of the properties in the context we formed from // if it is happy with the current context. for (int i=0; i<list.Count;i++) { if (!((IContextProperty)list[i]).IsNewContextOK( serverContext)) { throw new RemotingException( Environment.GetResourceString( "Remoting_Activation_PropertyUnhappy")); } } // Change to server context Thread.CurrentThread.EnterContext(serverContext, ref frame); } // call the first sink in the server context chain IMethodReturnMessage retMsg = (IMethodReturnMessage) Thread.CurrentContext.GetServerContextChain().SyncProcessMessage(reqMsg); // The return message may not be of type // IConstructionReturnMessage if an exception happens // in the sink chains. Exception e = null; IConstructionReturnMessage replyMsg = retMsg as IConstructionReturnMessage; if (null == replyMsg) { if (retMsg != null) { e = retMsg.Exception; } else { e = new RemotingException( Environment.GetResourceString( "Remoting_Activation_Failed")); } replyMsg = new ConstructorReturnMessage(e,null); // We have created our own message ... transfer the callcontext // from the request message. ((ConstructorReturnMessage)replyMsg).SetLogicalCallContext( (LogicalCallContext) reqMsg.Properties[Message.CallContextKey]); } if (bCtxBound) { Thread.CurrentThread.ReturnToContext(ref frame); } return replyMsg; }
[System.Security.SecurityCritical] // auto-generated internal static Object DoCrossContextActivationCallback(Object[] args) { IConstructionCallMessage reqMsg = (IConstructionCallMessage) args[0]; IConstructionReturnMessage replyMsg = null; // call the first sink in the server context chain // < IMethodReturnMessage retMsg = (IMethodReturnMessage) Thread.CurrentContext.GetServerContextChain().SyncProcessMessage(reqMsg); // The return message may not be of type // IConstructionReturnMessage if an exception happens // in the sink chains. Exception e = null; replyMsg = retMsg as IConstructionReturnMessage; if (null == replyMsg) { if (retMsg != null) { e = retMsg.Exception; } else { e = new RemotingException( Environment.GetResourceString( "Remoting_Activation_Failed")); } replyMsg = new ConstructorReturnMessage(e,null); // We have created our own message ... transfer the callcontext // from the request message. ((ConstructorReturnMessage)replyMsg).SetLogicalCallContext( (LogicalCallContext) reqMsg.Properties[Message.CallContextKey]); } return replyMsg; }
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; } } }
public IConstructionReturnMessage InitializeServerObject(IConstructionCallMessage ctorMsg) { IConstructionReturnMessage message = null; if (this._serverObject != null) { return message; } Type proxiedType = this.GetProxiedType(); if ((ctorMsg != null) && (ctorMsg.ActivationType != proxiedType)) { throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Proxy_BadTypeForActivation"), new object[] { proxiedType.FullName, ctorMsg.ActivationType })); } this._serverObject = RemotingServices.AllocateUninitializedObject(proxiedType); this.SetContextForDefaultStub(); MarshalByRefObject transparentProxy = (MarshalByRefObject) this.GetTransparentProxy(); IMethodReturnMessage message2 = null; Exception e = null; if (ctorMsg != null) { message2 = RemotingServices.ExecuteMessage(transparentProxy, ctorMsg); e = message2.Exception; } else { try { RemotingServices.CallDefaultCtor(transparentProxy); } catch (Exception exception2) { e = exception2; } } if (e == null) { object[] outArgs = (message2 == null) ? null : message2.OutArgs; int outArgsCount = (outArgs == null) ? 0 : outArgs.Length; LogicalCallContext callCtx = (message2 == null) ? null : message2.LogicalCallContext; message = new ConstructorReturnMessage(transparentProxy, outArgs, outArgsCount, callCtx, ctorMsg); this.SetupIdentity(); if (this.IsRemotingProxy()) { ((RemotingProxy) this).Initialized = true; } return message; } return new ConstructorReturnMessage(e, ctorMsg); }
internal static IConstructionReturnMessage SetupConstructionReply(object serverObj, IConstructionCallMessage ctorMsg, Exception e) { IConstructionReturnMessage message = null; if (e == null) { return new ConstructorReturnMessage((MarshalByRefObject) serverObj, null, 0, (LogicalCallContext) ctorMsg.Properties[Message.CallContextKey], ctorMsg); } message = new ConstructorReturnMessage(e, null); ((ConstructorReturnMessage) message).SetLogicalCallContext((LogicalCallContext) ctorMsg.Properties[Message.CallContextKey]); return message; }
internal static object DoCrossContextActivationCallback(object[] args) { IConstructionCallMessage msg = (IConstructionCallMessage) args[0]; IConstructionReturnMessage message2 = null; IMethodReturnMessage message3 = (IMethodReturnMessage) Thread.CurrentContext.GetServerContextChain().SyncProcessMessage(msg); Exception e = null; message2 = message3 as IConstructionReturnMessage; if (message2 == null) { if (message3 != null) { e = message3.Exception; } else { e = new RemotingException(Environment.GetResourceString("Remoting_Activation_Failed")); } message2 = new ConstructorReturnMessage(e, null); ((ConstructorReturnMessage) message2).SetLogicalCallContext((LogicalCallContext) msg.Properties[Message.CallContextKey]); } return message2; }
// 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")); } } }