How to: Build a Client Application
This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF).
To build a client of the remote type defined in How to: Build a Remotable Type and hosted by the application created in How to: Build a Hosting Application, your application must register itself as a client for that remote object and then invoke it as though it were within the client's application domain. The .NET remoting system intercepts your client calls, forwards them to the remote object, and returns the results to your client. The following procedure describes how to build a basic remoting client.
|See How to: Compile and Run a Basic Remoting Application for complete instructions on how to build and run this sample.|
To build a basic remoting client
Continuing on from How to: Build a Hosting Application, create a new directory under
client. Create a configuration file for the client application as shown in the following code and save the file in the
remoting\clientdirectory. The file name should follow the pattern of app-name.exe.config. In this case, it is called
client.exe.config. The following configuration file tells the remoting system that the type information for the
RemotableTyperemote object can be found in the
RemotableTypeassembly and the object is located at http://localhost:8989/RemotableType.rem.
<configuration> <system.runtime.remoting> <application> <client> <wellknown type="RemotableType, RemotableType" url="http://localhost:8989/RemotableType.rem" /> </client> </application> </system.runtime.remoting> </configuration>
For details about the URL attribute in this configuration file, see Activation URLs. If you want to run this application over a network, you must replace localhost in the client configuration with the name of the remote computer.
Note: Although there are only a few settings in the preceding configuration file, most of the problems using .NET remoting occur because some of these settings are either incorrect or do not match the configuration settings for client applications. It is easy to mistype a name, forget a port, or neglect an attribute. If you are having problems with your remoting application, check your configuration settings first.
Create a new source file in the language of your choice. In the main method, call RemotingConfiguration.Configure passing in the name of the client configuration file (
client.exe.config). Next, instantiate an instance of
RemotableTypeand call its
SayHellomethod. Save the client application as
Note: The client application should not be saved in the same directory as that of your Listener.exe application. If it is, you cannot be certain that you are receiving and making use of a remote reference, because assembly and type resolution can occur when applications are in the same directory.
Copy the RemotableType.dll assembly from
Note: A common question at this point is "How do I know that the remote object gets called if I am copying the assembly to the client?" This is exactly why we added the call to
RemotableType.SayHello()method. If the remote object is being called the
WriteLineoccurs in the Listener process, if not the
WriteLineoccurs in the client process.
Compile client application by typing the following command in the
Open up two command prompts. In one, go to the remoting\listener directory and run listener.exe. In the other, go to the remoting\client directory and run client.exe. The client command-prompt should look like this:
C:\tmp\Remoting\client>client Hello, world
The listener command-prompt should look like this:
C:\tmp\Remoting\listener>listener Listening for requests. Press Enter to exit... RemotableType.SayHello() was called!
You can see by the output of the listener that it received a call to