学习.Net Remoting已有一段时间,对于其初步部署应该倒不是很难,真正应用到系统中则需要考虑的问题逐渐多了起来,.Net Remoting机制其特点可以穿透防火墙,在局域网内速度很快,实现证明是比Web Service快很多,但在广域网测试的时候.Net Remoting(使用TCP)效能明显不如Web Service运行效能,后来通过更改.Net Remoting协议改为HEEP+SOAP方式进行,效能有所改善,网络上很多资料显示Web Service效能明显比.Net Remoting效能快,但个人觉得实用为第一,不必要追求所谓真正的快速,因为没有绝对的快速!
     选择机制要考虑到使用范围,开发环境还有网络配置等等因素,选择适合的才是最重要的!

在这次.Net Remoting效能改善最大的感受是,尽量少进入通道,尽量一次性读取必要讯息,重复使用离线数据源,并且使用了数据压缩技术,根据网络上的资料修改所得,与大家分享!
使用之前需要下载组件: ICSharpCode.SharpZipLib 类库可以从这里下载。

public class ZipHelper
    
{
        
public static byte[] Zip(byte[] data)
        
{
            
return Zip(data, 0, data.Length);
        }
 
 

        
public static byte[] Unzip(byte[] data)
        
{
            
return Unzip(data, 0, data.Length);
        }


        
public static byte[] Zip(byte[] data, int offset, int size)
        
{
            MemoryStream inStream 
= new MemoryStream(data, offset, size);
            MemoryStream outStream 
= new MemoryStream();
            BZip2.Compress(inStream, outStream, size);

            
byte[] result = outStream.ToArray();
            inStream.Close();
            outStream.Close();

            
return result;
        }


        
public static byte[] Unzip(byte[] data, int offset, int size)
        
{
            MemoryStream inStream 
= new MemoryStream(data, offset, size);
            MemoryStream outStream 
= new MemoryStream();
            BZip2.Decompress(inStream, outStream);
            
byte[] result = outStream.ToArray();
            inStream.Close();
            outStream.Close();
            
return result;
        }


        
/**////   <summary>   
        
///   序列化   
        
///   </summary>   
        
///   <param   name="data">要序列化的物件</param>   
        
///   <returns>返回存放序列化後的資料緩衝區</returns>   

        public static byte[] Serialize(object aodata)
        
{
            BinaryFormatter formatter 
= new BinaryFormatter();
            MemoryStream rems 
= new MemoryStream();
            formatter.Serialize(rems, aodata);
            
return rems.GetBuffer();
        }


        
/**////   <summary>   
        
///   反序列化   
        
///   </summary>   
        
///   <param   name="data">數據緩衝區</param>   
        
///   <returns>對象</returns>   

        public static object Deserialize(byte[] abdata)
        
{
            BinaryFormatter formatter 
= new BinaryFormatter();
            MemoryStream rems 
= new MemoryStream(abdata);
            abdata 
= null;
            
return formatter.Deserialize(rems);
        }


        
public static Dictionary<string, DataTable> UnZipDictionary(byte[] abDictionary)
        
{
            Dictionary
<string, DataTable> dictonary = new Dictionary<string, DataTable>();
            
byte[] bdictionary = Unzip(abDictionary);
            dictonary 
= (Dictionary<string, DataTable>)Deserialize(bdictionary);
            
return dictonary;
        }


        
public static byte[] ZipDictionary(Dictionary<string, DataTable> adDictionary)
        
{
            
byte[] bdictionary = Serialize(adDictionary);
            
byte[] dictionary = Zip(bdictionary);
            
return dictionary;
        }


        
public static DataTable UnZipDataTable(byte[] abDictionary)
        
{
            DataTable dictonary 
= new DataTable();
            
byte[] bdictionary = Unzip(abDictionary);
            dictonary 
= (DataTable)Deserialize(bdictionary);
            
return dictonary;
        }


        
public static byte[] ZipDataTable(DataTable adDictionary)
        
{
            
byte[] bdictionary = Serialize(adDictionary);
            
byte[] dictionary = Zip(bdictionary);
            
return dictionary;
        }


        
public static Object UnZipObject(byte[] aoObject)
        
{
            Object mObject 
= new Object();
            
byte[] bdictionary = Unzip(aoObject);
            mObject 
= (Object)Deserialize(bdictionary);
            
return mObject;
        }


        
public static byte[] ZipObject(Object aoObject)
        
{
            
byte[] bdictionary = Serialize(aoObject);
            
byte[] dictionary = Zip(bdictionary);
            
return dictionary;
        }