[Remoting专题系列] 十二:配置文件

使用配置文件替代硬编码可使应用程序拥有更高的灵活性,尤其是对分布式系统而言,意味着我们可以非常方便地调整分布对象的配置。Remoting 的配置文件比较简单,详细信息可以参考 MSDN。 

ms
-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/dv_fxgenref/html/52ebd450-de87-4a87-8bb9-6b13426fbc63.htm

下面是个简单的例子,包含了 SAO 
/ CAO 的配置样例。

Server.cs
BinaryClientFormatterSinkProvider cbin 
= new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider sbin 
= new BinaryServerFormatterSinkProvider();
sbin.TypeFilterLevel 
= TypeFilterLevel.Full;

Hashtable properties 
= new Hashtable();
properties[
"port"= 801;

TcpChannel channel 
= new TcpChannel(properties, cbin, sbin);
ChannelServices.RegisterChannel(channel, 
false);

RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Data), "data", WellKnownObjectMode.Singleton);
RemotingConfiguration.ApplicationName 
= "test";
RemotingConfiguration.RegisterActivatedServiceType(
typeof(Data2));

Client.cs
TcpChannel channel 
= new TcpChannel();
ChannelServices.RegisterChannel(channel, 
false);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(Data), "tcp://localhost:801/data");
RemotingConfiguration.RegisterActivatedClientType(
typeof(Data2), "tcp://localhost:801/test");

Data data 
= new Data();
data.Test();

Data2 data2 
= new Data2();
data2.Test();

改成对应的配置文件,就是下面这个样子。

Server.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.runtime.remoting>
    
<application name="test">
      
<channels>
        
<channel ref="tcp" port="801">
          
<clientProviders>
            
<formatter ref="binary"/>
          
</clientProviders>
          
<serverProviders>
            
<formatter ref="binary" typeFilterLevel="Full" />
          
</serverProviders>
        
</channel>
      
</channels>
      
<service>
        
<wellknown mode="Singleton" type="Learn.Library.Remoting.Data, Learn.Library" objectUri="data" />
        
<activated type="Learn.Library.Remoting.Data2, Learn.Library" />
      
</service>
    
</application>
  
</system.runtime.remoting>
</configuration>

Client.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.runtime.remoting>
    
<application>
      
<channels>
        
<channel ref="tcp">
          
<clientProviders>
            
<formatter ref="binary"/>
          
</clientProviders>
        
</channel>
      
</channels>
      
<client url="tcp://localhost:801/test">
        
<wellknown type="Learn.Library.Remoting.Data, Learn.Library" url="tcp://localhost:801/data" />
        
<activated type="Learn.Library.Remoting.Data2, Learn.Library" />
      
</client>
    
</application>
  
</system.runtime.remoting>
</configuration>

Server.cs
RemotingConfiguration.Configure(
"server.config"false);

Client.cs
RemotingConfiguration.Configure(
"client.config"false);

Data data 
= new Data();
data.Test();

Data2 data2 
= new Data2();
data2.Test();
[最后修改由 yuhen
posted on 2007-06-05 09:09  编程山人  阅读(303)  评论(0编辑  收藏  举报