您的位置:首页 > 大数据 > 人工智能

[Remoting] 十二:配置文件(收藏转帖http://www.rainsts.net/article.asp?id=420)

2008-03-26 14:21 417 查看
使用配置文件替代硬编码可使应用程序拥有更高的灵活性,尤其是对分布式系统而言,意味着我们可以非常方便地调整分布对象的配置。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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐