An Introduction To .NET Remoting - The Server Object
(Page 3 of 5 )
There are actually several ways to create the server object. The most straightforward method 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 references to the required DLLs that will make this program work. Go to Project->Add Reference, and add a reference to the DLL that we created in step 1 by clicking the "Browse" button.
In order to use the .NET remoting functionality, you must add a reference to the DLL using Project->Add Reference. Under the .NET tab, choose the System.Runtime.Remoting.DLL and click OK.
Next you will need to add a reference to System.Runtime.Remoting.dll in the same way we did in step 1. The object is shown below and is fairly simple and straightforward. I'll explain each of the 3 lines of code that really matter to .NET remoting in the following paragraphs.
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're working with a local network connection then it's best to use TCP because of it's enhanced performance over using HTTP.
If you're working over the Internet then 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 your 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 parameters 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 shown 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.
Next: The Remote Client >>
More C# Articles
More By David Talbot