Building a Basic .NET Remoting Application 之三 Building a Client Application

Building a Client Application

To build a client of the remote type defined in Building 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 will intercept your client calls, forward them to the remote object, and return the results to your client. The following code example shows how to build a simple remoting client.

[C#]
// 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.StringMethod());
   }
}

 

To compile this class into a client or calling executable using the command-line tools that ship with the .NET Framework SDK, save it as Client.language-extension (or use another file name of your choice, where the language extension is the language you want to compile). Save the file in the same directory in which you saved a copy of the RemotableType.dll that you built in the Building a Remotable Type topic. (Note that this should not be the same directory as that of your Listener.exe application. If it is, you will not 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.) At the command prompt in that directory, type the following command:

[C#]

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

In this command, the file name is as follows:

[C#]

Client.cs

 

As you can see in the example, the Client class must be able to find the Client.exe.config file to load the configuration for RemotableType class. This file should be saved in the same directory as the Client.exe file, or the configuration file will not be found and an exception will be thrown. The following code example shows the Client.exe.config configuration file for this listening or host application domain.

 

<configuration>
   <system.runtime.remoting>
      <application>
         <client>
            <wellknown
               type="RemotableType, RemotableType"
               url="http://localhost:8989/RemotableType.rem"
            />
         </client>
      </application>
   </system.runtime.remoting>
</configuration>

This file tells the remoting system that the type information for the RemotableType remote object can be found in the RemotableType assembly, and that this client should attempt to create and use a RemotableType object located at http://localhost:8989/RemotableType.rem. 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.

 

posted on 2010-01-04 15:33  Acor  阅读(218)  评论(0编辑  收藏  举报

导航