[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();
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();
相关文章推荐
- [Remoting] 六:异步调用(收藏转帖http://www.rainsts.net/article.asp?id=413)
- [Remoting] 三:激活模式(收藏转帖http://www.rainsts.net/article.asp?id=410)
- [Remoting] 七:调用上下文(收藏转帖http://www.rainsts.net/article.asp?id=414)
- [Remoting] 八:元数据(收藏转帖http://www.rainsts.net/article.asp?id=415)
- [Remoting] 九:动态发布(收藏转帖http://www.rainsts.net/article.asp?id=416)
- [Remoting] 十:追踪服务(收藏转帖http://www.rainsts.net/article.asp?id=417)
- [Remoting] 四:生存期租约(收藏转帖http://www.rainsts.net/article.asp?id=411)
- NET Remoting一,二(收藏转帖http://www.rainsts.net/article.asp?id=408)
- [Remoting] 五:信道(收藏转帖http://www.rainsts.net/article.asp?id=412)
- Message Queuing(收藏转帖http://www.rainsts.net/article.asp?id=418)
- [Remoting] 十一:事件(收藏转帖http://www.rainsts.net/article.asp?id=419)
- 从雨痕老大的博客上转来的 http://www.rainsts.net/article.asp?id=1066 读书人偷书不算窃 :)
- [ASP.NET 优化] IIS6 Gziphttp://www.rainsts.net/article.asp?id=531
- 动态调用 WebService (收藏转贴http://www.rainsts.net/article.asp?id=304)
- Fedora7安装后的配置(转自:想飞的鱼http://www.mxpc.cn/article.asp?id=772)
- Asp.NET 2.0学习资源(reference from http://www.eyeya.com/article.asp?id=197)
- ASP.NET 缓存(五)--使用文件依赖项缓存页输出http://www.opent.cn/?action=show&id=192
- COM interop理论/实践(http://www.manbu.net/article.asp?id=53)
- 将cooledit作为一个音频信号发生器(http://www.zsjys.net/JYJY/ShowArticle.asp?ArticleID=64)
- Ajax开发之JSON应用http://onewww.net/blog/article.asp?id=47