.NET Remoting basics

November 26, 2009

Remoting has been a topic of my interest right from early days of .NET. Here is just an beginner’s tutorial.

1. Whats Remoting?

A: In one line – Interprocess communication in distributed computing.

2. How far distributed?

A: Across the world over internet.

3. Unterstanding remoting from a simple use case?

A: There are 3 parts to the simple use case for remoting:

          a. Remotable object. (Data which travels on the channel)
          b. Host application domain to listen for requests for that object. (Server)
          c. Client application domain that makes requests for that object. (Client)

Lets start with a simple application in Visual Studio…the project will demonstrate a Patient service and remotable Patient objects.

There will be 4 projects

1. Remoting.ClientAccess (Just the interface which the client needs to access the objects on the server)
2. Remoting.ServerObject (Implementation of the interfaces from point 1)
3. RemotingServer (The console application running as server to listen to requests)
4. RemotingTest (The client console application which will make RPCs using the interfaces in point 1)

Project 1 //Remoting.ClientAccess //build it into a library

namespace Remoting.ClientAccess
{
    public interface IPatient
    {
        int PatientCode { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        int HospitalCode { get; set; }       
    }
}

namespace Remoting.ClientAccess
{
    public interface IPatientService
    {
        void SavePatient(IPatient patient);
    }
}

Project 2 //Remoting.ServerObject // build as a library //reference to project 1

namespace Remoting.ServerObjects
{
    [Serializable]
    public class Patient : System.MarshalByRefObject ,IPatient
    {
        private int ptCode;
        private string fName;
        private string lName;
        private int hospCode;
        #region IPatient Members

        public int PatientCode
        {
            get
            {
                return ptCode;
            }
            set
            {
                ptCode = value;
            }
        }

        public string FirstName
        {
            get
            {
                return fName;
            }
            set
            {
                fName = value;
            }
        }

        public string LastName
        {
            get
            {
                return lName;
            }
            set
            {
                lName = value;
            }
        }

        public int HospitalCode
        {
            get
            {
                return hospCode;
            }
            set
            {
                hospCode = value;
            }
        }
       
        #endregion
    }

}

[Serializable]
    public class PatientService : System.MarshalByRefObject, IPatientService
    {

        #region IPatientService Members

        public void SavePatient(IPatient patient)
        {
            Console.WriteLine(“First name : ” + patient.FirstName);
            Console.WriteLine(“Last name : ” + patient.LastName);
            Console.WriteLine(“Code : ” + patient.PatientCode);
            Console.WriteLine(“Hosp code : ” + patient.HospitalCode);
        }      

        #endregion
    }

Project 3 //RemotingServer // build as a console application //reference to project 1,2

namespace RemotingServer
{
    class Program
    {       
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure(“RemotingServer.exe.config”);
          
            Console.WriteLine(“Press return to exit”);
            Console.ReadLine();
        }
    }
}

Here is the config file which is used in above code:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode=”Singleton” type=”Remoting.ServerObjects.Patient, Remoting.ServerObjects” objectUri=”Patient.rem” />
        <wellknown mode=”Singleton” type=”Remoting.ServerObjects.PatientService, Remoting.ServerObjects” objectUri=”PatientService.rem” />
      </service>
      <channels>
        <channel ref=”tcp” port=”8000″>
          <serverProviders>
            <!–provider ref=”wsdl” / –>
            <!–formatter ref=”soap” typeFilterLevel=”Full” /–>
            <formatter ref=”binary” typeFilterLevel=”Full” />
          </serverProviders>
          <!–clientProviders>
            <formatter ref=”binary” />
          </clientProviders–>
        </channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

Project 4 //RemotingTest // build as a console application //reference to project 1

namespace RemotingTest
{
    class Program
    {
        static void Main(string[] args)
        {
           
            IPatientService ptSvc = (IPatientService)Activator.GetObject(typeof(IPatientService), “tcp://172.16.11.22:8000/PatientService.rem”);
            IPatient pt = (IPatient)Activator.GetObject(typeof(IPatient), “tcp://172.16.11.22:8000/Patient.rem”);
            pt.FirstName = “Vaibhav”;
            pt.LastName = “Gaikwad”;
            pt.PatientCode = 23423 ;
            pt.HospitalCode = 234 ;
            ptSvc.SavePatient(pt);
        }
    }
}

Execute the server application and then execute the client application

One Response to “.NET Remoting basics”


  1. u039au03acu03c4u03b9 u03aeu03beu03b5u03c1u03b5 u03b7 u03b3u03b9u03b1u03c6u03b9u03ac u03bcu03bfu03c5 u03c0u03bfu03c5 u03b4u03b5u03bd u03c0u03bfu03c5u03 Click https://twitter.com/moooker1


Leave a comment