Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
.NET Remoting
 How to: Build a Client Application
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Developer's Guide 
How to: Build a Client Application 

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 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.

    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.

    Visual Basic
       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:

    Visual Basic
    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

Visual Basic
' 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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Does this work?      Franklin Ribeiro   |   Edit   |   Show History
Raise a hand who was able to make this example to work!
Tags What's this?: Add a tag
Flag as ContentBug
Yes. This works.      Derek Morin   |   Edit   |   Show History

Yes. This works. Here is a batch file to help with the build. (Assumes using the Visual Studio 2005 command prompt, starting at the directory "remoting") Save the following as build.bat in the remoting directory.

cd type
csc /noconfig /t:library RemotableType.cs
cd ..
cd client
xcopy /y ..\type\RemotableType.dll
cd ..
cd listener
xcopy /y ..\type\RemotableType.dll
cd ..

cd listener
csc /noconfig /r:RemotableType.dll Listener.cs
cd ..

cd client
csc /noconfig /r:RemotableType.dll Client.cs
cd ..

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker