Introduction
This article will explores about .NET remoting. .NET remoting is a technology that allows you to invoke or accessing objects on remote server. For those who are familiar with Java technology, it is called RMI (Remote Method Invocation) in java world.
.NET remoting supports accessing your objects by using HTTP,TCP and IPC channels. Similar with Web Services, .NET remoting also support calling your objects via HTTP protocol. However Web Services provide platform independence and you also get loose coupling between your client server objects which means that easier to deal with versioning issues.
Unlike Web Services, .NET remoting requires both of your client and server objects running on the same .NET Framework version. and it has a tight coupling between client and server because the same object types are shared.
Confuse about which one to choose?. Since this article is about .NET Remoting, I will list down why you should choose .NET remoting for your apps over the Web Services.
1. If you develop application only for internal use and not for public use.
2. If you require faster performance with less overhead.
3. If you transfer large data between your client and server application.
4. Web Services is running on Http protocol and you know that http protocol can go timeout if the
processing time is more than 20 minutes. Although you can override this.
5. If you need to access your objects using protocol other than HTTP or HTTPS.
6. If you need richer object oriented model for your client server apps.
Remote Objects, Clients and Servers.
In this example below, we will try to write very basic .NET Remoting technology comprising of RemoteObjects, Clients and Servers application.
RemoteObjects is the objects being shared by Client and Server application which can be invoked using protocols that you define. .NET remoting support object invocation via HTTP,TCP and IPC protocols.
Developing .NET remoting application consists of 3 steps.
1. Developing the RemoteObjects (consists of method that you like to invoke on the remote server)
2. Developing the Server Application. (listens on certain port and certain protocol)
3. Developing Client Application. (you can use this apps to invoke server objects where the server
application is running)
Developing the Remote Objects.
Open Visual Studio, and Create New Class Library.
Add References to System.Runtime.Remoting.
Create a new Class Called RemoteObjects.cs. Please note that the Remoteobjects must be derived from System.MarshalByRefObject class.
public class RemoteObjects : System.MarshalByRefObject
{
public RemoteObjects()
{
}
public void StopIIS()
{
ExecuteProcess(@"net", "stop w3svc");
}
public void StartIIS()
{
ExecuteProcess(@"net", "start w3svc");
}
private void ExecuteProcess(string ProcessFileName, string ProcessArguments)
{
string Result = "";
Process myProcess = new Process();
try
{
myProcess.StartInfo.FileName = ProcessFileName;
myProcess.StartInfo.Arguments = ProcessArguments;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();
Result = myProcess.StandardError.ReadToEnd();
myProcess.WaitForExit(20000);
if (!myProcess.HasExited)
{
myProcess.Kill();
}
}
catch (Exception ex)
{
EventLog.WriteEntry("Remoting", ex.ToString());
}
finally
{
myProcess.Close();
}
}
}
From the code above, you can see that we create few methods inside the Remoting objects that we can use to invoke Start IIS or Stop IIS on remote Server. This is particularly useful if you like to have some sort of utility that allow you to do IIS reset without have to login to the server.
Developing the Remote Server application.
For the Server, we will be creating a Console application called Server.cs. If you are planning to launch the .NET remoting application on the real production server, then I would recommend you to write it in Windows Service rather than on Console application. But for simplicity of this example, we will write it in Console.
Open Visual Studio, and Create New Console Applications. Reference System.Runtime.Remoting and reference your RemoteObjects Class library as we created above.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Server
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8500);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType
(typeof(RemoteObjects.RemoteObjects), "Remoting Server", WellKnownObjectMode.Singleton);
Console.WriteLine("Remoting Server is Listening on TCP Port 8500.\nPlease Press Enter For Exit");
Console.ReadLine();
}
}
}
From the code above, you can see that we are creating a Server application that listening on TCP Port 8500.And you will notice that the RemoteObjects is being referenced by the Server application and the RemoteObjects has been registered to be exposed as objects which can be called by your client application.
In the example above, the remote object type is registered using WellKnownObjectMode.Singleton. There are two types available for WellKnownObjectMode.
1. Singleton.
Only one instance per Server. Every objects call will be referencing to the same objects.
2. SingleCall
Create new instance for every objects call.
Developing the Remoting Client Application
For the Client application,we will be developing as a Windows Form application.
Create New Project, and Choose Windows Application as the Template.
Don't Forget to reference the RemotingObjects Library and System.Runtime.Remoting
Drag two button in your Windows Forms and named it btnStartIIS and btnStopIIS.
Paste the code below for the button Event Handler.
private void btnStartIIS_Click(object sender, EventArgs e)
{
TcpClientChannel chan = new TcpClientChannel();
RemoteObjects.RemoteObjects objRemote = (RemoteObjects.RemoteObjects)Activator.GetObject
(typeof(RemoteObjects.RemoteObjects), "tcp://localhost:8500/Remoting Server");
if (objRemote == null)
{
MessageBox.Show("Could not Locate Server");
}
objRemote.StartIIS();
}
private void btnStopIIS_Click(object sender, EventArgs e)
{
TcpClientChannel chan = new TcpClientChannel();
RemoteObjects.RemoteObjects objRemote = (RemoteObjects.RemoteObjects)Activator.GetObject
(typeof(RemoteObjects.RemoteObjects), "tcp://localhost:8500/Remoting Server");
if (objRemote == null)
{
MessageBox.Show("Could not Locate Server");
}
objRemote.StopIIS();
}
}
Running the Remoting Application.
To start demonstrating the Remoting application, we will be running both the server and client application on your localhost machine.
First, you need to start the Server Application. Once you run the Server application, your apps should be listened on TCP port 8500.
Next, fire up your Client application and try to press button Stop IIS. Wait for a while and then check for your IIS status in your service manager. Your Worldwideweb server should be stopped.
If for some reason it is not working, please make sure no application is running on port 8500 and you have IIS server installed on your XP machine.
Running the Remoting Application in Remote Server
If you wish to run the Remoting application that you just developed on remote server, then you need to change your Client Application code a bit. You just need to change the tcp://localhost to tcp://yourserverip.
After that fire up the server apps on your remote server and fire up the client apps on your local server.
Conclusion
As you can see from the example above, .NET remoting is quite easy to be implemented and you can have your .NET Server application listened on TCP, HTTP or IPC Protocol. .NET Framework already has all the implementation for all that three protocol. The above example only discusses about TCP Protocol, however to change it to HTTP protocol and IPC protocol should be quite easy. You just need to use HTTPServerChannel or IPCServerChannel on your Server application and then Use HttpClientChannel or IPCClientChannel on your Client application
Download Source Code