Description
In this article I will show you how to develop your first .Net Remoting Application, and how to send object between two different applications.
Introduction If you are developing distributed applications and you want to send object between these applications, such sending customer information from one application to the other. You have to use the remoting framework to be able to send data between the .Net applications.
The Problem
Before we start with the code we have to define the problem we have, suppose you have application called App1 which is a windows application, that is hotel management system application and you have to know the if room number (x) is Free or occupied. And there is another application called App2 running in different computer, and connected to the central database. Now you need to send a request from App1 to App2 to check if the room is occupied.
The Solution
We will use the .Net remoting framework classes to solve this problem, let us look to the following diagram and define each object and its function in the solution.
Figure 1: proposal solution
Here is the solution; App1 is your windows application running in computer X. App2 the other application running in computer Y and connected to the database. Also you will see common assembly which will play an important role in the solution. Common assembly will contain an interface which App2 must implement and used by App1 to know the type of the object it will use. Finally the channel that connects applications and use a specific port.
In This article I will create Server-Activated Object (SAO) if you need more information about SAO refer to my article
Overview of .Net Remotingthese are the steps:
1- Create a common assembly that define the interface that your SAO will implement
2- Create a server, register the remote object's type as SingleCall and the endpoint
3- Create a client, create the local proxy object.
1- Create a Common Assembly A common assembly is necessary to separate the definition of the remotable class from its implementation; you have to crate a class library in visual studio project which will contain a simple interface contains a simple method. Such like this code:
C# |
using System;
using System.Collections.Generic;
using System.Text;
namespace Common
{
public class CommonClass
{
public interface IRoomChecker
{
string CheckRoom(int number);
}
}
}
|
We have here a simple class library which contains a class called
CommomClass that contains a public interface
IRoomChecker, the
CheckRoom is a method that take the room number and return its status. Finally build your common assembly.
2- Create a Server
The server is the application that contains the object which we need to call, in our solution it is App2.
1- Choose an executable assembly, such as Windows application or a consol application in visual studio.
2- After you have a server application, add reference to the System.Runtime.Remoting assembly and your common assembly.
3- Then add this code to import the System.Runtime.Remoting, here in this code I will use TCP channel.
C# |
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
|
4- Add code to import your common assembly, if it is called common, enter the following code:
5- Add a class for your SAO; this class must inherit from MarshalByRefObject and implement the interface defined in your common assembly. For example:
C# |
class MyCalss : MarshalByRefObject, CommonClass.IRoomChecker
{
public string CheckRoom(int number)
{
// Add code that connect to the database
// and retrive the room status.
// i will use simple method implementation just to
// run the application, because connection to database
// is beyond the scope of this article.
string status = String.Empty;
if (number == 100)
{
status = "occupied";
} return (status);
}
}
|
6- Now you have to add code to establish a channel to transport messages, the following code creates a TCP channel object on port 9000. And then register that channel. Add this code in the Main method of your server's startup object
C# |
TcpChannel MyChannel = new TcpChannel(9000);
ChannelServices.RegisterChannel(MyChannel,true);
|
7- In the Main method, write the code to register the remote object's type and endpoint. Now you have to decide which activation mode you will use SingleCall or Singleton. You can change the type from the third parameter in the constructor .
C# |
WellKnownServiceTypeEntry RemObj = new WellKnownServiceTypeEntry(typeof(MyCalss),"RemotingServer/RoomChecker",WellKnownObjectMode.SingleCall); RemotingConfiguration.RegisterWellKnownServiceType(RemObj);
|
The last code snippet register your Remoting CommonClass object as a SingleCall object that is accessed at the tcp://ServerUrl:9000/ RemotingServer/RoomChecker URI.
3- Create a Client
The client is the application that need to invoke a method in the server, here in our solution is App1.
1- Choose an executable assembly, such as Windows application or a consol application in visual studio.
2- After you have a client application, add reference to the System.Runtime.Remoting assembly and your common assembly.
3- Then add this code to import the System.Runtime.Remoting, here in this code I will use TCP channel.
C# |
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
|
4- Add code to import your common assembly, if it is called common, enter the following code:
5- Write a code to establish a channel to transport messages
C# |
TcpClientChannel ClientChannel = new TcpClientChannel();
ChannelServices.RegisterChannel(ClientChannel,true);
|
6- Add the code to create the local proxy object, this code using Activator.GetObject() method to create a proxy to a remote object.
C# |
CommonClass.IRoomChecker Obj= (CommonClass.IRoomChecker)Activator.GetObject(typeof(CommonClass.IRoomChecker), "tcp://nemo:9000/RemotingServer/RoomChecker");
|
GetObject method takes two parameters the first one is the Type of the object and the second is the URL of the object, Note that "nemo" is the computer that host the remoting object that App1 will call.
Now you can use the object to execute any method that belongs to the Remoting object, but don't forget to run the server application first before calling the methods.
Conclusion
By this simple way, you can send objects between two different applications created using .Net framework. In .Net framework 3.0 Microsoft introduced a new programming concept for creating distributed applications called windows communication foundation, but you can use WCF to communicate with .Net remoting Applications.