Remoting学习之配置篇

     
      使用配置文件的好处是什么?很简单,他可以简化代码,可以随时更改,通道,端口,URL的设置不需要重新编译就可以运行。所以在实际项目中经常采用这种方式。
怎么写一个服务器端的配置文件?
下面举个例子:
<configuration>
  <system runtime remoting>   ///配置的都是与remoting有关的内容
    <application>  ///可以包含多个application
       <serive>//表示在我的一个程序中注册了一个service
        <wellknown mode="Singleton" ObjectUri="HelloNewegg"  ///ObjectUri为访问这个对象的地址
         type="Newegg.RAM,Accounting"/>   ///type表示对象的类型;Newegg.RAM表示这个类的名字;
                                                                   ///Accounting表示程序集,实际表现为一个DLL
        </serive>
        <channels>//可以在里面注册多个通道
             <channel port="8888" ref="http"/>  ///ref表示引用了machine.config本身http不是在这个配置文件里配置的
        </channels>
    </application>
  </system runtime remoting>
</configuration>
怎么写一个客户器端的配置文件?
<configuration>
  <system runtime remoting> ///配置的都是与remoting有关的内容
    <application>  ///可以包含多个application
       <client>//示在我的一个程序中注册了一个service
        <wellknown  ObjectUri="http://localhost:8888/HelloNewegg"
         type="Newegg.RAM,Accounting"/>  
                                         
        </client>
        <channels>
             <channel port="0" ref="http"/> //port="0"什么意思?表示客户端不用探听任何端口。
        </channels>
    </application>
  </system runtime remoting>
</configuration>

使用配置文件的代码怎么写??
服务器端:RemotingConfiguration.Configure("NeweggServer.exe.config");///NeweggServer表示服务器端的工程名
          有时候也这样写:
            string cfg=AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            RemotingConfiguration.Configure(cfg);
客户端:RemotingConfiguration.Configure("NeweggClient.exe.config");///NeweggClient表示客户器端的工程名
           RAM obj=new RAM();

1,如下是Server端典型的Remoting配置文件:
<? xml version ="1.0" encoding ="utf-8" ?>
< configuration >
 <system.runtime.remoting>
    <application>
      <channels>
        <channelref="http"/>
      </channels>
      <service>
        <wellknownmode="Singleton"
                    type ="ComponentHost.CustomerManager, ComponentHost"
                   objectUri ="CustomerManager.soap" />
      </service>
 
     </application>
   </system.runtime.remoting>
</ configuration >
 
1 )当 Remote Objects 部署在 Console/Windows Form Windows Services 下时(上面的配置文件 channel 需要设置 port 属性),相应 Server 端声明 Remote Objects 的代码可以简化为:
string filename = "server.exe.config";
RemotingConfiguration.Configure(filename);
 
2 )如果 Remote Objects 部署在 IIS 时,根本就不需要任何代码声明。但是需要将上述配置文件命名为: web.config ,并且将 Remote Objects DLL 文件安置在 web application BIN 文件夹。
 
一般在实际应用中,基本上将 Remote Objects 部署在 IIS 环境中,好处是( I )不需要编写额外的代码;( II )只要启动机器,远程对象就启动了。不需要你半夜三更跑到公司去登录,然后启动发生故障的远程服务;( III )容易与 IIS 认证服务进行集成;( IV )可能还有更多优点,我现在没有想到。
 
3 )如果需要声明多个远程对象,只需要在 < service > </ service > 之间添加相应的 Remote Objects 配置信息即可。
 
4 )另外需要注意 type 属性为: <namespace>.<class>, <assembly>
 
2,如下是Client端典型的配置文件:
<? xml version ="1.0" encoding ="utf-8" ?>
< configuration >
 <system.runtime.remoting>
    <application>
 
      <client>
        <wellknowntype="ComponentHost.CustomerManager, RemotingTest"
                   url ="http://localhost/ComponentHost/CustomerManager.soap" />
      </client>
 
    </application>
 </system.runtime.remoting>
</ configuration >
 
要注意 type 属性的设定: <namespace>.<class>, <assembly>
如果 Client 通过 SoapSuds 产生 Remote Objects 的元数据 assembly ,或者是 Shared Assembly (如 Interface Abstract Class ),这里 <assembly> 则为上述 assembly 的名称。
如果是通过 SoapSuds 产生 Source code ,则 <assembly> Client 应用程序名(无 exe 后缀)。
 
同时, Client application 调用 Remote Objects 时,可以省掉:注册通道、 Activator.GetObject()/RemotingConfiguration.RegisterActivatedServiceType() 等代码,取而代之的代码为:
string filename = “clientApplication.exe.config”;
RemotingConfiguration.Configure(filename);
下面通过 new 来创建 Remote Object 实例。
 
3,标准的.Net Remoting Configuration配置文件
MSDN 中有 .Net Remoting Configuration file 中全部元素 / 属性的完整的详细说明,需要的时候再查阅了。一般情况下,知道下面这些属性就够用了。
<configuration>
   <system.runtime.remoting>
      <application>
        <lifetime /> ―― 配置 Remote Objects 生存期的信息
        <channels /> ―― 配置与远程对象进行通信的信道
        <service />
        <client />
      </application>
   </system.runtime.remoting>
</configuration>
 
简单说明:
1 <service> ―― 仅在 Server 端配置
      <service>
          <wellknown /> ―― 配置要发布的 SAO (已知)对象的信息
          <activated /> ―― 配置要发布的 CAO 客户端激活对象的信息
      </service>
 
 
2 <client> ―― 仅在 Client 端配置,与 Server <service> 对应
         <client>
            <wellknown />
            <activated />
        </client>
 
When using CAOs, the <client> property has to specify the URI to the server for all underlying <activated> entries.
Note When using CAOs from more than one server, you have to create several <client> properties in your configuration file.
当调用 CAO 远程对象时,必须设定 <client> url 属性。如果 CAO 来自不同的 Server ,则需要在配置文件中定义多个 <client> 。如下所示:
     <client url="http://localhost/MyServer>
        <activated type="Server.MyRemote, Client" />
      </client>
 
4,定制Client/Server Channel元素
1 Client Side
< channel ref ="http">
    < clientProviders >
          < formatter ref ="binary" />  
    </ clientProviders >
</ channel >
其中, formatter ref=”binary” or “soap” formatter ref 指要在通道上发送的消息格式,在此示例中为二进制,以增强性能。
 
2 Server Side
< channel ref ="http">
    < serverProviders >
          <providerref="wsdl"/>
          <formatterref="binary"typeFileterLevel="Full"/>
          <formatterref="soap"typeFileterLevel="Full"/>
    </ serverProviders >
</ channels >   
typeFilterLevel 表示当前自动反序列化级别,支持的值包括 Low (默认值)和 Full
1. Ingo Rammer, Advanced .Net Remoting.
2. MSDN




Reference:
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值