Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 77,097 times

Contents

Related Categories

Introducing .NET Remoting - The server object

David Talbot

The server object

There are actually several ways to create the server object, the most straightforward is described below. In Visual Studio.NET, Click on File->New Project. Choose a Command Line Application and name it ResumeSuperServer.

First things first, we need to add referances to the required DLLs that will make this program work. Go to Project->Add Referance, and add a referance to the DLL that we created in Step1 by clicking the "Browse" button.

In order to use the .NET remote functionality, you must add a referance to the DLL using Project->Add Referance. Under the .NET tab, choose System.Runtime.Remoting.DLL and click on OK. Side note, to the authors of the .NET books out there that will remain nameless--Details like this are extremely important to someone trying to compile a .NET Remoting "hello world" :).

Next you will need to add a referance to System.Runtime.Remoting.dll the same way we did in Step 1.

The object below is fairly simple and straight forward. Below I'll explain each of the 3 lines of code that really matter to .NET remoting.

The TcpServerChannel is one of the two types of channels supported by .NET remoting. This will set up the port number we want our object to respond to requests on, and the ChannelServices.RegisterChannel will bind that port number to the TCP/IP stack on the operating system.

TcpServerChannel channel = new TcpServerChannel(9932);
ChannelServices.RegisterChannel(channel);

The other type of channel that can be set up is HTTP and is done simply by using the HttpServerChannel object in the System.Runtime.Remoting.Channels.Http namespace. The differances between using an HTTP and a TCP channel can be summed up simply--If you are working with a local network connection it's best to use TCP because of it's enhanced performance over using HTTP. If you're working over the internet HTTP can sometimes be the only choice depending on firewall configurations. Keep in mind that if you do have control over the firewall, almost all firewalls can be configured to allow TCP traffic through on the port you've chosen to use for you object in addition to the DNAT you'll likely need to employ in most situations. If you don't know how to alter these rules, ask your system administrator.

RemotingConfiguration.RegisterWellKnownServiceType(typeof(ResumeLoader), 
	"ResumeLoader", WellKnownObjectMode.SingleCall);

This line sets a few prameters on your service and binds the object you want to the name you want to use on this remote object. The first parameter is the object you're binding, typeof(ResumeLoader). The second parameter is the String that is the name for the object on the TCP or HTTP channel. For example, remote clients would refer to the above object as "tcp://localhost:9932/ResumeLoader". The third parameter tells the container what should be done with the object when a request for the object comes in. WellKnownObjectMode.Single call makes a new instance of the object for each client while WellKnownObjectMode.Singleton uses one instance of the object for all callers.

The complete code for the object is below.

using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data.SqlClient;
using DotNetRemoteTest;

namespace ResumeServerServer
{
	public class ResumeSuperServer
	{
		public static void Main(String[] args)
		{
			TcpServerChannel channel = new TcpServerChannel(9932);
			ChannelServices.RegisterChannel(channel);
			RemotingConfiguration.RegisterWellKnownServiceType(typeof(ResumeLoader),
					"ResumeLoader", WellKnownObjectMode.SingleCall);
			System.Console.WriteLine("Press Any Key");
			System.Console.ReadLine();
		}
	}
}

Compile this program and note the location of the generated .EXE file.

David Talbot is an experienced Software Architect with a diverse background including creating network applicances, working with television set top boxes, building Billing/CRM systems, Web Portals and more. He has also provided technical guidance in different capacities on two C# books. David is currently finishing up work on a real estate analytics application in C# for Pathfinder Technologies and seeking additional contract or permenet work.

Comments

  • Posted by scotlandhoy on 06 Dec 2005


    forget that last post ...

    I found it in my code ... non-application test code I threw in on the quick ... cause the StackOverflow.

  • Posted by scotlandhoy on 05 Dec 2005


    Hey, did you ever find out what your problem was?

    I got the same error on a completely different project.

    Same M.O. ...

    ... unhandled exception of type 'System.StackOverflowException' occur...

  • Problem running ResumeClient

    Posted by JLChew on 16 Aug 2005

    I encountered the following error message when running the ResumeClient.exe:

    "[red] An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll[/red] "

    at the followi...

  • Great Tutorial!!

    Posted by priyajay on 25 May 2004

    I was looking for a Remoting Sample for beginners for weeks and this was the first that made absolute sense to me. If you are a person with an average IQ like myself, and want to understand the basics...

  • Excellent

    Posted by NutSoft on 30 Jan 2004

    I've been looking for a way to implement a cross application boundary component in C# .NET for most of this week. This looks like it will suit my needs perfectly. Thanks.