File: RemotingServices.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (918 lines) | stat: -rw-r--r-- 29,267 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
//
// System.Runtime.Remoting.RemotingServices.cs
//
// Authors:
//   Dietmar Maurer (dietmar@ximian.com)
//   Lluis Sanchez Gual (lluis@ideary.com)
//   Patrik Torstensson
//
// (C) 2001 Ximian, Inc.  http://www.ximian.com
// Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Diagnostics;
using System.Text;
using System.Reflection;
using System.Threading;
using System.Collections;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Remoting.Services;
using System.Security.Permissions;
using System.Runtime.ConstrainedExecution;
using System.Runtime.Serialization.Formatters;

namespace System.Runtime.Remoting
{
	[System.Runtime.InteropServices.ComVisible (true)]
	static
	public class RemotingServices 
	{
		// Holds the identities of the objects, using uri as index
		static Hashtable uri_hash = new Hashtable ();		

		static BinaryFormatter _serializationFormatter;
		static BinaryFormatter _deserializationFormatter;
		
		static string app_id;
		static readonly object app_id_lock = new object ();
		
		static int next_id = 1;
		const BindingFlags methodBindings = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
		static readonly MethodInfo FieldSetterMethod;
		static readonly MethodInfo FieldGetterMethod;
		
		// Holds information in xdomain calls. Names are short to minimize serialized size.
		[Serializable]
		class CACD {
			public object d;	/* call data */
			public object c;	/* call context */
		}
		
		static RemotingServices ()
		{
			RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
			StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
			_serializationFormatter = new BinaryFormatter (surrogateSelector, context);
			_deserializationFormatter = new BinaryFormatter (null, context);
			_serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Full;
			_deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Full;
			
			RegisterInternalChannels ();
			CreateWellKnownServerIdentity (typeof(RemoteActivator), "RemoteActivationService.rem", WellKnownObjectMode.Singleton);
			
			FieldSetterMethod = typeof(object).GetMethod ("FieldSetter", BindingFlags.NonPublic|BindingFlags.Instance);
			FieldGetterMethod = typeof(object).GetMethod ("FieldGetter", BindingFlags.NonPublic|BindingFlags.Instance);
		}

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		internal extern static object InternalExecute (MethodBase method, Object obj,
							       Object[] parameters, out object [] out_args);

		// Returns the actual implementation of @method in @type.
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		internal extern static MethodBase GetVirtualMethod (Type type, MethodBase method);

#if !FEATURE_REMOTING
		public static bool IsTransparentProxy (object proxy)
		{
			throw new NotSupportedException ();
		}
#else
		[ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		public extern static bool IsTransparentProxy (object proxy);

		internal static bool ProxyCheckCast (RealProxy rp, RuntimeType castType)
		{
			// TODO: What should it do?
			return true;
		}
#endif

		internal static IMethodReturnMessage InternalExecuteMessage (
		        MarshalByRefObject target, IMethodCallMessage reqMsg)
		{
			ReturnMessage result;
			
			Type tt = target.GetType ();
			MethodBase method;
			if (reqMsg.MethodBase.DeclaringType == tt ||
			    reqMsg.MethodBase == FieldSetterMethod || 
			    reqMsg.MethodBase == FieldGetterMethod) {
				method = reqMsg.MethodBase;
			} else {
				method = GetVirtualMethod (tt, reqMsg.MethodBase);

				if (method == null)
					throw new RemotingException (
						String.Format ("Cannot resolve method {0}:{1}", tt, reqMsg.MethodName));
			}

			if (reqMsg.MethodBase.IsGenericMethod) {
				Type[] genericArguments = reqMsg.MethodBase.GetGenericArguments ();
				MethodInfo gmd = ((MethodInfo)method).GetGenericMethodDefinition ();
				method = gmd.MakeGenericMethod (genericArguments);
			}

			var oldContext = CallContext.SetLogicalCallContext (reqMsg.LogicalCallContext);
			
			try 
			{
				object [] out_args;
				object rval = InternalExecute (method, target, reqMsg.Args, out out_args);
			
				// Collect parameters with Out flag from the request message
				// FIXME: This can be done in the unmanaged side and will be
				// more efficient
				
				ParameterInfo[] parameters = method.GetParameters();
				object[] returnArgs = new object [parameters.Length];
				
				int n = 0;
				int noa = 0;
				foreach (ParameterInfo par in parameters)
				{
					if (par.IsOut && !par.ParameterType.IsByRef) 
						returnArgs [n++] = reqMsg.GetArg (par.Position);
					else if (par.ParameterType.IsByRef)
						returnArgs [n++] = out_args [noa++]; 
					else
						returnArgs [n++] = null; 
				}
				
				var latestCallContext = Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext;
				result = new ReturnMessage (rval, returnArgs, n, latestCallContext, reqMsg);
			} 
			catch (Exception e) 
			{
				result = new ReturnMessage (e, reqMsg);
			}
			
			CallContext.SetLogicalCallContext (oldContext);
			return result;
		}

		public static IMethodReturnMessage ExecuteMessage (
			MarshalByRefObject target, IMethodCallMessage reqMsg)
		{
			if (IsTransparentProxy(target))
			{
				// Message must go through all chain of sinks
				RealProxy rp = GetRealProxy (target);
				return (IMethodReturnMessage) rp.Invoke (reqMsg);
			}
			else	// Direct call
				return InternalExecuteMessage (target, reqMsg);
		}

		[System.Runtime.InteropServices.ComVisible (true)]
		public static object Connect (Type classToProxy, string url)
		{
			ObjRef objRef = new ObjRef (classToProxy, url, null);
			return GetRemoteObject (objRef, classToProxy);
		}

		[System.Runtime.InteropServices.ComVisible (true)]
		public static object Connect (Type classToProxy, string url, object data)
		{
			ObjRef objRef = new ObjRef (classToProxy, url, data);
			return GetRemoteObject (objRef, classToProxy);
		}

		public static bool Disconnect (MarshalByRefObject obj)
		{
			if (obj == null) throw new ArgumentNullException ("obj");

			ServerIdentity identity;

			if (IsTransparentProxy (obj))
			{
				// CBOs are always accessed through a proxy, even in the server, so
				// for server CBOs it is ok to disconnect a proxy

				RealProxy proxy = GetRealProxy(obj);
				if (proxy.GetProxiedType().IsContextful && (proxy.ObjectIdentity is ServerIdentity))
					identity = proxy.ObjectIdentity as ServerIdentity;
				else
					throw new ArgumentException ("The obj parameter is a proxy.");
			}
			else {
				identity = obj.ObjectIdentity;
				obj.ObjectIdentity = null;
			}

			if (identity == null || !identity.IsConnected)
				return false;
			else
			{
				LifetimeServices.StopTrackingLifetime (identity);
				DisposeIdentity (identity);
				TrackingServices.NotifyDisconnectedObject (obj);
				return true;
			}
		}

		public static Type GetServerTypeForUri (string URI)
		{
			ServerIdentity ident = GetIdentityForUri (URI) as ServerIdentity;
			if (ident == null) return null;
			return ident.ObjectType;
		}

		public static string GetObjectUri (MarshalByRefObject obj)
		{
			Identity ident = GetObjectIdentity(obj);
			if (ident is ClientIdentity) return ((ClientIdentity)ident).TargetUri;
			else if (ident != null) return ident.ObjectUri;
			else return null;
		}

		public static object Unmarshal (ObjRef objectRef)
		{
			return Unmarshal (objectRef, true);
		}

		public static object Unmarshal (ObjRef objectRef, bool fRefine)
		{
			Type classToProxy = fRefine ? objectRef.ServerType : typeof (MarshalByRefObject);
			if (classToProxy == null) classToProxy = typeof (MarshalByRefObject);

			if (objectRef.IsReferenceToWellKnow) {
				object obj = GetRemoteObject (objectRef, classToProxy);
				TrackingServices.NotifyUnmarshaledObject (obj, objectRef);
				return obj;
			}
			else
			{
				object obj;
				
				if (classToProxy.IsContextful) {
					// Look for a ProxyAttribute
					ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (classToProxy, typeof(ProxyAttribute), true);
					if (att != null) {
						obj = att.CreateProxy (objectRef, classToProxy, null, null).GetTransparentProxy();
						TrackingServices.NotifyUnmarshaledObject (obj, objectRef);
						return obj;
					}
				}
				obj = GetProxyForRemoteObject (objectRef, classToProxy);
				TrackingServices.NotifyUnmarshaledObject (obj, objectRef);
				return obj;
			}
		}

		public static ObjRef Marshal (MarshalByRefObject Obj)
		{
			return Marshal (Obj, null, null);
		}
		
		public static ObjRef Marshal (MarshalByRefObject Obj, string URI)
		{
			return Marshal (Obj, URI, null);
		}
		
		public static ObjRef Marshal (MarshalByRefObject Obj, string ObjURI, Type RequestedType)
		{
			if (IsTransparentProxy (Obj))
			{
				RealProxy proxy = RemotingServices.GetRealProxy (Obj);
				Identity identity = proxy.ObjectIdentity;

				if (identity != null)
				{
					if (proxy.GetProxiedType().IsContextful && !identity.IsConnected)
					{
						// Unregistered local contextbound object. Register now.
						ClientActivatedIdentity cboundIdentity = (ClientActivatedIdentity)identity;
						if (ObjURI == null) ObjURI = NewUri();
						cboundIdentity.ObjectUri = ObjURI;
						RegisterServerIdentity (cboundIdentity);
						cboundIdentity.StartTrackingLifetime ((ILease)Obj.InitializeLifetimeService());
						return cboundIdentity.CreateObjRef (RequestedType);
					}
					else if (ObjURI != null)
						throw new RemotingException ("It is not possible marshal a proxy of a remote object.");

					ObjRef or = proxy.ObjectIdentity.CreateObjRef (RequestedType);
					TrackingServices.NotifyMarshaledObject (Obj, or);
					return or;
				}
			}

			if (RequestedType == null) RequestedType = Obj.GetType ();

			if (ObjURI == null) 
			{
				if (Obj.ObjectIdentity == null)
				{
					ObjURI = NewUri();
					CreateClientActivatedServerIdentity (Obj, RequestedType, ObjURI);
				}
			}
			else
			{
				ClientActivatedIdentity identity = GetIdentityForUri ("/" + ObjURI) as ClientActivatedIdentity;
				if (identity == null || Obj != identity.GetServerObject()) 
					CreateClientActivatedServerIdentity (Obj, RequestedType, ObjURI);
			}

			ObjRef oref;
			
			if (IsTransparentProxy (Obj))
				oref = RemotingServices.GetRealProxy (Obj).ObjectIdentity.CreateObjRef (RequestedType);
			else
				oref = Obj.CreateObjRef (RequestedType);
			
			TrackingServices.NotifyMarshaledObject (Obj, oref);
			return oref;
		}

		static string NewUri ()
		{
			if (app_id == null) {
				lock (app_id_lock) {
					if (app_id == null)
						app_id = Guid.NewGuid().ToString().Replace('-', '_') + "/";
				}
			}

			int n = Interlocked.Increment (ref next_id);
			return app_id + Environment.TickCount.ToString("x") + "_" + n + ".rem";
		}

		[ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
		public static RealProxy GetRealProxy (object proxy)
		{
			if (!IsTransparentProxy(proxy)) throw new RemotingException("Cannot get the real proxy from an object that is not a transparent proxy.");
			return (RealProxy)((TransparentProxy)proxy)._rp;
		}

		public static MethodBase GetMethodBaseFromMethodMessage(IMethodMessage msg)
		{
			Type type = Type.GetType (msg.TypeName);
			if (type == null)
				throw new RemotingException ("Type '" + msg.TypeName + "' not found.");

			return GetMethodBaseFromName (type, msg.MethodName, (Type[]) msg.MethodSignature);
		}

		internal static MethodBase GetMethodBaseFromName (Type type, string methodName, Type[] signature)
		{
			if (type.IsInterface) {
				return FindInterfaceMethod (type, methodName, signature);
			}
			else {
				MethodBase method = null;
				if (signature == null)
					method = type.GetMethod (methodName, methodBindings);
				else
					method = type.GetMethod (methodName, methodBindings, null, (Type[]) signature, null);
				
				if (method != null)
					return method;
					
				if (methodName == "FieldSetter")
					return FieldSetterMethod;

				if (methodName == "FieldGetter")
					return FieldGetterMethod;
				
				if (signature == null)
					return type.GetConstructor (methodBindings, null, Type.EmptyTypes, null);
				else
					return type.GetConstructor (methodBindings, null, signature, null);
			}
		}
		
		static MethodBase FindInterfaceMethod (Type type, string methodName, Type[] signature)
		{
			MethodBase method = null;
			
			if (signature == null)
				method = type.GetMethod (methodName, methodBindings);
			else
				method = type.GetMethod (methodName, methodBindings, null, signature, null);
				
			if (method != null) return method;
			
			foreach (Type t in type.GetInterfaces ()) {
				method = FindInterfaceMethod (t, methodName, signature);
				if (method != null) return method;
			}
			
			return null;
		}

		public static void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
		{
			if (obj == null) throw new ArgumentNullException ("obj");

			ObjRef oref = Marshal ((MarshalByRefObject)obj);
			oref.GetObjectData (info, context);
		}

		public static ObjRef GetObjRefForProxy(MarshalByRefObject obj)
		{
			Identity ident = GetObjectIdentity(obj);
			if (ident == null) return null;
			else return ident.CreateObjRef(null);
		}

		public static object GetLifetimeService (MarshalByRefObject obj)
		{
			if (obj == null) return null;
			return obj.GetLifetimeService ();
		}

		public static IMessageSink GetEnvoyChainForProxy (MarshalByRefObject obj)
		{
			if (IsTransparentProxy(obj))
				return ((ClientIdentity)GetRealProxy (obj).ObjectIdentity).EnvoySink;
			else
				throw new ArgumentException ("obj must be a proxy.","obj");			
		}

		[MonoTODO]
		[Conditional ("REMOTING_PERF")]
		[Obsolete ("It existed for only internal use in .NET and unimplemented in mono")]
		public static void LogRemotingStage (int stage)
		{
			throw new NotImplementedException ();
		}

		public static string GetSessionIdForMethodMessage(IMethodMessage msg)
		{
			// It seems that this it what MS returns.
			return msg.Uri;
		}

		public static bool IsMethodOverloaded(IMethodMessage msg)
		{
			const BindingFlags bfinst = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
			RuntimeType type = (RuntimeType) msg.MethodBase.DeclaringType;
			return type.GetMethodsByName (msg.MethodName, bfinst, RuntimeType.MemberListType.CaseSensitive, type).Length > 1;
		}

		public static bool IsObjectOutOfAppDomain(object tp)
		{
			MarshalByRefObject mbr = tp as MarshalByRefObject;

			if (mbr == null)
				return false;

			// TODO: use internal call for better performance
			Identity ident = GetObjectIdentity (mbr);
			return ident is ClientIdentity;
		}

		public static bool IsObjectOutOfContext(object tp)
		{
			MarshalByRefObject mbr = tp as MarshalByRefObject;

			if (mbr == null)
				return false;

			// TODO: use internal call for better performance
			Identity ident = GetObjectIdentity (mbr);
			if (ident == null) return false;
			
			ServerIdentity sident = ident as ServerIdentity;
			if (sident != null) return sident.Context != System.Threading.Thread.CurrentContext;
			else return true;
		}

		public static bool IsOneWay(MethodBase method)
		{
			return method.IsDefined (typeof (OneWayAttribute), false);
		}

		internal static bool IsAsyncMessage(IMessage msg)
		{
			if (! (msg is MonoMethodMessage)) return false;
			else if (((MonoMethodMessage)msg).IsAsync) return true;
			else if (IsOneWay (((MonoMethodMessage)msg).MethodBase)) return true;
			else return false;
		}

		public static void SetObjectUriForMarshal(MarshalByRefObject obj, string uri)
		{
			if (IsTransparentProxy (obj)) {
				RealProxy proxy = RemotingServices.GetRealProxy(obj);
				Identity identity = proxy.ObjectIdentity;

				if (identity != null && !(identity is ServerIdentity) && !proxy.GetProxiedType().IsContextful)
					throw new RemotingException ("SetObjectUriForMarshal method should only be called for MarshalByRefObjects that exist in the current AppDomain.");
			}
			
			Marshal (obj, uri);
		}

		#region Internal Methods
		
		internal static object CreateClientProxy (ActivatedClientTypeEntry entry, object[] activationAttributes)
		{
			if (entry.ContextAttributes != null || activationAttributes != null)
			{
				ArrayList props = new ArrayList ();
				if (entry.ContextAttributes != null) props.AddRange (entry.ContextAttributes);
				if (activationAttributes != null) props.AddRange (activationAttributes);
				return CreateClientProxy (entry.ObjectType, entry.ApplicationUrl, props.ToArray ());
			}
			else
				return CreateClientProxy (entry.ObjectType, entry.ApplicationUrl, null);
		}
	
		internal static object CreateClientProxy (Type objectType, string url, object[] activationAttributes)
		{
			string activationUrl = url;
			if (!activationUrl.EndsWith ("/"))
				activationUrl += "/";
			activationUrl += "RemoteActivationService.rem";

			string objectUri;
			GetClientChannelSinkChain (activationUrl, null, out objectUri);

			RemotingProxy proxy = new RemotingProxy (objectType, activationUrl, activationAttributes);
			return proxy.GetTransparentProxy();
		}
	
		internal static object CreateClientProxy (WellKnownClientTypeEntry entry)
		{
			return Connect (entry.ObjectType, entry.ObjectUrl, null);
		}
	
		internal static object CreateClientProxyForContextBound (Type type, object[] activationAttributes)
		{
			if (type.IsContextful)
			{
				// Look for a ProxyAttribute
				ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (type, typeof(ProxyAttribute), true);
				if (att != null)
					return att.CreateInstance (type);
			}
			RemotingProxy proxy = new RemotingProxy (type, ChannelServices.CrossContextUrl, activationAttributes);
			return proxy.GetTransparentProxy();
		}
#if !MOBILE
		internal static object CreateClientProxyForComInterop (Type type)
		{
			Mono.Interop.ComInteropProxy proxy = Mono.Interop.ComInteropProxy.CreateProxy (type);
			return proxy.GetTransparentProxy ();
		}
#endif
		internal static Identity GetIdentityForUri (string uri)
		{
			string normUri = GetNormalizedUri (uri);
			lock (uri_hash)
			{
				Identity i = (Identity) uri_hash [normUri];

				if (i == null) {
					normUri = RemoveAppNameFromUri (uri);
					if (normUri != null)
						i = (Identity) uri_hash [normUri];
				}

				return i;
			}
		}

		//
		// If the specified uri starts with the application name,
		// RemoveAppNameFromUri returns the uri w/out the leading
		// application name, otherwise it returns null.
		//
		// Assumes that the uri is not normalized.
		//
		static string RemoveAppNameFromUri (string uri)
		{
			string name = RemotingConfiguration.ApplicationName;
			if (name == null) return null;
			name = "/" + name + "/";
			if (uri.StartsWith (name))
				return uri.Substring (name.Length);
			else
				return null;
		}

		internal static Identity GetObjectIdentity (MarshalByRefObject obj)
		{
			if (IsTransparentProxy(obj))
				return GetRealProxy (obj).ObjectIdentity;
			else
				return obj.ObjectIdentity;
		}

		internal static ClientIdentity GetOrCreateClientIdentity(ObjRef objRef, Type proxyType, out object clientProxy)
		{
			// This method looks for an identity for the given url. 
			// If an identity is not found, it creates the identity and 
			// assigns it a proxy to the remote object.

			// Creates the client sink chain for the given url or channelData.
			// It will also get the object uri from the url.

			object channelData = objRef.ChannelInfo != null ? objRef.ChannelInfo.ChannelData : null;

			string objectUri;
			IMessageSink sink = GetClientChannelSinkChain (objRef.URI, channelData, out objectUri);

			if (objectUri == null) objectUri = objRef.URI;

			lock (uri_hash)
			{
				clientProxy = null;
				string uri = GetNormalizedUri (objRef.URI);
				
				ClientIdentity identity = uri_hash [uri] as ClientIdentity;
				if (identity != null)
				{
					// Object already registered
					clientProxy = identity.ClientProxy;
					if (clientProxy != null) return identity;
					
					// The proxy has just been GCed, so its identity cannot
					// be reused. Just dispose it.
					DisposeIdentity (identity);
				}

				// Creates an identity and a proxy for the remote object

				identity = new ClientIdentity (objectUri, objRef);
				identity.ChannelSink = sink;

				// Registers the identity
				uri_hash [uri] = identity;
				if (proxyType != null)
				{
					RemotingProxy proxy = new RemotingProxy (proxyType, identity);
					CrossAppDomainSink cds = sink as CrossAppDomainSink;
					if (cds != null)
						proxy.SetTargetDomain (cds.TargetDomainId);

					clientProxy = proxy.GetTransparentProxy();
					identity.ClientProxy = (MarshalByRefObject) clientProxy;
				}
				return identity;
			}
		}

		static IMessageSink GetClientChannelSinkChain(string url, object channelData, out string objectUri)
		{
			IMessageSink sink = ChannelServices.CreateClientChannelSinkChain (url, channelData, out objectUri);
			if (sink == null) 
			{
				if (url != null) 
				{
					string msg = String.Format ("Cannot create channel sink to connect to URL {0}. An appropriate channel has probably not been registered.", url); 
					throw new RemotingException (msg);
				}
				else 
				{
					string msg = String.Format ("Cannot create channel sink to connect to the remote object. An appropriate channel has probably not been registered.", url); 
					throw new RemotingException (msg);
				}
			}
			return sink;
		}

		internal static ClientActivatedIdentity CreateContextBoundObjectIdentity(Type objectType)
		{
			ClientActivatedIdentity identity = new ClientActivatedIdentity (null, objectType);
			identity.ChannelSink = ChannelServices.CrossContextChannel;
			return identity;
		}

		internal static ClientActivatedIdentity CreateClientActivatedServerIdentity(MarshalByRefObject realObject, Type objectType, string objectUri)
		{
			ClientActivatedIdentity identity = new ClientActivatedIdentity (objectUri, objectType);
			identity.AttachServerObject (realObject, Context.DefaultContext);
			RegisterServerIdentity (identity);
			identity.StartTrackingLifetime ((ILease)realObject.InitializeLifetimeService ());
			return identity;
		}

		internal static ServerIdentity CreateWellKnownServerIdentity(Type objectType, string objectUri, WellKnownObjectMode mode)
		{
			ServerIdentity identity;

			if (mode == WellKnownObjectMode.SingleCall)
				identity = new  SingleCallIdentity(objectUri, Context.DefaultContext, objectType);
			else
				identity = new  SingletonIdentity(objectUri, Context.DefaultContext, objectType);

			RegisterServerIdentity (identity);
			return identity;
		}

		private static void RegisterServerIdentity(ServerIdentity identity)
		{
			lock (uri_hash)
			{
				if (uri_hash.ContainsKey (identity.ObjectUri)) 
					throw new RemotingException ("Uri already in use: " + identity.ObjectUri + ".");

				uri_hash[identity.ObjectUri] = identity;
			}
		}

		internal static object GetProxyForRemoteObject (ObjRef objref, Type classToProxy)
		{
			ClientActivatedIdentity identity = GetIdentityForUri (objref.URI) as ClientActivatedIdentity;
			if (identity != null) return identity.GetServerObject ();
			else return GetRemoteObject (objref, classToProxy);
		}

		internal static object GetRemoteObject(ObjRef objRef, Type proxyType)
		{
			object proxy;
			GetOrCreateClientIdentity (objRef, proxyType, out proxy);
			return proxy;
		}
		
		// This method is called by the runtime
		internal static object GetServerObject (string uri)
		{
			ClientActivatedIdentity identity = GetIdentityForUri (uri) as ClientActivatedIdentity;
			if (identity == null) throw new RemotingException ("Server for uri '" + uri + "' not found");
			return identity.GetServerObject ();
		}

		// This method is called by the runtime
		[SecurityPermission (SecurityAction.Assert, SerializationFormatter = true)] // FIXME: to be reviewed
		internal static byte[] SerializeCallData (object obj)
		{
			var ctx = Thread.CurrentThread.GetExecutionContextReader().LogicalCallContext;
			if (!ctx.IsNull) {
				CACD cad = new CACD ();
				cad.d = obj;
				cad.c = ctx.Clone ();
				obj = cad;
			}
			
			if (obj == null) return null;
			MemoryStream ms = new MemoryStream ();
			lock (_serializationFormatter) {
				_serializationFormatter.Serialize (ms, obj);
			}
			return ms.ToArray ();
		}

		// This method is called by the runtime
		[SecurityPermission (SecurityAction.Assert, SerializationFormatter = true)] // FIXME: to be reviewed
		internal static object DeserializeCallData (byte[] array)
		{
			if (array == null) return null;
			
			MemoryStream ms = new MemoryStream (array);
			object obj;
			lock (_deserializationFormatter) {
				obj = _deserializationFormatter.Deserialize (ms);
			}
			
			if (obj is CACD) {
				CACD cad = (CACD) obj;
				obj = cad.d;

				var lcc = (LogicalCallContext) cad.c;
				if (lcc.HasInfo)
					Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext.Merge (lcc);
			}
			return obj;
		}
		
		// This method is called by the runtime
		[SecurityPermission (SecurityAction.Assert, SerializationFormatter = true)] // FIXME: to be reviewed
		internal static byte[] SerializeExceptionData (Exception ex)
		{
			byte[] result = null;
			try {
				/* empty - we're only interested in the protected block */
			} finally {
				MemoryStream ms = new MemoryStream ();
				lock (_serializationFormatter) {
					_serializationFormatter.Serialize (ms, ex);
				}
				result = ms.ToArray ();
			}
			return result;
		}
		
		internal static object GetDomainProxy(AppDomain domain) 
		{
			byte[] data = null;

			Context currentContext = Thread.CurrentContext;

			try
			{
				data = (byte[])AppDomain.InvokeInDomain (domain, typeof (AppDomain).GetMethod ("GetMarshalledDomainObjRef", BindingFlags.Instance|BindingFlags.NonPublic), domain, null);
			}
			finally
			{
				AppDomain.InternalSetContext (currentContext);
			}				

			byte[] data_copy = new byte [data.Length];
			data.CopyTo (data_copy, 0);
			MemoryStream stream = new MemoryStream (data_copy);
			ObjRef appref = (ObjRef) CADSerializer.DeserializeObject (stream);
			return (AppDomain) RemotingServices.Unmarshal(appref);
		}

		private static void RegisterInternalChannels() 
		{
			CrossAppDomainChannel.RegisterCrossAppDomainChannel();
		}
	        
		internal static void DisposeIdentity (Identity ident)
		{
			lock (uri_hash)
			{
				if (!ident.Disposed) {
					ClientIdentity clientId = ident as ClientIdentity;
					if (clientId != null)
						uri_hash.Remove (GetNormalizedUri (clientId.TargetUri));
					else
						uri_hash.Remove (ident.ObjectUri);
						
					ident.Disposed = true;
				}
			}
		}

		internal static Identity GetMessageTargetIdentity (IMessage msg)
		{
			// Returns the identity where the message is sent

			if (msg is IInternalMessage) 
				return ((IInternalMessage)msg).TargetIdentity;

			lock (uri_hash)
			{
				string uri = GetNormalizedUri (((IMethodMessage)msg).Uri);
				return uri_hash [uri] as ServerIdentity;
			}
		}

		internal static void SetMessageTargetIdentity (IMessage msg, Identity ident)
		{
			if (msg is IInternalMessage) 
				((IInternalMessage)msg).TargetIdentity = ident;
		}
		
		internal static bool UpdateOutArgObject (ParameterInfo pi, object local, object remote)
		{
			if (pi.ParameterType.IsArray && ((Array)local).Rank == 1)
			{
				Array alocal = (Array) local;
				if (alocal.Rank == 1)
				{
					Array.Copy ((Array) remote, alocal, alocal.Length);
					return true;
				}
				else
				{
					// TODO
				}
			}
			return false;
		}
		
		static string GetNormalizedUri (string uri)
		{
			if (uri.StartsWith ("/")) return uri.Substring (1);
			else return uri;
		}

		#endregion
	}
}