Click here to Skip to main content
12,445,274 members (55,329 online)
Click here to Skip to main content

Stats

65.1K views
1.2K downloads
58 bookmarked
Posted

.NET Remoting and Cross Domain Marshalling

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

namespace ContextBoundRemoting
{
    /// <summary>
    /// 
    /// </summary>
    class CustomProxy : RealProxy
    {
        string url;
        string clientID;
        IMessageSink messageSink;

        public CustomProxy(string url, Type type) : base(type)
        {
            this.url = url;
            // create a unique identifier
            this.clientID = Guid.NewGuid().ToString();

            foreach (IChannel channel in ChannelServices.RegisteredChannels)
            {
                if (channel is IChannelSender)
                {
                    IChannelSender sender = (IChannelSender)channel;
                    if (string.Compare(sender.ChannelName, "tcp") == 0)
                    {
                        string objectUri;
                        this.messageSink = sender.CreateMessageSink(this.url, null, out objectUri);
                        if (this.messageSink != null)
                            break;
                    }
                }
            }

            if (this.messageSink == null)
                throw new Exception("No channel found for " + this.url);
        }

        public override IMessage Invoke(IMessage msg)
        {
            msg.Properties["__Uri"] = this.url;

            // pass the client's unique identifier as part of the call context
            LogicalCallContext callContext = (LogicalCallContext)msg.Properties["__CallContext"];
            callContext.SetData("__ClientID", this.clientID);
            
            return this.messageSink.SyncProcessMessage(msg);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                RemotingConfiguration.Configure(configFile, false);
 
                string url = "tcp://localhost:1234/PrintService.rem";

                // IPrintService service = (IPrintService)Activator.GetObject(typeof(IPrintService), url);

                IPrintService service = (IPrintService)new CustomProxy(url, typeof(IPrintService)).GetTransparentProxy();

                string msg = service.PrintMessage("Hello World");
                Console.WriteLine(msg);

                msg = service.PrintMessage("Hello World again ...");
                Console.WriteLine(msg);

                Console.ReadLine();

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

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.

License

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

Share

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...

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