Click here to Skip to main content
13,142,065 members (53,056 online)
Click here to Skip to main content

Stats

68.6K views
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.Generic;
using System.Text;
using System.Security.Permissions;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

using System.Runtime.Serialization;

namespace ContextBoundRemoting
{

    /// <summary>
    /// 
    /// </summary>
    public interface ICrossDomainService
    {
        IMessage Marshal(IMessage msg);
    }

    /// <summary>
    /// 
    /// </summary>
    public class CrossDomainService :
        MarshalByRefObject,
        ICrossDomainService
    {
        /// <summary>
        /// 
        /// </summary>
        class Proxy : RealProxy
        {
            string uri;

            [PermissionSet(SecurityAction.LinkDemand)]
            public Proxy(MarshalByRefObject obj)
            {
                this.uri = RemotingServices.Marshal(obj).URI;
            }

            [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
            public override IMessage Invoke(IMessage msg)
            {
                msg.Properties["__Uri"] = this.uri;
                return ChannelServices.SyncDispatchMessage(msg);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        Proxy proxy;

        /// <summary>
        /// 
        /// </summary>
        public CrossDomainService()
        {
            this.proxy = new Proxy(this);
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }

        public IMessage Marshal(IMessage msg)
        {
            return this.proxy.Invoke(msg);
        }
    }


    public class PrintService :
        CrossDomainService,
        IPrintService
    {
        public string PrintMessage(string msg)
        {
            Console.WriteLine("{0} in AppDomain {1}", msg, AppDomain.CurrentDomain.FriendlyName);
            return "Ok " + msg;
        }
    }
}

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

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