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.
Note
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
remoting
calledclient
. Create a configuration file for the client application as shown in the following code and save the file in theremoting\client
directory. The file name should follow the pattern of app-name.exe.config. In this case, it is calledclient.exe.config
. The following configuration file tells the remoting system that the type information for theRemotableType
remote object can be found in theRemotableType
assembly and the object is located at https://localhost:8989/RemotableType.rem.<configuration> <system.runtime.remoting> <application> <client> <wellknown type="RemotableType, RemotableType" url="https://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 ofRemotableType
and call itsSayHello
method. Save the client application asClient.cs
orClient.vb
in theremoting\client
directory.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.
Public Shared Sub Main() RemotingConfiguration.Configure("Client.exe.config") Dim remoteObject As New RemotableType() Console.WriteLine(remoteObject.SayHello()) End Sub 'Main
public static void Main(){ RemotingConfiguration.Configure("Client.exe.config"); RemotableType remoteObject = new RemotableType(); Console.WriteLine(remoteObject.SayHello()); }
Copy the RemotableType.dll assembly from
remoting\Type
intoremoting\client
.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
Console.WriteLine
in theRemotableType.SayHello()
method. If the remote object is being called theWriteLine
occurs in the Listener process, if not theWriteLine
occurs in the client process.Compile client application by typing the following command in the
remoting\client
directory:vbc /r:RemotableType.dll Client.vb
csc /noconfig /r:RemotableType.dll Client.cs
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
RemotableType.SayHello()
.
Example
' Client.vb
Imports System
Imports System.Runtime.Remoting
Public Class Client
Public Shared Sub Main()
RemotingConfiguration.Configure("Client.exe.config")
Dim remoteObject As New RemotableType()
Console.WriteLine(remoteObject.SayHello())
End Sub 'Main
End Class 'Client
// Client.cs
using System;
using System.Runtime.Remoting;
public class Client{
public static void Main(){
RemotingConfiguration.Configure("Client.exe.config");
RemotableType remoteObject = new RemotableType();
Console.WriteLine(remoteObject.SayHello());
}
}
See Also
Tasks
How to: Build a Hosting Application
Reference
Concepts
Configuration of Remote Applications
Server Activation