Compartir a través de


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 Building a Host 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.

NoteNote

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

  1. Continuing on from How to: Build a Hosting Application, create a new directory under remoting called client. Create a configuration file for the client application as shown in the following code and save the file in the remoting\client directory. 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 RemotableType remote object can be found in the RemotableType 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.

    NoteNote

    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.

  2. 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 RemotableType and call its SayHello method. Save the client application as Client.cs or Client.vb in the remoting\client directory.

    NoteNote

    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());
       }
    
  3. Copy the RemotableType.dll assembly from remoting\Type into remoting\client.

    NoteNote

    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 the RemotableType.SayHello() method. If the remote object is being called the WriteLine occurs in the Listener process, if not the WriteLine occurs in the client process.

  4. 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
    
  5. 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 
    
  6. The listener command-prompt should look like this:

    C:\tmp\Remoting\listener>listener
    Listening for requests. Press Enter to exit...
    RemotableType.SayHello() was called!
    
  7. 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

Remoting Settings Schema

Concepts

Configuration of Remote Applications
Server Activation

Other Resources

Building a Basic .NET Framework Remoting Application