WinCE数据通讯之Web Service篇

  转载至 http://www.cnblogs.com/gbmf/archive/2009/05/22/1459914.html

      准备写个WinCE平台与数据库服务器数据通讯交互方面的专题文章,今天先整理个Web Service通讯方式。

      公司目前的硬件产品平台是WinCE5.0,数据通讯是连接服务器与终端的桥梁,关系着终端的数据能否准确及时高效抵达服务器,是整个项目成败的关键。原先公司有同事用VC写过一个程序用Socket进行数据通讯,但一直问题不断。年前我开始探索用SqlCE与SqlServer数据同步方式进行数据上传与下载,通讯已经正常稳定。这方面的文章后续再整理。

     Web Service用于PC间通讯的文章网上有很多,但用于WinCE平台调用的经验总结并不多见。Web Service的程序编写与配置调用相对来讲比较简单,Visual Studio里直接新建一个“Asp.net web 服务应用程序”就可以创建一个web Service项目了。其中的代码根据实际需求编写就行,这方面就不详述了。

      终端设备是通过GPRS来进行数据传输的,因此,数据流量是非常重要的问题,应当尽可能少的减少数据传输,流量可是Money,压缩技术是关键。Google大法,找到了一款物美价廉的东东-Ihttp://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 所谓物美是这款代码支持Dot net CF平台,所谓价廉是这款代码完全开源免费。

      操刀开工。。。先建一个直接返回DataSet集的Web Service服务


 private SqlConnection Conn;
 
private string ConnString = "Data Source=(local);Initial Catalog=Northwind;uid=sa;pwd=sa;";
#region dataConnection
        
private DataSet GetNorthwindDataSet()
        
{
            
return ExecuteSql("select * from Employees");
        }


        
private DataSet ExecuteSql(string mysql)
        
{
            DataSet dataSet 
= new DataSet();
            SqlDataAdapter adapter 
= new SqlDataAdapter(mysql, this.Conn);
            
try
            
{
                
if (this.Conn.State == ConnectionState.Closed)
                
{
                    
this.Conn.Open();
                }

                adapter.Fill(dataSet, 
"table");
            }

            
catch (Exception exception)
            
{
                HttpContext.Current.Response.Write(exception.Message);
                HttpContext.Current.Response.End();
            }

            
finally
            
{
                
if ((this.Conn != null&& (this.Conn.State == ConnectionState.Open))
                
{
                    
this.Conn.Close();
                }

                adapter.Dispose();
            }

            
return dataSet;
        }

        
#endregion

 
//方法一:直接返回 DataSet 对象
        [WebMethod(Description = "直接返回 DataSet 对象。")]
        
public DataSet GetDataSet()
        
{
            DataSet dataSet 
= GetNorthwindDataSet();
            
return dataSet;
        }

      建立一个智能设备应用程序,添加Web引用,我这里用的是静态引用,没有用动态引用的原因是,试过网上的动态生成WebService引用的代码,效率远比静态引用要低很多,考虑终端设备资源的有限性,还是用的静态引用。建立好项目后在界面上添加一个button和datagrid控件,添加代码:


 private webSer.Service1 ws;
        
private void FrmMain_Load(object sender, EventArgs e)
        
{
            ws 
= new DeviceApplication1.webSer.Service1();
        }

        
//webmethod直接返回dataset
        private void btnDataSet_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
this.dtGrid.DataSource = null;
                DateTime dtBegin 
= DateTime.Now;
                DataSet ds 
= ws.GetDataSet();
                DateTime dtDown 
= DateTime.Now;
                
this.dtGrid.DataSource = ds.Tables[0];
                MessageBox.Show(
string.Format("下载耗时:{0},绑定数据耗时:{1},数据量:{2}"
                    dtDown 
- dtBegin, DateTime.Now - dtDown, ds.GetXml().Length));
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

        }

      连接好终端设备,测试点击按钮,几秒后DataGrid表格正确显示数据,OK,说明WinCE已经能够正确调用Web Service了。如果不能正确调用,检察WebService发布与Web引用是否正确,数据库配置是否正确。

      接下来就是要把数据进行压缩了,压缩前要进行数据的序列化,序列化代码如下:


 /// <summary>
        
/// 序列化数据成byte[]
        
/// </summary>
        
/// <param name="o">未序列化的数据</param>
        
/// <returns>返回序列化后的byte[]数组</returns>

        public static byte[] byteXmlSerializer(object o)
        
{
            
if (o == null)
            
{
                
throw new ArgumentNullException("null input");
            }

            
try
            
{
                System.Xml.Serialization.XmlSerializer ser 
= new System.Xml.Serialization.XmlSerializer(o.GetType());
                System.IO.MemoryStream mem 
= new MemoryStream();
                System.Xml.XmlTextWriter writer 
= new System.Xml.XmlTextWriter(mem, Encoding.Default);
                ser.Serialize(writer, o);
                writer.Close();
                
return mem.ToArray();
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }

 
/// <summary>
        
/// 反序列化数据
        
/// </summary>
        
/// <param name="input">序列化后的数据</param>
        
/// <param name="type">被序列化的数据类型</param>
        
/// <returns>返回反序列化后的object数据</returns>

        public static object objXmlDeserialize(byte[] input, Type type)
        
{
            
if (input == null)
            
{
                
throw new ArgumentNullException("null input");
            }

            
try
            
{
                XmlSerializer mySerializer 
= new XmlSerializer(type);
                StreamReader stmRead 
= new StreamReader(new MemoryStream(input), System.Text.Encoding.Default);
                
return mySerializer.Deserialize(stmRead);
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }

      ICSharpCode提供了多种数据压缩的方式,我这里测试了四种方式:BZip,Deflate,GZip,Zip


 private static int buffSize = 2048;//指定压缩块缓存的大小,一般为2048的倍数
  /// <summary>
        
/// BZIP2压缩数据
        
/// </summary>
        
/// <param name="input">原始未压缩数据</param>
        
/// <returns>压缩后的byte[]数据</returns>

       public static byte[] BZipCompress(byte[] input)
        
{
            
if (input == null)
            
{
                
throw new ArgumentNullException("null input");
            }

            
try
            
{
                
//int buffSize = 2048;//指定压缩块的大小,一般为2048的倍数
                using (MemoryStream outmsStrm = new MemoryStream())
                
{
                    
using (MemoryStream inmsStrm = new MemoryStream(input))
                    
{
                        BZip2.Compress(inmsStrm, outmsStrm, buffSize);
                    }

                    
return outmsStrm.ToArray();
                }

            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }

        
/// <summary>
        
/// 解压缩BZIP2数据
        
/// </summary>
        
/// <param name="input">被BZIP2压缩过的byte[]数据</param>
        
/// <returns>解压后的byte[]数据</returns>

        public static byte[] BZipDeCompress(byte[] input)
        
{
            
if (input == null)
            
{
                
throw new ArgumentNullException("null input");
            }

            
try
            
{
                
using (MemoryStream outmsStrm = new MemoryStream())
                
{
                    
using (MemoryStream inmsStrm = new MemoryStream(input))
                    
{
                        BZip2.Decompress(inmsStrm, outmsStrm);
                    }

                    
return outmsStrm.ToArray();
                }

            }

            
catch (Exception ex)
            
{
                
throw (ex);
            }

        }

       
/// <summary>
        
/// 压缩Deflater数据
       
/// </summary>
       
/// <param name="input">待压缩byte[]数据</param>
       
/// <returns>返回压缩后的byte[]</returns>

       public static byte[] DeflaterCompress(byte[] input)
       
{
           
if (input == null)
           
{
               
throw new ArgumentNullException("null input");
           }

           
try
           
{
               Deflater mDeflater 
= new Deflater(Deflater.BEST_COMPRESSION);
               
//int buffSize = 2048;//131072 buff size
               using (MemoryStream outmsStrm = new MemoryStream())
               
{
                   
using (DeflaterOutputStream mStream = new DeflaterOutputStream(outmsStrm, mDeflater, buffSize))
                   
{
                       mStream.Write(input, 
0, input.Length);
                   }

                   
return outmsStrm.ToArray();
               }

           }

           
catch (Exception ex)
           
{
               
throw (ex);
           }

       }

      
       
/// <summary>
       
/// 解压缩Deflater数据
       
/// </summary>
       
/// <param name="input">压缩过的byte[]数据</param>
       
/// <returns>解压后的byte[]数据</returns>

       public static byte[] DeflaterDeCompress(byte[] input)
        
{
            
if (input == null)
            
{
                
throw new ArgumentNullException("null input");
            }

            
try
            
{
                Int32 mSize;
                
//int buffSize = 2048;
                byte[] buff = new byte[buffSize];
                
using (MemoryStream outmsStrm = new MemoryStream())
                
{
                    
using (InflaterInputStream mStream = new InflaterInputStream(new MemoryStream(input)))
                    
{
                        
while (true)
                        
{
                            mSize 
= mStream.Read(buff, 0, buff.Length);
                            
if (mSize > 0)
                            
{
                                outmsStrm.Write(buff, 
0, mSize);
                            }

                            
else
                            
{
                                
break;
                            }

                        }

                    }

                    
return outmsStrm.ToArray();
                }

            }

            
catch (Exception ex)
            
{
                
throw (ex);
            }

        }

       
/// <summary>
       
/// GZIP压缩
       
/// </summary>
       
/// <param name="input">未压缩的数据</param>
       
/// <returns>GZIP压缩后的数据</returns>

       public static byte[] GZipCompress(byte[] input)
       
{
           
if (input == null)
           
{
               
throw new ArgumentNullException("null input");
           }

           
try
           
{
               
using (MemoryStream outmsStrm = new MemoryStream())
               
{
                   
using (GZipOutputStream gzip = new GZipOutputStream(outmsStrm))
                   
{
                       gzip.Write(input, 
0, input.Length);
                   }

                   
return outmsStrm.ToArray();
               }

           }

           
catch (Exception ex)
           
{
               
throw (ex);
           }

       }

       
/// <summary>
       
/// GZIP解压缩
       
/// </summary>
       
/// <param name="input">压缩过的数据</param>
       
/// <returns>解压后的数据</returns>

       public static byte[] GZipDeCompress(byte[] input)
       
{
           
if (input == null)
           
{
               
throw new ArgumentNullException("null input");
           }

           
try
           
{
               
using (MemoryStream outmsStrm = new MemoryStream())
               
{
                   
using (GZipInputStream gzip = new GZipInputStream(new MemoryStream(input)))
                   
{
                       Int32 mSize;
                       
//int buffSize = 2048;
                       byte[] buff = new byte[buffSize];
                       
while (true)
                       
{
                           mSize 
= gzip.Read(buff, 0, buffSize);
                           
if (mSize > 0)
                           
{
                               outmsStrm.Write(buff, 
0, mSize);
                           }

                           
else
                           
{
                               
break;
                           }

                       }

                   }

                   
return outmsStrm.ToArray();
               }

           }

           
catch (Exception ex)
           
{
               
throw (ex);
           }

       }

       
/// <summary>
       
/// ZIP压缩数据
       
/// </summary>
       
/// <param name="input">待压缩的数据</param>
       
/// <returns>ZIP压缩后的数据</returns>

       public static byte[] ZipCompress(byte[] input)
       
{
           
if (input == null)
           
{
               
throw new ArgumentNullException("null input");
           }

           
try
           
{
               
using (MemoryStream outmsStrm = new MemoryStream())
               
{
                   
using (ZipOutputStream zipStrm = new ZipOutputStream(outmsStrm))
                   
{
                       ZipEntry zn 
= new ZipEntry("znName");
                       zipStrm.PutNextEntry(zn);
                       zipStrm.Write(input, 
0, input.Length);
                   }

                   
return outmsStrm.ToArray();
               }

           }

           
catch (Exception ex)
           
{
               
throw (ex);
           }

       }

       
/// <summary>
       
/// ZIP解压缩数据
       
/// </summary>
       
/// <param name="input">压缩过的数据</param>
       
/// <returns>解压后的数据</returns>

       public static byte[] ZipDeCompress(byte[] input)
       
{
           
if (input == null)
           
{
               
throw new ArgumentNullException("null input");
           }

           
try
           
{
               
using (MemoryStream outmsStrm = new MemoryStream())
               
{
                   
using (ZipInputStream zipStrm = new ZipInputStream(new MemoryStream(input)))
                   
{
                       Int32 mSize;
                       
//int buffSize = 2048;
                       byte[] buff = new byte[buffSize];
                       ZipEntry zn 
= new ZipEntry("znName");
                       
while ((zn = zipStrm.GetNextEntry()) != null)
                       
{
                           
while (true)
                           
{
                               mSize 
= zipStrm.Read(buff, 0, buffSize);
                               
if (mSize > 0)
                               
{
                                   outmsStrm.Write(buff, 
0, mSize);
                               }

                               
else
                               
{
                                   
break;
                               }

                           }


                       }

                   }

                   
return outmsStrm.ToArray();
               }

           }

           
catch (Exception ex)
           
{
               
throw (ex);
           }

       }

      添加WebService服务


        //方法二:返回 DataSet 对象用序列化后的byte[]字节数组
        [WebMethod(Description = "返回 DataSet 对象用序列化后的byte[]字节数组。")]
        
public byte[] GetDataSetBytes()
        {
            DataSet dataSet 
= GetNorthwindDataSet();
            
return ComZipClass.zip.byteXmlSerializer(dataSet);
        }

        
//方法三返回 DataSet对象用序列化并Bzip压缩后的byte[]字节数组
        [WebMethod(Description = "返回 DataSet对象用序列化并Bzip压缩后的byte[]字节数组。")]
        
public byte[] GetBZiipCompress()
        {
            DataSet dataSet 
= GetNorthwindDataSet();
            
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
            
byte[] zip = ComZipClass.zip.BZipCompress(ser);
            
return zip;
        }
        [WebMethod(Description 
= "返回Deflater压缩")]
        
public byte[] GetDeflaterCompress()
        {
            DataSet dataSet 
= GetNorthwindDataSet();
            
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
            
//byte[] ser = ZipClass.zip.byteXmlSerializer(dataSet);
            byte[] zip = ComZipClass.zip.DeflaterCompress(ser);
            
return zip;
        }
        [WebMethod(Description 
= "返回Gzip压缩")]
        
public byte[] GetGZipCompress()
        {
            DataSet dataSet 
= GetNorthwindDataSet();
            
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
            
byte[] zip = ComZipClass.zip.GZipCompress(ser);
            
return zip;
        }
        [WebMethod(Description 
= "返回Zip压缩")]
        
public byte[] GetZipCompress()
        {
            DataSet dataSet 
= GetNorthwindDataSet();
            
byte[] ser = ComZipClass.zip.byteXmlSerializer(dataSet);
            
byte[] zip = ComZipClass.zip.ZipCompress(ser);
            
return zip;
        }

     添加终端设备调用


 //webmethod序列化数据
        private void btnSerial_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
this.dtGrid.DataSource = null;
                DataSet ds 
= new DataSet();
                DateTime dtBegin 
= DateTime.Now;
                
byte[] datas = ws.GetDataSetBytes();
                DateTime dtDown 
= DateTime.Now;

                ds 
= (DataSet)ComZipClass.zip.objXmlDeserialize(datas, typeof(DataSet));
                DateTime dtSerial 
= DateTime.Now;
                
this.dtGrid.DataSource = ds.Tables[0];
                MessageBox.Show(
string.Format("下载耗时:{0},解压序列化数据耗时:{1},绑定数据耗时:{2},数据量:{3}",
                dtDown 
- dtBegin,dtSerial-dtDown, DateTime.Now - dtDown, datas.Length));
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

        }

        
//序列化并压缩BZIP后数据
        private void btnBZip_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
this.dtGrid.DataSource = null;
                DataSet ds 
= new DataSet();
                DateTime dtBegin 
= DateTime.Now;
                
byte[] datas = ws.GetBZiipCompress();
                DateTime dtDown 
= DateTime.Now;
                
byte[] uzip = ComZipClass.zip.BZipDeCompress(datas);
                DateTime dtUnzip 
= DateTime.Now;
                ds 
= (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
                DateTime dtSerial 
= DateTime.Now;
                
this.dtGrid.DataSource = ds.Tables[0];
                 MessageBox.Show(
string.Format("下载耗时:{0},解压BZIP耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
                dtDown 
- dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

        }

        
//序列化并压缩Deflate后数据
        private void btnDeflater_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
this.dtGrid.DataSource = null;
                DataSet ds 
= new DataSet();
                DateTime dtBegin 
= DateTime.Now;
                
byte[] datas = ws.GetDeflaterCompress();
                DateTime dtDown 
= DateTime.Now;
                
byte[] uzip = ComZipClass.zip.DeflaterDeCompress(datas);
                DateTime dtUnzip 
= DateTime.Now;
                ds 
= (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
                DateTime dtSerial 
= DateTime.Now;
                
this.dtGrid.DataSource = ds.Tables[0];
                MessageBox.Show(
string.Format("下载耗时:{0},解压Deflater耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
               dtDown 
- dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

        }

        
//序列化并压缩GZIP后数据
        private void btnGZip_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
this.dtGrid.DataSource = null;
                DataSet ds 
= new DataSet();
                DateTime dtBegin 
= DateTime.Now;
                
byte[] datas = ws.GetGZipCompress();
                DateTime dtDown 
= DateTime.Now;
                
byte[] uzip = ComZipClass.zip.GZipDeCompress(datas);
                DateTime dtUnzip 
= DateTime.Now;
                ds 
= (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
                DateTime dtSerial 
= DateTime.Now;
                
this.dtGrid.DataSource = ds.Tables[0];
                MessageBox.Show(
string.Format("下载耗时:{0},解压GZIP耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
               dtDown 
- dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

        }

        
//序列化并压缩ZIP后数据
        private void btnZip_Click(object sender, EventArgs e)
        
{
            
try
            
{
                
this.dtGrid.DataSource = null;
                DataSet ds 
= new DataSet();
                DateTime dtBegin 
= DateTime.Now;
                
byte[] datas = ws.GetZipCompress();
                DateTime dtDown 
= DateTime.Now;
                
byte[] uzip = ComZipClass.zip.ZipDeCompress(datas);
                DateTime dtUnzip 
= DateTime.Now;
                ds 
= (DataSet)ComZipClass.zip.objXmlDeserialize(uzip, typeof(DataSet));
                DateTime dtSerial 
= DateTime.Now;
                
this.dtGrid.DataSource = ds.Tables[0];
                MessageBox.Show(
string.Format("下载耗时:{0},解压ZIP耗时:{1},解压序列化耗时:{2},绑定数据耗时:{3},数据量:{4}",
               dtDown 
- dtBegin, dtUnzip - dtDown, dtSerial - dtUnzip, DateTime.Now - dtSerial, datas.Length));
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

        }

      几个测试压缩数据dataset :268128;serial:269199;bzip:94561;Deflater:107049;gzip:108276;zip:108408;

      附上源码,解压密码暂不公开,有需要的留下Email。源码一  源码二

posted on 2012-03-16 13:25  jshchg  阅读(251)  评论(0编辑  收藏  举报

导航