Showing posts with label .Net Remoting. Show all posts
Showing posts with label .Net Remoting. Show all posts

Friday, January 16, 2009

.Net Remoting Walkthrough

Remoting is the process of two pieces of software communicating across application domains.
Start a Class Library call it say ShalvinObject. Create a class that inherits from MarshalByRefObject.

namespace ShalvinObject
{
class RemoteObject : MarshalByRefObject
{
public int Mul(int i, int j)
{
return i * j;
}
}

Now we are going to host the service. This can be done with Console, Windows Forms or Web Service.
Letsuse Windows Forms.
Add to a reference to the previously created dll, ie. ShalvinObject.dll and System.Runtime.Remoting.


using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

private void Form1_Load(object sender, EventArgs e)
{
TcpChannel c = new TcpChannel(900);
ChannelServices.RegisterChannel(c);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ShalvinObject.RemoteObject), "Shalvin", WellKnownObjectMode.Singleton);
}

Now for testing the service lets create another Windows Application. Here too set references to ShalvinObject.dll and System.Runtime.Remoting.


using ShalvinObject;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;


private void Form1_Load(object sender, EventArgs e)
{
TcpChannel c = new TcpChannel();
ChannelServices.RegisterChannel(c);
object obj = Activator.GetObject(typeof(ShalvinObject.RemoteObject), "tcp://localhost:900/Shalvin");
RemoteObject ro = (RemoteObject)obj;
int intRes = ro.Mul(56, 45);
MessageBox.Show(intRes.ToString());
}


Related Blog
Currency Conversion Web Service with Asp .Net

Creating and Consuming Class Library in C#, Object Initializer