DotNet Remoting 处理复杂类型

在使用Remoting时,不但可以传递值类型,字符串,只要是可以序列化的类都可以处理

新建的类,可以通过添加Serializable来将一个类标示为可序列化

TypeFilterLevel枚举描述了Remoting的2种序列化级别,Low和Full

Full表示.NET Framework 远程处理的 Full (完全)反序列化级别。它支持远程处理在所有情况下支持的所有类型。 

Low表示.NET Framework 远程处理的 Low (低)反序列化级别。它支持与基本远程处理功能相关联的类型。 

默认配置是Low级别,所以需要将服务器端的Provider的Formatter(如BinaryServerFormatterSinkProvider)的TypeFilterLevel属性设为Full级别,客户端不需要设置TypeFilterLevel,客户端在注册信道时必需要指定端口号,服务器端使用代码配置:

System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider server = new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider();
server.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Low;

客户端使用代码配置:

System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider client = new System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider();
System.Collections.IDictionary props = new System.Collections.Hashtable();
props["port"] = 0;
System.Runtime.Remoting.Channels.Tcp.TcpChannel cl = new System.Runtime.Remoting.Channels.Tcp.TcpChannel(props, client, null);

也可以使用Xml进行配置,这时需要使用formatter 元素,服务器端的Xml配置:

<channel ref="http" port="1234">
          <serverProviders>
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>
        </channel>

客户端的Xml配置:

<channel ref="http" port="0">
          <clientProviders>
            <formatter ref="binary" />
          </clientProviders>
        </channel>

posted on 2010-06-07 10:20  漫步人生  阅读(229)  评论(0编辑  收藏  举报