Webcast.NET Remoting学习笔记(5)使用配置文件

使用配置文件
使用配置文件主要有三点好处:
1 简化代码,将配置相关的代码写到配置文件中,程序的逻辑处理更清晰。
2 便于更改配置,项目的配置有可能会产生变化,例如已经注册的通道端口被占用等等,有了配置文件就可以方便的统一修改。
3 配置文件采用统一的标准,易于大家理解
下面举一个例子来简单说明配置文件的内容
<configuration>
    
<system.runtime.remoting>
        
<application>
            
<service>
                
<wellknown 
                    mode
="Singleton" 
                    type
="RemotingSamples.HelloServer, General" 
                    objectUri
="SayHello" />
            
</service>
            
<channels>
                
<channel port="8086" ref="http"/>
            
</channels>
            
<!--leaseManagerPollTime="7S"-->
            
<lifetime 
               leaseTime
="7M" 
               sponsorshipTimeout
="7M" 
               renewOnCallTime
="7M" 
               
/>
        
</application>
    
</system.runtime.remoting>
</configuration>
<service>可以看出这是一个服务器端的配置文件,wellknown说明对象激活方式  mode="Singleton"说明采用单一实例的激活方式,type="RemotingSamples.HelloServer, General"中 RemotingSamples为名字空间,HelloServer为类名,General为程序集,也就是dll文件名,objectUri="SayHello"指出了这个对象的地址。<channels><channel port="8086" ref="http"/></channels>为通道的注册,其中ref表示此配置引用其它配置文件,即machine.config。
客户端的配置文件基本相似
<configuration>
    
<system.runtime.remoting>
        
<application>
            
<client>
                
<wellknown type="HelloServer, General" url="http://localhost:8086/SayHello" />
            
</client>
            
<channels>
                
<channel ref="http" port="0"></channel>
            
</channels>
        
</application>
    
</system.runtime.remoting>
</configuration>
这样,在程序中就可以直接引用配置文件来配置
客户端
                 RemotingConfiguration.Configure(@"client.exe.config");
                 HelloServer obj2 = new HelloServer();
             注意这里的new并非是调用HelloServer的构造函数,而是创建一个它的本地代理
服务器端
RemotingConfiguration.Configure("server.exe.config")
实际在解决方案中配置文件为App.config,当项目生成时他会自动转化为生成的exe文件的名字加上.config,但是有时候我们不知道项目最终生成的名字是什么,我们就可以用
 RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
);来实现,通过它我们就可以获取当前应用程序域的配置文件名称

posted on 2007-02-05 17:12  stuhrbeu  阅读(325)  评论(0编辑  收藏  举报

导航