惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
G
Google Developers Blog
D
Docker
The GitHub Blog
The GitHub Blog
H
Help Net Security
WordPress大学
WordPress大学
博客园_首页
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
罗磊的独立博客
I
InfoQ
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Jina AI
Jina AI
J
Java Code Geeks
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News

博客园 - 编程山人

品牌大全 控制只生成一个子窗体(简单) C#类中的get 和set 函数的具体用法 What's TM? 在C#中利用SharpZipLib进行文件的压缩和解压缩 在C#中运用SQLDMO备份和恢复Microsoft SQL Server数据库 WinForm实现SQLServer存储图片 通过C#调用CHM帮助文件 [Remoting专题系列] 一:.NET Remoting [Remoting专题系列] 二:远程对象 [Remoting专题系列] 三:激活模式 [Remoting专题系列] 四:生存期租约 [Remoting专题系列] 五:信道 [Remoting专题系列] 六:异步调用 [Remoting专题系列] 七:调用上下文 [Remoting专题系列] 八:元数据 [Remoting专题系列] 九:动态发布 [Remoting专题系列] 十:追踪服务 [Remoting专题系列] 十一:事件
[Remoting专题系列] 十二:配置文件
编程山人 · 2007-06-05 · via 博客园 - 编程山人

使用配置文件替代硬编码可使应用程序拥有更高的灵活性,尤其是对分布式系统而言,意味着我们可以非常方便地调整分布对象的配置。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