2 // System.Runtime.Remoting.ActivationServices.cs
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
6 // (C) 2002, Lluis Sanchez Gual
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Threading;
34 using System.Runtime.Remoting.Messaging;
35 using System.Runtime.Remoting.Activation;
36 using System.Runtime.Remoting.Contexts;
37 using System.Runtime.Remoting.Proxies;
38 using System.Reflection;
39 using System.Runtime.CompilerServices;
40 using System.Collections;
41 using System.Runtime.Remoting.Channels;
43 namespace System.Runtime.Remoting.Activation
45 internal class ActivationServices
47 static IActivator _constructionActivator = new ConstructionLevelActivator ();
49 public static IMessage Activate (RemotingProxy proxy, ConstructionCall ctorCall)
53 if (Thread.CurrentContext.HasExitSinks && !ctorCall.IsContextOk)
54 response = Thread.CurrentContext.GetClientContextSinkChain ().SyncProcessMessage (ctorCall);
56 response = RemoteActivate (ctorCall);
58 if (response is IConstructionReturnMessage && ((IConstructionReturnMessage)response).Exception == null)
60 Identity identity = RemotingServices.GetMessageTargetIdentity (ctorCall);
61 proxy.AttachIdentity (identity);
67 public static IMessage RemoteActivate (IConstructionCallMessage ctorCall)
71 return ctorCall.Activator.Activate (ctorCall);
73 catch (Exception ex)
\r
75 return new ReturnMessage (ex, ctorCall);
79 public static object CreateProxyFromAttributes (Type type, object[] activationAttributes)
81 string activationUrl = null;
82 foreach (object attr in activationAttributes)
84 if (!(attr is IContextAttribute)) throw new RemotingException ("Activation attribute does not implement the IContextAttribute interface");
85 if (attr is UrlAttribute) activationUrl = ((UrlAttribute)attr).UrlValue;
88 if (activationUrl != null)
89 return RemotingServices.CreateClientProxy (type, activationUrl, activationAttributes);
91 ActivatedClientTypeEntry activatedEntry = RemotingConfiguration.IsRemotelyActivatedClientType (type);
92 if (activatedEntry != null)
93 return RemotingServices.CreateClientProxy (activatedEntry, activationAttributes);
95 if (type.IsContextful)
96 return RemotingServices.CreateClientProxyForContextBound (type, activationAttributes);
101 public static ConstructionCall CreateConstructionCall (Type type, string activationUrl, object[] activationAttributes)
103 ConstructionCall ctorCall = new ConstructionCall (type);
105 if (!type.IsContextful)
107 // Must be a remote activated object
108 ctorCall.Activator = new AppDomainLevelActivator (activationUrl, _constructionActivator);
109 ctorCall.IsContextOk = false; // It'll be activated in a remote context
113 // It is a CBO. Need collect context properties and
114 // check if a new context is needed.
116 IActivator activatorChain = _constructionActivator;
117 activatorChain = new ContextLevelActivator (activatorChain);
119 ArrayList attributes = new ArrayList ();
120 if (activationAttributes != null) attributes.AddRange (activationAttributes);
122 bool isContextOk = (activationUrl == ChannelServices.CrossContextUrl); // Remote CBOs are always created in a new context
123 Context currentContext = Threading.Thread.CurrentContext;
127 foreach (IContextAttribute attr in attributes)
129 if (!attr.IsContextOK (currentContext, ctorCall))
137 object[] typeAttributes = type.GetCustomAttributes (true);
138 foreach (object attr in typeAttributes)
140 if (attr is IContextAttribute)
142 isContextOk = isContextOk && ((IContextAttribute)attr).IsContextOK (currentContext, ctorCall);
143 attributes.Add (attr);
149 // A new context is needed. Collect the context properties and chain
150 // the context level activator.
152 ctorCall.SetActivationAttributes (attributes.ToArray());
154 foreach (IContextAttribute attr in attributes)
155 attr.GetPropertiesForNewContext (ctorCall);
158 if (activationUrl != ChannelServices.CrossContextUrl)
159 activatorChain = new AppDomainLevelActivator (activationUrl, activatorChain);
161 ctorCall.Activator = activatorChain;
162 ctorCall.IsContextOk = isContextOk;
167 public static IMessage CreateInstanceFromMessage (IConstructionCallMessage ctorCall)
169 object obj = AllocateUninitializedClassInstance (ctorCall.ActivationType);
170 ctorCall.MethodBase.Invoke (obj, ctorCall.Args);
172 ServerIdentity identity = (ServerIdentity) RemotingServices.GetMessageTargetIdentity (ctorCall);
174 identity.AttachServerObject ((MarshalByRefObject) obj, Threading.Thread.CurrentContext);
176 return new ConstructionResponse (obj, null, ctorCall);
179 public static object CreateProxyForType (Type type)
181 // Called by the runtime when creating an instance of a type
182 // that has been registered as remotely activated.
184 // First of all check for remote activation. If the object is not remote, then
185 // it may be contextbound.
187 ActivatedClientTypeEntry activatedEntry = RemotingConfiguration.IsRemotelyActivatedClientType (type);
188 if (activatedEntry != null)
189 return RemotingServices.CreateClientProxy (activatedEntry, null);
191 WellKnownClientTypeEntry wellknownEntry = RemotingConfiguration.IsWellKnownClientType (type);
192 if (wellknownEntry != null)
193 return RemotingServices.CreateClientProxy (wellknownEntry);
195 if (type.IsContextful)
196 return RemotingServices.CreateClientProxyForContextBound (type, null);
201 // Allocates an uninitialized instance. It never creates proxies.
202 [MethodImplAttribute(MethodImplOptions.InternalCall)]
203 public static extern object AllocateUninitializedClassInstance (Type type);
205 [MethodImplAttribute(MethodImplOptions.InternalCall)]
206 public extern static void EnableProxyActivation (Type type, bool enable);