An Introduction To .NET Remoting - The Remote Client
(Page 4 of 5 )
The ResumeClient object is the test user of our newly created ResumeSuperServer remote object. To start this project go to File->New->Project. Choose a Console Application as the application type and enter "ResumeClient" as the project's name. As in step 2, make sure you add a reference to our shared DLL created in step 1 and the System.Runtime.Remoting DLL.
The code shown below has two lines of particular interest to .NET remoting. The first line creates a TCP client channel. This channel is not bound to a port. The second line actually gets a reference to our remote ResumeLoader object. The Activator.GetObject method returns a type of Object that we can then cast into our ResumeLoader.
The parameters we pass in are extremely similar to what we passed to the RemotingConfiguration object for the server project. The first parameter is the type of the object. The second is the URI of our remote object:
ChannelServices.RegisterChannel(new TcpClientChannel());
ResumeLoader loader = (ResumeLoader)Activator.GetObject(
typeof(ResumeLoader), "tcp://localhost:9932/ResumeLoader"); The complete code for our ResumeClient is shown below:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using DotNetRemoteTest;
namespace ResumeClient
{
public class ResumeClient
{
public static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel());
ResumeLoader loader = (ResumeLoader)Activator.GetObject(
typeof(ResumeServer), "tcp://localhost:9932/ResumeLoader");
if(loader==null)
{ Console.WriteLine("Unable to get remote referance"); }
else
{
Resume resume = loader.GetResumeByUserID(1);
Console.WriteLine("ResumeID:"+ resume.ResumeID);
Console.WriteLine("UserID:"+ resume.UserID);
Console.WriteLine("Title:"+ resume.Title);
Console.WriteLine("Body:"+ resume.Body);
}
Console.ReadLine();//Keep the window from closing before we can read the result.
}//END OF MAIN METHOD
}//END OF ResumeClient Object
}//END OF ResumeClientNamespace Compile this project and note the location of the executable.
Testing It Out Create a table in your database with the following basic schema:
Table Name-Resume
ResumeID, numeric (autonumber)
UserID, numeric
Title, Char(30)
Body, Text Double click on the server executable that we created in step 2 and then double click on the client executable that we created in step 3. If everything works out then you should see the row from the database that has a ResumeID of 1.
Next: Conclusion >>
More C# Articles
More By David Talbot