To create the client program, go to File->New->Project and choose a C# Console Application named RemotingExampleClient.
IRemotingExampleService resService =(IRemotingExampleService)Activator.GetObject( typeof(IRemotingExampleService), "tcp://localhost:9988/RemotingExampleService"); Console.WriteLine("RESUME:\n"+ resService.GetFormattedResume());
Notice that we're using the IRemotingExampleService here instead of RemotingExampleService? That is because our client side code knows only about the interface, not the implementation the server is using. The result of this is the server is serving "RemotingExampleService", but our client is using "IRemotingExampleService".
IResume aResume = resService.GetResume(); Console.WriteLine("NAME:"+ aResume.GetFormattedName()); Console.WriteLine("RESUME:"+ aResume.GetFormattedResume());
This section of code makes use of the instance of IRemotingExampleService to return yet another interface. Here we don't have to make any calls to the Activator object because the .NET runtime accesses the server's Resume implementation transparently using the IResume interface.
To make this code work, we will need to add a referance to both our RemotingExampleInterfaces and System.Runtime.Remoting dlls as we did in step 2. Without this you will get compiler errors.
The complete code listing is below. For more information on the Activator object, see my article titled "Introduction to .NET Remoting" on this site.
using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using RemotingExampleInterfaces; namespace RemotingExampleClient { class RemotingExampleClient { static void Main(string[] args) { ChannelServices.RegisterChannel(new TcpClientChannel()); IRemotingExampleService resService =(IRemotingExampleService)Activator.GetObject( typeof(IRemotingExampleService), "tcp://localhost:9988/RemotingExampleService"); Console.WriteLine("RESUME:\n"+ resService.GetFormattedResume()); IResume aResume = resService.GetResume(); Console.WriteLine("NAME:"+ aResume.GetFormattedName()); Console.WriteLine("RESUME:"+ aResume.GetFormattedResume()); Console.WriteLine("Press any key to continue..."); Console.Read(); }//END OF MAIN METHOD }//END OF RemotingExampleClient class }//END OF RemotingExampleClient namespace
Compile this project and note the location of the .EXE file.
Comments