Most Viewed

Most Viewed

Sunday 15 July 2012

C#.Net Remoting application


Steps for C#.Net Remoting application :

  1. Create service first
  2. Create server  object by ref. service
  3. create client  code.

  1. Create Service first – new project à class library à

Create a folder  : c:\remotingdemo and store class library , client and server in this folder.

New Project à Class Library à SimpleRemotingLibrary
Location : c:\>remotingDemo

using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleRemotingLibrary
{
    public class RemoteObject:MarshalByRefObject
    {

        public RemoteObject()
        {
            Console .WriteLine (" constructor called for RemoteObject");
        }
        public void DisplayMessage(string msg)
        {
            Console.WriteLine("Message : " + msg);
        }
        public string ReturnMessage()
        {
            return ("Hello from server");
        }
    }
}

Build Solution

Step 2 :

New Project à Console Application  à SimpleRemoteObjectServer
Location : c:\>remotingDemo

// Server application – Console application
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
namespace SimpleRemoteObjectServer
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                HttpChannel c = new HttpChannel(32469);
                ChannelServices.RegisterChannel(c,false);
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(SimpleRemotingLibrary.RemoteObject), "RemotingObject.soap", WellKnownObjectMode.Singleton);
                Console.WriteLine("Server is ready... Tap any key to stop service");
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
    }
}

Build  Solution:

New Project à Console Application  à SimpleClient
Location : c:\>remotingDemo

step 3 :

// client application – Console application
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Proxies;

namespace SimpleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpChannel c = new HttpChannel();
            ChannelServices.RegisterChannel(c,false );
            object remobj = Activator.GetObject(typeof(SimpleRemotingLibrary.RemoteObject), "http://localhost:32469/RemotingObject.soap");
            SimpleRemotingLibrary .RemoteObject  simple= (SimpleRemotingLibrary .RemoteObject ) remobj ;
            simple.DisplayMessage ("Hello From Client - Murthy" );
            Console.WriteLine ("Server says : "+simple.ReturnMessage ());
            Console.ReadKey ();
        }
    }
}


Add existing Project à SimpleServerObject.exe code from bin/debug directory to client application .

Then goto solution à right click à properties à select Multiple Projects à first star server application then start client application à start  (Order it properly).


Run the application  and see the output.


No comments:

Post a Comment