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.

 

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值