zoukankan      html  css  js  c++  java
  • Building a Basic .NET Remoting Application 之二 Building a Host Application

    Building a Host Application

    By itself, the RemotableType class defined in the Building a Remotable Type topic is not special. To enable objects in other application domains to create instances of this object remotely, you must build a host or listener application to do two things:

    • Choose and register a channel, which is an object that handles the networking protocols and serialization formats on your behalf.
    • Register your type with the .NET remoting system so that it can use your channel to listen for requests for your type.

    The .NET Framework includes two default channels, HttpChannel (which uses SOAP formatting) and TcpChannel (which uses binary formatting). HttpChannel is a good channel to start with because in some scenarios it can be used through firewalls without opening a port, and it supports standard security and authentication protocols. For more information about choosing channels that suit your scenario, see Channels.

    You can build listener applications using any type of application domain — a Windows Forms application, an ASP.NET Web application, a console application, a Windows Service (also known as a Windows NT Service), or any other managed application domain. Because remote configuration is done on a per-application-domain basis, the application domain must be running to listen for requests.

    Note   Unlike COM, remoting does not start the host or server application for you. This is an important difference between .NET remoting and remote activation in COM.

    Configuration can be done programmatically or by using an application or machine configuration file. The following code implements a simple RemotableType host application domain that uses a configuration file. (Using a configuration file enables you to change the remoting configuration without recompiling your executable, among other things.) For details on the configuration of the .NET remoting infrastructure, see Configuration.

    [C#]
    // Listener.cs
    using System;
    using System.Runtime.Remoting;

    public class Listener{
       public static void Main(){
          RemotingConfiguration.Configure("Listener.exe.config");
          Console.WriteLine("Listening for requests. Press Enter to exit...");
          Console.ReadLine();
       }
    }

    To compile this class into a host or listener executable using the command-line tools that ship with the .NET Framework SDK, save it as Listener.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 the RemotableType.dll that you built in the Building a Remotable Type topic. At the command prompt in that directory, type the following command:

    [C#]

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

    In this example, the file name is:

    [C#]

    Listener.cs

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

    <configuration>
       <system.runtime.remoting>
          <application>
             <service>
                <wellknown
                   mode="Singleton"
                   type="RemotableType, RemotableType"
                   objectUri="RemotableType.rem"
                />
             </service>
             <channels>
                <channel ref="http" port="8989"/>
             </channels>
          </application>
       </system.runtime.remoting>
    </configuration>

    The remoting system uses the information in this file to listen for and route remote requests to an instance of a remotable type. The file specifies the Singleton server-activation mode, the type name and assembly of the type on behalf of which it is to listen, and the object Uniform Resource Identifier (URI) or external name of the object. (For more details about object URIs and remoting, see Activation URLs.) The file also tells the remoting system to listen for requests on port 8989 using the system-provided HttpChannel.

    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.
  • 相关阅读:
    帧锁定同步算法
    为 Raft 引入 leader lease 机制解决集群脑裂时的 stale read 问题
    etcd:从应用场景到实现原理的全方位解读
    给定一个二叉搜索树(BST),找到树中第 K 小的节点
    UDP如何实现可靠传输
    理解TCP/IP三次握手与四次挥手的正确姿势
    Redis持久化
    Redis提供的持久化机制(RDB和AOF)
    redis渐进式 rehash
    redis rehash
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1638921.html
Copyright © 2011-2022 走看看