.net remoting的配置有两种形式,一种为编程配置,一种为配置文件配置。

       配置文件配置即把与.net remoting有关的信息(如信道类型、端口号)存储在web.config或app.config文件中。.net框架提供并规定了有关.net remoting的设置。下面是服务器端与客户端的两个简单的文件配置:

服务器端

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting><!--.net remoting配置的根节点-->
    <application><!--包含有关远程应用程序使用及公共的对象信息-->
      <service><!--服务器端,用于指定以什么方式公开什么对象-->
        <wellknown mode="Singleton" type="MyRemoting.Type.ServerMachineInfo,RemotingType" objectUri="ServerMachineInfo"/>
        <!--与wellknown相对是activated,wellknown表示服务器端激活对象,后者为客户端激活对象-->
        <!--mode表示如何响应客户端请求,Singleton表示单一实例-->
        <!--type表示要公开的类型,选择指定类型名称(含名称空间),然后指定类型所属.dll文件-->
        <!--objectUri表示远程对象访问路径-->
      </service>
      <channels><!--用于指定通道信息,可心同时指定多个通道-->
        <channel port="8001" ref="http"/><!--具体的通道信息,port为端口号,ref为引用的通道类型-->
        <!--.net 框架提供了http与tcp通道-->
      </channels>
    </application>
</system.runtime.remoting>
</configuration>

客户端

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
    <application>
      <client>
        <wellknown url="
http://172.16.28.151:8001/ServerMachineInfo" type="MyRemoting.Type.ServerMachineInfo,RemotingType"/>
      </client>
        <!--远程对象访问路径,域名与IP地址都可以-->
        <channels>
          <channel port="0" ref="http"/>
          <!--port为0表示客户端不侦听任何端口-->
        </channels>
    </application>
</system.runtime.remoting>
</configuration>

       采用配置文件来配置.net remoting程序,其好处在于可使.net remoting特有的配置与具体代码分开。采用了这种方式来配置.net remoting程序,代码在书写的时候与以编程方式配置.net remoting是不一样。下面是具体的一个示例:

服务器端

1、建立名称为RemotingServer的控制台应用程序

2、添加对RemotingType.dll文件与System.Runtime.Remoting.dll文件的引用(有关RemotingType.dll,请查看.net remoting应用程序建立(-)远程对象类型建立)。

3、主程序代码如下:

using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using MyRemoting.Type;


namespace RemotingServer
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,false);
            Console.WriteLine("Server started!Press any key to exit...");
            Console.ReadLine();
            return;
        }
    }
}

客户端:

1、建立名称为Client的控制台应用程序

2、添加对RemotingType.dll文件与System.Runtime.Remoting.dll文件的引用(有关RemotingType.dll,请查看.net remoting应用程序建立(-)远程对象类型建立)。

3、主程序代码如下:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using MyRemoting.Type;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);
            ServerMachineInfo SMschine = new ServerMachineInfo();
            Console.WriteLine(SMschine.MachineName);
            Console.ReadLine();
        }
    }
}

posted on 2009-10-07 17:25  Myhsg  阅读(512)  评论(0编辑  收藏  举报