浅谈.Net WebService开发
一、什么是WebService:
简单通俗来说,就是企业之间、网站之间通过Internet来访问并使用在线服务,一些数据,由于安全性问题,不能提供数据库给其他单位使用,这时候可以使 用WebService服务提供。
二、创建WebService
创建WebService之后,我们就可以在文件里写返回数据的方法了。
三、返回数据的四种形式
笔者水平有限,只列出这四种数据的返回形式:
(1)直接返回DataSet对象
(2)返回DataSet对象用Binary序列化后的字节数组
(3)返回DataSetSurrogate对象用Binary序列化后的 字节数组
(4)返回DataSetSurrogate对象用Binary序列化并Zip 压缩后的字节数组
理论上来说,网络传输字节与传输时间,应该是递减的,其中,(3)(4)种方法需要引用微软提供的开源组件 下载地址:http://support.microsoft.com/kb/829740/zh-cn
下面展示这四种返回数据的代码,其中(1)是其三种方法的根本,都要得到一个DataSet作为根本,然后来做各种转换压缩的操作:
[WebMethod(Description = "直接返回DataSet对象")] public DataSet GetDataSet() { string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString(); SqlConnection conn = new SqlConnection(connStr); string sql = "select * from china_city"; conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet("China"); sda.Fill(ds); conn.Close(); return ds; } [WebMethod(Description = "直接返回DataSet对象,并用Binary序列化后的字节数组")] public byte[] GetDataSetBytes() { DataSet ds = GetDataSet(); BinaryFormatter ser = new BinaryFormatter(); //序列化对象 MemoryStream ms = new MemoryStream(); //内存流 ser.Serialize(ms, ds); byte[] buffer = ms.ToArray(); //字节流 return buffer; } [WebMethod(Description = "直接返回DataSetSurrogate对象,并用Binary序列化后的字节数组")] public byte[] GetDataSetSurrogateBytes() { DataSet ds = GetDataSet(); DataSetSurrogate dss = new DataSetSurrogate(ds); BinaryFormatter ser = new BinaryFormatter(); //序列化对象 MemoryStream ms = new MemoryStream(); //内存流 ser.Serialize(ms, dss); byte[] buffer = ms.ToArray(); //字节流 return buffer; } [WebMethod(Description = "直接返回DataSetSurrogate对象,并用Binary序列化后并且ZIP压缩的字节数组")] public byte[] GetDataSetSurrogateZipBytes() { DataSet ds = GetDataSet(); DataSetSurrogate dss = new DataSetSurrogate(ds); BinaryFormatter ser = new BinaryFormatter(); //序列化对象 MemoryStream ms = new MemoryStream(); //内存流 ser.Serialize(ms, dss); byte[] buffer = ms.ToArray(); //字节流 byte[] bufferZip = ComPress(buffer); return buffer; } //压缩方法 public byte[] ComPress(byte[] data) { try { MemoryStream ms = new MemoryStream(); Stream zipStream = null; zipStream = new GZipStream(ms, CompressionMode.Compress, true); zipStream.Write(data, 0, data.Length); zipStream.Close(); ms.Position = 0; byte[] compressed_data = new byte[ms.Length]; ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString())); return compressed_data; } catch { return null; } }
我们可以在浏览器中查看下WebService的效果,如图,在这个页面中,有提供四个方法,这四个方法就是上述我们写的四个返回数据的方法了,点击方法即可返回相应的数据,这样,我们数据提供方的代码就可以写好了,接下来,我们写调用数据的方法!
四、调用数据
客户端WebService程序
private void button1_Click(object sender, EventArgs e) { com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1(); //new出WebService对象 DateTime dtBegin = DateTime.Now; DataSet dataSet = ds.GetNorthwindDataSet(); this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin); binddata(dataSet); } private void button2_Click(object sender, EventArgs e) { com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1(); DateTime dtBegin = DateTime.Now; byte[] buffer = ds.GetDataSetBytes(); BinaryFormatter ser = new BinaryFormatter(); DataSet dataSet = ser.Deserialize(new MemoryStream(buffer)) as DataSet; this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + " " + buffer.Length; binddata(dataSet); } private void button3_Click(object sender, EventArgs e) { com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1(); DateTime dtBegin = DateTime.Now; byte[] buffer = ds.GetDataSetSurrogateBytes(); BinaryFormatter ser = new BinaryFormatter(); DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet dataSet = dss.ConvertToDataSet(); this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + " " + buffer.Length; binddata(dataSet); } private void button4_Click(object sender, EventArgs e) { com.dzbsoft.www.Service1 ds = new com.dzbsoft.www.Service1(); DateTime dtBegin = DateTime.Now; byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes(); byte[] buffer = UnZipClass.Decompress(zipBuffer); BinaryFormatter ser = new BinaryFormatter(); DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet dataSet = dss.ConvertToDataSet(); this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + " " + zipBuffer.Length; binddata(dataSet); } private void binddata(DataSet dataSet) { this.dataGridView1.DataSource = dataSet.Tables[0]; this.label5.Text = "共计:" + dataSet.Tables[0].Rows.Count + "条记录"; }在数据返回的方法中,我们使用了数据的压缩,所以,在调用方这边,需要进行解压,代码:
客户端UnZipClass程序 public static class UnZipClass { public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); Stream zipStream = null; zipStream = new GZipStream(ms, CompressionMode.Decompress); byte[] dc_data = null; dc_data = ExtractBytesFromStream(zipStream, data.Length); return dc_data; } catch { return null; } } public static byte[] ExtractBytesFromStream(Stream zipStream, int dataBlock) { byte[] data = null; int totalBytesRead = 0; try { while (true) { Array.Resize(ref data, totalBytesRead + dataBlock + 1); int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock); if (bytesRead == 0) { break; } totalBytesRead += bytesRead; } Array.Resize(ref data, totalBytesRead); return data; } catch { return null; } } }在上例中,调用四个方法的效果是一样的,唯一不同的是,传输过程中,数据量大小和传输时间的差异。 转自:http://blog.csdn.net/wayne20018891/article/details/7578040