现在的位置: 首页 > 综合 > 正文

net Remoting(4)——配置文件

2012年03月11日 ⁄ 综合 ⁄ 共 1456字 ⁄ 字号 评论关闭

RemotingConfiguration类进行类型注册时,可以采用程序配置方式,也可以通过配置文件来进行。这个类有一个Configure方法:

public static void Configure(

    string filename,

    bool ensureSecurity

)

Filename就就文件名(配置文件的文件名),第二个参数用于选择安全性的启用与否

 

在服务端激活下用配置文件来实现:

在控制台应用程序下添加SelfService.config文件(一个文件),添加以下内容:

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.runtime.remoting>

    <application name="ServerRemoting">

      <service>

        <wellknown mode="Singleton"

type="SelfRemote.selfRemoteObject,SelfRemote"

objectUri="selfRemoteObject"/>

      </service>

      <channels>

        <channel ref="http" port="10001"/>

      </channels>

    </application>

  </system.runtime.remoting>

</configuration>

 

在这个文件中,通道注册,类型注册与程序中对应。然后把此文件的 复制到输出目录 设置为 始终复制(为的就是在生成的控制台应用程序同级目录,能够找得到这个配置文件)。

 

然后,在控制台可以:

Console.WriteLine("http 通道remoting服务开始……");

RemotingConfiguration.Configure("SelfService.config", false);

onsole.Read();

 

这样就可以了。

 

在客户端,如果采用客户端激活方式,那么同上差不多,同时也要保证在程序同级目录能找到文件(始终复制):

新建立ClientService.config文件,文件内容为:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.runtime.remoting>

    <application name="ServerRemoting">

      <service>

        <wellknown mode="Singleton"

type="SelfRemote.selfRemoteObject,SelfRemote"

objectUri="selfRemoteObject"/>

      </service>

      <channels>

        <channel ref="http"/>

      </channels>

    </application>

  </system.runtime.remoting>

</configuration>

 

这里的channel不要再指定端口。

在测试中:

RemotingConfiguration.Configure("ClientService.config",false);

selfRemoteObject app1 = new selfRemoteObject();

Assert.AreEqual(0, app1.Unid);

 

这样就可以了。

 

抱歉!评论已关闭.