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.