• 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.
  • 相关阅读:
    webapi 获取json数据
    js的匿名函数与自定义函数
    深入理解计算机系统 第八章 异常控制流(2)
    深入理解计算机系统 第八章 异常控制流(1)
    深入理解计算机系统 第三章 程序的机器级表示(2)
    深入理解计算机系统 第三章 程序的机器级表示(1)
    深入理解计算机系统 第二章 信息的表示和处理(2)
    深入理解计算机系统 第二章 信息的表示和处理(1)
    深入理解计算机系统第一章,计算机系统漫游
    Leetcode练习(Python):第292题:Nim 游戏:你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1638943.html
Copyright © 2020-2023  润新知