Remoting is used to build distributed applications and network services, accessible over TCP and HTTP. Remoting
is a complicated topic, so we suggest that you spend extra time studying the Remoting topics covered by the exam.
A Complete Remoting Example
Let's look at an example of a remote component that provides the time on the
server. Such a component could be used to synchronize clients with a central
server.
Server Class We'll need to first write the server class, which provides
the implementation of methods to be called remotely. As mentioned previously, a
component that is intended to be called remotely must derive from
MarshalByRefObject. A single method, GetServerTime(), will return the server
time, formatted as a string so that we can display it on the client. Here is
the complete sourcecode for our server component:
using
System;
namespace
RemotingExample
{
 //Remote Server Class
 publicclass
TimeServer
:
MarshalByRefObject
 {
   //Method to Be Called Remotely By Client
   publicstring
GetServerTime()
   {
     return
DateTime.Now.ToString();
   }
 } }
Server Wrapper
Once we have the Server class implemented, we need to provide a wrapper class
that registers a new channel for requests, and indicates the server class
containing the remote method implementations. [Note: If you are using Visual
Studio.NET, you will need to add a reference to the System.Runtime.Remoting
assembly.]
using
System; using
System.Runtime.Remoting; using
System.Runtime.Remoting.Channels.Tcp; using
System.Runtime.Remoting.Channels;
     //Wait for Connections
     Console.WriteLine("Waiting for connections. Press any key to exit.");
     Console.ReadLine();
   }
 } }
Client
A client calling a remote component needs to register a channel on it's end for the communication, and
then use the Activator object to get an instance of the component.
using
System; using
System.Runtime.Remoting; using
System.Runtime.Remoting.Channels.Tcp; using
System.Runtime.Remoting.Channels;
In order to run the example, we first must run the TimeServerWrapper server executable (TimeServerWrapper.exe)
to start listening for connections.
Next, in another command window, we run TimeClient.exe. Assuming the correct port numbers and object URIs are configured,
the client will display the time on the server.
TCP and HTTP Channel Protocols
There are two channels types that you can use: the TcpChannel, and the HttpChannel. The type
of channel you choose determines the underlying protocol used to transmit the remote method call.
In our previous example, we used the TcpChannel and in turn, the TCP protocol. In order to use
the HttpChannel, make the following changes:
Import the System.Runtime.Remoting.Channels.Http namespace
Create instances of HttpChannel in the client and in the server wrapper.
On the client, begin the URI location of the object with "http://"
Once you have a server wrapper that uses the HttpChannel, try this exercise. Start up the server
wrapper so that it is listening for connections. Now, open a browser, and enter in the location of
the service, as set by your port number and object URI, followed by ?WSDL. In a modified
version of our TcpChannel example, this would be "http://localhost:8765/TimeServerURI?WSDL". The
screenshot below shows the WSDL for our server component. If you think about it, a remotely activated
component over HTTP is the definition of a web service.