通过Spring.net发布Remoting
Spring.Services程序集中提供了对Remoting的支持,使用Spring.net可以方便的发布和使用Remoting服务。
1.Remoting发布(服务端)
(1)首先在config文件中注册通道:
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="8100" >
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="8100" >
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
(2)通过配置发布Remoting对象:
这里我们以SAO的方式发布Remoting对象。
<object id="gamesBoard" type="GameRabbit.Platform.Management.GamesBoard,GameRabbit.Platform">
</object>
<object name="saoGamesBoard" type="Spring.Remoting.SaoExporter,Spring.Services">
<property name="TargetName" value="gamesBoard" />
<property name="ServiceName" value="GamesBoard" />
</object>
</object>
<object name="saoGamesBoard" type="Spring.Remoting.SaoExporter,Spring.Services">
<property name="TargetName" value="gamesBoard" />
<property name="ServiceName" value="GamesBoard" />
</object>
(3)在程序启动时,初始化Remoting配置:
RemotingConfiguration.Configure("EntityTest.exe.config");
2.Remoting订阅(客户端)
<object name="remoteGamesBoard" type="Spring.Remoting.SaoFactoryObject, Spring.Services">
<property name="ServiceInterface">
<value>GameRabbit.Platform.Management.IGamesBoard, GameRabbit.Platform</value>
</property>
<property name="ServiceUrl">
<value>tcp://localhost:8100/GamesBoard</value>
</property>
</object>
<property name="ServiceInterface">
<value>GameRabbit.Platform.Management.IGamesBoard, GameRabbit.Platform</value>
</property>
<property name="ServiceUrl">
<value>tcp://localhost:8100/GamesBoard</value>
</property>
</object>
说明:(2008-07-16)
1.Spring发布的remoting对象不需要从MarshalByRefObject继承,也不需要设置remoting对象的生存期(spring将其设为无限长)。Spring采用为对象添加动态代理的方式来做到这点。
2.如果是注册到服务器然后用于回调的Remoting对象,必须从MarshalByRefObject继承,而且还要设置生存期,否则可能出现在服务端回调的时候出现“找不到对应的服务”的错误。