Click here to Skip to main content
15,895,777 members
Articles / Programming Languages / C#

.NET Remoting Relay Server

Rate me:
Please Sign up or sign in to vote.
4.60/5 (2 votes)
5 Jul 2006CPOL4 min read 37.9K   398   22  
An article about re-deploying remote services with the help of a relay server.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using System.Runtime.Remoting;
using Application.Shared;

namespace ApplicationClient
{
    class Program
    {
        delegate string FormatTimeStampDelegate(DateTime dt);
        delegate void ConvertDelegate(Input input, out Output output);
        delegate void CallbackDelegate(Callback cb);

        IApplicationObject1 service1;
        IApplicationObject2 service2;
        IApplicationObject3 service3;

        FormatTimeStampDelegate delegate1;
        ConvertDelegate delegate2;
        CallbackDelegate delegate3;


        Random random = new Random();
        Timer timer;
        void TimerCallback(object state)
        {
            try
            {
                string text = this.service1.FormatTimeStamp(DateTime.Now);
                Console.WriteLine("Service1: {0}", text);
                this.service1.PostTimeStamp(text);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            try
            {
                Input input = new Input(random.Next(0, 2).ToString(), random.Next(0, 100));
                Output output = null;
                this.service2.Convert(input, out output);
                Console.WriteLine("Service2: {0}, {1}", input, output);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                Callback cb = new Callback();
                this.service3.SendCallback(cb);
                Console.WriteLine("Service3: {0}", cb.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            try
            {
                IAsyncResult ar1 = this.delegate1.BeginInvoke(DateTime.Now, null, null);

                Input input = new Input(random.Next(0, 2).ToString(), random.Next(0, 100));
                Output output = null;
                IAsyncResult ar2 = this.delegate2.BeginInvoke(input, out output, null, null);

                Callback cb = new Callback();
                IAsyncResult ar3 = this.delegate3.BeginInvoke(cb, null, null);

                string text = this.delegate1.EndInvoke(ar1);
                Console.WriteLine("Delegate1: {0}", text);

                this.delegate2.EndInvoke(out output, ar2);
                Console.WriteLine("Delegate2: {0}, {1}", input, output);

                this.delegate3.EndInvoke(ar3);
                Console.WriteLine("Delegate3: {0}", cb.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            this.timer.Change(1000, Timeout.Infinite);
        }

        void Run()
        {
            string uri;
            uri = "tcp://localhost:5000/ApplicationObject1";
            this.service1 = (IApplicationObject1)Activator.GetObject(typeof(IApplicationObject1), uri);
            this.delegate1 = new FormatTimeStampDelegate(this.service1.FormatTimeStamp);

            uri = "tcp://localhost:5000/ApplicationObject2";
            this.service2 = (IApplicationObject2)Activator.GetObject(typeof(IApplicationObject2), uri);
            this.delegate2 = new ConvertDelegate(this.service2.Convert);

            uri = "tcp://localhost:5000/ApplicationObject3";
            this.service3 = (IApplicationObject3)Activator.GetObject(typeof(IApplicationObject3), uri);
            this.delegate3 = new CallbackDelegate(this.service3.SendCallback);

            this.timer = new Timer(this.TimerCallback);
            this.timer.Change(1000, Timeout.Infinite);
        }


        static void Main(string[] args)
        {
            try
            {
                string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                RemotingConfiguration.Configure(configFile, false);

                new Program().Run();

                Console.WriteLine("Press ENTER to exit.");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.WriteLine("Press ENTER to exit.");
                Console.ReadLine();
            }
        }
    }
}

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)


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

Comments and Discussions