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.

 

转载于:https://www.cnblogs.com/MayGarden/archive/2010/01/04/1638943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值