WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 82 users


WorldofASP.NET >> General .NET >> General .NET

Basics of .NET Remoting

This article explores about .NET remoting,architecture and simple .NET remoting example and tutorials
Published Date : 03 Sep 2007
Author : James Douglas
Language : C#
Platform : .NET
Technology : Visual Studio
Views : 11338
Rating : (4 votes so far)



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




Other Related and Popular Articles :

Introduction to Regular Expression in .NET
Basic introduction about regular expression in .NET

Collections and Lists in .NET Framework
This article explains about the classes that you can use as Collections such as Array, ArrayList,Hashtable,SortedList

Manipulating and Editing Registry with .NET
This article teach you how to read, write and editing registry keys in .NET Framework


Author Profile : James Douglas

I work in a Software House Company in Malaysia (Kuala Lumpur) and I am MCP Certified in C# and Web Application course.
I originally started my programming in Java but later on changed to Microsoft platform because of the simplicity and ease of use.
I love .NET programming and am doing it almost every day now.

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

#how to stop / start IIS on selected remote server
15 May 2008 13:12 by : Natalie Gertsovich

I need do pass name of the server and stop/start IIS on selected server. Can any one show me how to do that? I am using windows forms application in C#.
I appreciate any help

#Why do we need to give reference of our remoteclass in the client applictaion
23 Apr 2008 2:37 by : Jigar Shah

Hi chang.....i have a doubt regarding teh remoting method that we use. after creating our "RemoteClass" we add its reference to our "RemoteServer" as well as to our "RemoteClient"...now my question is why do we need to do that? i mean if v hav to add its reference then rather then using remoting we can add the "RemoteClass" assembly to GAC n then directly use it in our "RemoteServer" as well as "RemoteCllient". so then why do we use remoting?

#.Net remoting and framework versions
24 Oct 2007 0:46 by : Meghana T

Unlike Web Services, .NET remoting requires both of your client and server objects running on the same .NET Framework version.

Is this something to do with the serialization mechanism supported by versions before .NET 2.0?
I would like to know whether the client and server objects incompatibility is still there with .NET 2.0?

Thanks !

#
02 Oct 2007 16:51 by : Tanner W.

I changed a little bit on this project so that the start/stop form had the remoteobjects class within the project. So now it is no longer using the DLL.

When I was still using the DLL, I could change the service name in the startIIS() or stopIIS() subs and still have the w3svc stop (instead of the correct service). I began to think that the GAC was storing my DLL and referencing it somewhere else without me knowing. So that is why I removed the DLL.

now when I try and start/stop something other than w3svc I get a "remoting exception was unhandled" thrown. Any suggestions??

 
#Re:
03 Oct 2007 18:44 by : James Douglas

The easiest way for troubleshooting your remoting objects is by telnetting
You can do telnet remoteserver port

So if your remoting objects is running on port 9090
Then you need to do telnet remoteserverip 9090
If it is connected then, there should be no problem in connecting to the remote server

#
01 Oct 2007 8:09 by : Tanner W.

The code works when using w3svc on a remote machine, but not with any other service.

my first comment problem was that the app was not listening and I resolved that. Now I just need to be able to pass a service name to it and stop those.

#how to use with something other than w3svc?
28 Sep 2007 15:41 by : Tanner W.

I got this to work local and on a remote server, now to add to it:

I've tried rebuilding the class to accept a parameter when .startIIS is called. That paramater is the service name. When I do this it throws an unhandled socket exception.

I also tried just changing the w3svc in the StartIIS/StopIIS call to something like spooler. And that will not work either.
------------------------------------------------------------
I have tried:

Public Sub StopIIS()
ExecuteProcess("net", "stop spooler")
End Sub

also:

Public Sub StopIIS(ByVal service as string)
ExecuteProcess("net", "stop " + service)
End Sub

Also:

Public Sub StopIIS()
ExecuteProcess("net", "'stop' + service")
End Sub

------------------------------------------------------------

None of these will produce what I am looking for. What am I missing here so that I can change this to be a very functional app? Must I change it somewhere else too?

 
#Re: how to use with something other than w3svc?
28 Sep 2007 20:19 by : James Douglas

Is your code work when you try to run net start w3svc?
I just want to make sure that you don't have permission issue when running the net command

#error
28 Sep 2007 12:11 by : Tanner W.

I got an unhandled socket exception when running this code.

Any advice? It said my machine actively refused the connection.

 
#Re: error
28 Sep 2007 20:17 by : James Douglas

Is your remote machine has IIS installed?
and also do you have firewall enabled on your remote machine?

You can detect that by telnet remotemachine 80
If it connected then you should have no problem in running the code

Leave New Comments


Article Content copyright by James Douglas
Everything else Copyright © by WorldofASP.NET 2010

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here







Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2010 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | ASP Hosting | ASP.NET Hosting | Phone Card | Calling Card |Stock Investing