A quick look at .NET Remoting

by Jagadish Pulakhandam on 12/2/2012 10:21:08 AM
Rated 0 from 0 votes
Brief: Demonstrates on developing a Remoting Server (which serves informaiton) and Remoting Client (which consumes information)
Posted to: Advanced concepts in .NET Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached Source Code covers the following:
  • Developing "interface" to make it available to both server and client
  • Developing serializable classes using "MarshalByRefObject" 
  • Developing a Windows Form which acts as .NET Remoting Server (or service)
  • Developing a Windows Form which acts as .NET Remoting Client (which consumes from the above server)
  • Working with "TcpChannel"
  • How to check if the channel is already registered
  • Creating and registering Channels
  • Getting the object proxy (of remote object available at server) to the client
  • Calling remote methods
Screenshots:





Source Code:

1.namespace BusinessInterfaces
2.{
3.    public interface IEmployee
4.    {
5.        double GetAnnualSalary(double sal);
6.    }
7. 
8.}

01.namespace BusinessComponents
02.{
03.    [Serializable]
04.    public class Employee : MarshalByRefObject, BusinessInterfaces.IEmployee
05.    {
06.        public double GetAnnualSalary(double sal)
07.        {
08.            return sal*12;
09.        }
10.    }
11.}

01.using System;
02.using System.Runtime.Remoting;
03.using System.Runtime.Remoting.Channels;
04.using System.Runtime.Remoting.Channels.Tcp;
05.using System.Windows.Forms;
06.namespace RemotingServer
07.{
08.    public partial class Form1 : Form
09.    {
10.        public Form1()
11.        {
12.            InitializeComponent();
13.        }
14. 
15.        private const int portNo = 8082;
16. 
17.        private void Form1_Load(object sender, EventArgs e)
18.        {
19.            //create server channel (to start listening)
20.            TcpChannel CurrentChannel = new TcpChannel(portNo);
21.             
22.            //register server channel
23.            ChannelServices.RegisterChannel(CurrentChannel, false);
24.             
25.            //configure remoting parameters
26.            //RemotingConfiguration.ApplicationName = "RemotingBusinessComponent";
27.            RemotingConfiguration.RegisterWellKnownServiceType(typeof(BusinessComponents.Employee), "EmployeeService",
28.                                                               WellKnownObjectMode.SingleCall);
29. 
30.            lblMsg.Text = "Service started..";
31.        }
32. 
33.    }
34.}

01.using System;
02.using System.Collections;
03.using System.Runtime.Remoting.Channels;
04.using System.Runtime.Remoting.Channels.Tcp;
05.using System.Windows.Forms;
06.using BusinessInterfaces;
07.using System.Linq;
08. 
09.namespace RemotingClient
10.{
11.    public partial class Form1 : Form
12.    {
13.        public Form1()
14.        {
15.            InitializeComponent();
16.        }
17. 
18.        const string url = "tcp://localhost:8082/EmployeeService";
19.        private const string clientChannelName = "EmployeeServiceChannel";
20. 
21.        private void btnCalcAnnSal_Click(object sender, EventArgs e)
22.        {
23.            try
24.            {
25.                //check to see if the channel is already opened
26.                if (ChannelServices.RegisteredChannels
27.                    .FirstOrDefault(c => c.ChannelName == clientChannelName) == null)
28.                {
29.                    //if channel is not registered, create new one here
30. 
31.                    IDictionary propBag = new Hashtable();
32.                    propBag["name"] = clientChannelName; //set the client channel name here
33. 
34.                    TcpChannel CurrentChannel = new TcpChannel(propBag, null, null); //create the client channel
35.                    ChannelServices.RegisterChannel(CurrentChannel); //register the client channel
36.                }
37. 
38.                //get the proxy of remote object
39.                IEmployee oEmployee = (IEmployee)Activator.GetObject(typeof(IEmployee), url); //get remote object
40.                 
41.                //call method in remote object (remote method invocation)
42.                MessageBox.Show("Calcuated Annual Salary: " + oEmployee.GetAnnualSalary(Convert.ToDouble(txtSal.Text)).ToString());
43.            }
44.            catch (Exception ex)
45.            {
46.                MessageBox.Show(ex.Message);
47.            }
48.        }
49. 
50.    }
51.}

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following