浅谈WebService返回数据效率对比
本文转载:http://www.cnblogs.com/waynechan/archive/2012/06/25/2560685.html
一、什么是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作为根本,然后来做各种转换压缩的操作:
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程序
{
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 + "条记录";
}
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;
}
}
}
在上例中,调用四个方法的效果是一样的,唯一不同的是,传输过程中,数据量大小和传输时间的差异。
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET