Click here to Skip to main content
13,000,469 members (60,902 online)
Click here to Skip to main content


59 bookmarked
Posted 10 Apr 2006

.NET Remoting and Cross Domain Marshalling

, 10 Apr 2006
An article about how to marshal a remote client request from one AppDomain to another.
using System;
using System.Collections;
using System.Text;
using System.Security.Permissions;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Channels;

namespace Touchcom.Prototypes.ContextBoundRemoting
    // Create a custom 'RealProxy'.
    public class MyProxy : RealProxy
        String myURIString;
        MarshalByRefObject myMarshalByRefObject;

        public MyProxy(Type myType)
            : base(myType)
            // RealProxy uses the Type to generate a transparent proxy.
            myMarshalByRefObject = (MarshalByRefObject)Activator.CreateInstance((myType));
            // Get 'ObjRef', for transmission serialization between application domains.
            ObjRef myObjRef = RemotingServices.Marshal(myMarshalByRefObject);
            // Get the 'URI' property of 'ObjRef' and store it.
            myURIString = myObjRef.URI;
            Console.WriteLine("URI :{0}", myObjRef.URI);

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
        public override IMessage Invoke(IMessage myIMessage)
            Console.WriteLine("MyProxy.Invoke Start");

            if (myIMessage is IMethodCallMessage)

            if (myIMessage is IMethodReturnMessage)

            Type msgType = myIMessage.GetType();
            Console.WriteLine("Message Type: {0}", msgType.ToString());
            Console.WriteLine("Message Properties");
            IDictionary myIDictionary = myIMessage.Properties;
            // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
            myIDictionary["__Uri"] = myURIString;
            IDictionaryEnumerator myIDictionaryEnumerator =

            while (myIDictionaryEnumerator.MoveNext())
                Object myKey = myIDictionaryEnumerator.Key;
                String myKeyName = myKey.ToString();
                Object myValue = myIDictionaryEnumerator.Value;

                Console.WriteLine("\t{0} : {1}", myKeyName,
                if (myKeyName == "__Args")
                    Object[] myObjectArray = (Object[])myValue;
                    for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
                        Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex,

                if ((myKeyName == "__MethodSignature") && (null != myValue))
                    Object[] myObjectArray = (Object[])myValue;
                    for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
                        Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex,

            IMessage myReturnMessage;

            myIDictionary["__Uri"] = myURIString;
            Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);

            myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);

            // Push return value and OUT parameters back onto stack.

            IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)
            Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}",

            Console.WriteLine("MyProxy.Invoke - Finish");

            return myReturnMessage;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wytek Szymanski
Web Developer
United States United States
I am a consultant, trainer, software archtect/engineer, since the early 1980s, working in the greater area of Boston, MA, USA.

My work comprises the entire spectrum of software, shrink-wrapped applications, IT client-server, systems and protocol related work, compilers and operating systems, and more ....

I am currently focused on platform development for distributed computing in service oriented data centers.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 10 Apr 2006
Article Copyright 2006 by Wytek Szymanski
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid