在网上看到(包括MSDN中)关于自定义压缩信道的配置方式都是使用*.config配置文件的方式,可以是我的框架中是自定义的配置文件,编程实现Remoting的配置,要编程配置自定义信道方法如下:
服务端:
//建立一个端口键值对。名称,端口号。
IDictionary props = new Hashtable();
props["name"] = serviceName;
props["port"] = channelPort;
//为使用 压缩接收器 的服务器格式化程序信道接收器提供程序提供实现。
JCsoft.Common.Remoting.Channels.CompressionServerFormatterSinkProvider serverSinkProvider = new JCsoft.Common.Remoting.Channels.CompressionServerFormatterSinkProvider(compressionLevel);
//注册信道。
switch(channelType)
{
case ChannelType.TCP:
{
serverSinkProvider.Next = new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider();
ChannelServices.RegisterChannel(new TcpServerChannel(props, serverSinkProvider));
break;
}
case ChannelType.HTTP:
{
serverSinkProvider.Next = new System.Runtime.Remoting.Channels.SoapServerFormatterSinkProvider();
ChannelServices.RegisterChannel(new HttpServerChannel(props, serverSinkProvider));
break;
}
}
//注册服务类型。
switch(objectMode)
{
case ObjectMode.Singleton:
{
RemotingConfiguration.RegisterWellKnownServiceType(serviceType, serviceName, WellKnownObjectMode.Singleton);
break;
}
case ObjectMode.SingleCall:
{
RemotingConfiguration.RegisterWellKnownServiceType(serviceType, serviceName, WellKnownObjectMode.SingleCall);
break;
}
}
return true;
IDictionary props = new Hashtable();
props["name"] = serviceName;
props["port"] = channelPort;
//为使用 压缩接收器 的服务器格式化程序信道接收器提供程序提供实现。
JCsoft.Common.Remoting.Channels.CompressionServerFormatterSinkProvider serverSinkProvider = new JCsoft.Common.Remoting.Channels.CompressionServerFormatterSinkProvider(compressionLevel);
//注册信道。
switch(channelType)
{
case ChannelType.TCP:
{
serverSinkProvider.Next = new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider();
ChannelServices.RegisterChannel(new TcpServerChannel(props, serverSinkProvider));
break;
}
case ChannelType.HTTP:
{
serverSinkProvider.Next = new System.Runtime.Remoting.Channels.SoapServerFormatterSinkProvider();
ChannelServices.RegisterChannel(new HttpServerChannel(props, serverSinkProvider));
break;
}
}
//注册服务类型。
switch(objectMode)
{
case ObjectMode.Singleton:
{
RemotingConfiguration.RegisterWellKnownServiceType(serviceType, serviceName, WellKnownObjectMode.Singleton);
break;
}
case ObjectMode.SingleCall:
{
RemotingConfiguration.RegisterWellKnownServiceType(serviceType, serviceName, WellKnownObjectMode.SingleCall);
break;
}
}
return true;
客户端:
object service = null;
string machine = "localhost";
string connectionString = string.Empty;
//为使用 压缩接收器 的服务器格式化程序信道接收器提供程序提供实现。
JCsoft.Common.Remoting.Channels.CompressionClientFormatterSinkProvider clientSinkProvider = new JCsoft.Common.Remoting.Channels.CompressionClientFormatterSinkProvider(compressionLevel);
// JCsoft.Common.Remoting.Channels.CompressionClientFormatterSink clientSink = new JCsoft.Common.Remoting.Channels.CompressionClientFormatterSink(compressionLevel);
//注册信道。
switch(channelType)
{
case ChannelType.TCP:
{
System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider firstClientSinkProvider = new System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider();
firstClientSinkProvider.Next = clientSinkProvider;
IDictionary props = new Hashtable();
// props["name"] = "TcpClientChannel";
//props["priority"] = 100;
ChannelServices.RegisterChannel(new TcpClientChannel(props, firstClientSinkProvider));
// ChannelServices.RegisterChannel(new BinaryClientFormatterSink(clientSink));
break;
}
case ChannelType.HTTP:
{
System.Runtime.Remoting.Channels.SoapClientFormatterSinkProvider firstClientSinkProvider = new System.Runtime.Remoting.Channels.SoapClientFormatterSinkProvider();
firstClientSinkProvider.Next = clientSinkProvider;
IDictionary props = new Hashtable();
// props["name"] = "HttpClientChannel";
//props["priority"] = 100;
ChannelServices.RegisterChannel(new HttpClientChannel(props, firstClientSinkProvider));
// ChannelServices.RegisterChannel(new SoapClientFormatterSink(clientSink));
break;
}
}
switch(channelType)
{
case ChannelType.TCP:
{
connectionString = string.Format("tcp://{0}:{1}/{2}", machine, channelPort, serviceName);
break;
}
case ChannelType.HTTP:
{
connectionString = string.Format("http://{0}:{1}/{2}", machine, channelPort, serviceName);
break;
}
}
try
{
service = Activator.GetObject(serviceType, connectionString);
}
catch(Exception ex)
{
// 处理异常。
ExceptionManager.Publish(ex);
}
string machine = "localhost";
string connectionString = string.Empty;
//为使用 压缩接收器 的服务器格式化程序信道接收器提供程序提供实现。
JCsoft.Common.Remoting.Channels.CompressionClientFormatterSinkProvider clientSinkProvider = new JCsoft.Common.Remoting.Channels.CompressionClientFormatterSinkProvider(compressionLevel);
// JCsoft.Common.Remoting.Channels.CompressionClientFormatterSink clientSink = new JCsoft.Common.Remoting.Channels.CompressionClientFormatterSink(compressionLevel);
//注册信道。
switch(channelType)
{
case ChannelType.TCP:
{
System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider firstClientSinkProvider = new System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider();
firstClientSinkProvider.Next = clientSinkProvider;
IDictionary props = new Hashtable();
// props["name"] = "TcpClientChannel";
//props["priority"] = 100;
ChannelServices.RegisterChannel(new TcpClientChannel(props, firstClientSinkProvider));
// ChannelServices.RegisterChannel(new BinaryClientFormatterSink(clientSink));
break;
}
case ChannelType.HTTP:
{
System.Runtime.Remoting.Channels.SoapClientFormatterSinkProvider firstClientSinkProvider = new System.Runtime.Remoting.Channels.SoapClientFormatterSinkProvider();
firstClientSinkProvider.Next = clientSinkProvider;
IDictionary props = new Hashtable();
// props["name"] = "HttpClientChannel";
//props["priority"] = 100;
ChannelServices.RegisterChannel(new HttpClientChannel(props, firstClientSinkProvider));
// ChannelServices.RegisterChannel(new SoapClientFormatterSink(clientSink));
break;
}
}
switch(channelType)
{
case ChannelType.TCP:
{
connectionString = string.Format("tcp://{0}:{1}/{2}", machine, channelPort, serviceName);
break;
}
case ChannelType.HTTP:
{
connectionString = string.Format("http://{0}:{1}/{2}", machine, channelPort, serviceName);
break;
}
}
try
{
service = Activator.GetObject(serviceType, connectionString);
}
catch(Exception ex)
{
// 处理异常。
ExceptionManager.Publish(ex);
}
代码中部分注释掉的是错误的。
如何实现压缩信道可以参见博客园-Zendy的实现一个压缩Remoting传输数据的Sink:CompressionSink 的一篇文件http://www.cnblogs.com/caomao/archive/2005/07/29/202942.aspx,当然也可以自已看MSDN来学习