2 // System.Runtime.Remoting.ActivationServices.cs
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
6 // (C) 2002, Lluis Sanchez Gual
10 using System.Runtime.Remoting.Messaging;
\r
11 using System.Runtime.Remoting.Activation;
\r
12 using System.Runtime.Remoting.Contexts;
\r
13 using System.Reflection;
\r
14 using System.Runtime.CompilerServices;
15 using System.Collections;
16 using System.Runtime.Remoting.Channels;
18 namespace System.Runtime.Remoting.Activation
\r
20 internal class ActivationServices
\r
22 static IActivator _constructionActivator = new ConstructionLevelActivator ();
\r
24 public static object CreateProxyFromAttributes (Type type, object[] activationAttributes)
\r
26 string activationUrl = null;
\r
27 foreach (object attr in activationAttributes)
29 if (!(attr is IContextAttribute)) throw new RemotingException ("Activation attribute does not implement the IContextAttribute interface");
30 if (attr is UrlAttribute) activationUrl = ((UrlAttribute)attr).UrlValue;
33 if (activationUrl != null)
34 return RemotingServices.CreateClientProxy (type, activationUrl, activationAttributes);
36 ActivatedClientTypeEntry activatedEntry = RemotingConfiguration.IsRemotelyActivatedClientType (type);
37 if (activatedEntry != null)
38 return RemotingServices.CreateClientProxy (activatedEntry, activationAttributes);
40 if (type.IsContextful)
41 return RemotingServices.CreateClientProxyForContextBound (type, activationAttributes);
46 public static ConstructionCall CreateConstructionCall (Type type, string activationUrl, object[] activationAttributes)
48 ConstructionCall ctorCall = new ConstructionCall (type);
49 ctorCall.Activator = _constructionActivator;
51 if (!type.IsContextful) return ctorCall;
53 ArrayList attributes = new ArrayList ();
54 if (activationAttributes != null) attributes.AddRange (activationAttributes);
56 bool isContextOk = (activationUrl == ChannelServices.CrossContextUrl); // Remote CBOs are always created in a new context
57 Context currentContext = Threading.Thread.CurrentContext;
61 foreach (IContextAttribute attr in attributes)
\r
63 if (!attr.IsContextOK (currentContext, ctorCall))
\r
71 object[] typeAttributes = type.GetCustomAttributes (true);
72 foreach (object attr in typeAttributes)
\r
74 if (attr is IContextAttribute)
\r
76 isContextOk = isContextOk && ((IContextAttribute)attr).IsContextOK (currentContext, ctorCall);
\r
77 attributes.Add (attr);
83 // A new context is needed. Collect the context properties and set
84 // the context level activator.
86 ctorCall.SetActivationAttributes (attributes.ToArray());
87 ctorCall.Activator = new ContextLevelActivator (ctorCall.Activator);
89 foreach (IContextAttribute attr in attributes)
90 attr.GetPropertiesForNewContext (ctorCall);
96 public static IMessage CreateInstanceFromMessage (IConstructionCallMessage ctorCall)
\r
98 object obj = AllocateUninitializedClassInstance (ctorCall.ActivationType);
\r
99 ctorCall.MethodBase.Invoke (obj, ctorCall.Args);
\r
101 ServerIdentity identity = (ServerIdentity) RemotingServices.GetMessageTargetIdentity (ctorCall);
103 identity.AttachServerObject ((MarshalByRefObject) obj, Threading.Thread.CurrentContext);
105 return new ConstructionResponse (obj, null, ctorCall);
108 public static object CreateProxyForType (Type type)
110 // Called by the runtime when creating an instance of a type
111 // that has been registered as remotely activated.
113 // First of all check for remote activation. If the object is not remote, then
114 // it may be contextbound.
116 ActivatedClientTypeEntry activatedEntry = RemotingConfiguration.IsRemotelyActivatedClientType (type);
117 if (activatedEntry != null)
118 return RemotingServices.CreateClientProxy (activatedEntry, null);
120 WellKnownClientTypeEntry wellknownEntry = RemotingConfiguration.IsWellKnownClientType (type);
121 if (wellknownEntry != null)
122 return RemotingServices.CreateClientProxy (wellknownEntry);
124 if (type.IsContextful)
125 return RemotingServices.CreateClientProxyForContextBound (type, null);
130 // Allocates an uninitialized instance. It never creates proxies.
\r
131 [MethodImplAttribute(MethodImplOptions.InternalCall)]
132 private static extern object AllocateUninitializedClassInstance (Type type);
134 [MethodImplAttribute(MethodImplOptions.InternalCall)]
135 public extern static void EnableProxyActivation (Type type, bool enable);