首页 > 代码库 > 浅谈WebService返回数据效率对比
浅谈WebService返回数据效率对比
原文链接 http://www.dotnetgeek.cn/xuexiwebservice1.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作为根本,然后来做各种转换压缩的操作:
1 [WebMethod(Description = "直接返回DataSet对象")]
2 public DataSet GetDataSet()
3 {
4 string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();
5 SqlConnection conn = new SqlConnection(connStr);
6 string sql = "select * from china_city";
7 conn.Open();
8 SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
9 DataSet ds = new DataSet("China");
10 sda.Fill(ds);
11 conn.Close();
12 return ds;
13 }
14
15 [WebMethod(Description = "直接返回DataSet对象,并用Binary序列化后的字节数组")]
16 public byte[] GetDataSetBytes()
17 {
18 DataSet ds = GetDataSet();
19 BinaryFormatter ser = new BinaryFormatter(); //序列化对象
20 MemoryStream ms = new MemoryStream(); //内存流
21 ser.Serialize(ms, ds);
22 byte[] buffer = ms.ToArray(); //字节流
23 return buffer;
24 }
25
26 [WebMethod(Description = "直接返回DataSetSurrogate对象,并用Binary序列化后的字节数组")]
27 public byte[] GetDataSetSurrogateBytes()
28 {
29 DataSet ds = GetDataSet();
30 DataSetSurrogate dss = new DataSetSurrogate(ds);
31 BinaryFormatter ser = new BinaryFormatter(); //序列化对象
32 MemoryStream ms = new MemoryStream(); //内存流
33 ser.Serialize(ms, dss);
34 byte[] buffer = ms.ToArray(); //字节流
35 return buffer;
36
37 }
38
39 [WebMethod(Description = "直接返回DataSetSurrogate对象,并用Binary序列化后并且ZIP压缩的字节数组")]
40 public byte[] GetDataSetSurrogateZipBytes()
41 {
42 DataSet ds = GetDataSet();
43 DataSetSurrogate dss = new DataSetSurrogate(ds);
44 BinaryFormatter ser = new BinaryFormatter(); //序列化对象
45 MemoryStream ms = new MemoryStream(); //内存流
46 ser.Serialize(ms, dss);
47 byte[] buffer = ms.ToArray(); //字节流
48 byte[] bufferZip = ComPress(buffer);
49 return buffer;
50 }
51 //压缩方法
52 public byte[] ComPress(byte[] data)
53 {
54 try
55 {
56 MemoryStream ms = new MemoryStream();
57 Stream zipStream = null;
58 zipStream = new GZipStream(ms, CompressionMode.Compress, true);
59 zipStream.Write(data, 0, data.Length);
60 zipStream.Close();
61 ms.Position = 0;
62 byte[] compressed_data = new byte[ms.Length];
63 ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
64 return compressed_data;
65 }
66 catch
67 {
68 return null;
69 }
70 }
2 public DataSet GetDataSet()
3 {
4 string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();
5 SqlConnection conn = new SqlConnection(connStr);
6 string sql = "select * from china_city";
7 conn.Open();
8 SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
9 DataSet ds = new DataSet("China");
10 sda.Fill(ds);
11 conn.Close();
12 return ds;
13 }
14
15 [WebMethod(Description = "直接返回DataSet对象,并用Binary序列化后的字节数组")]
16 public byte[] GetDataSetBytes()
17 {
18 DataSet ds = GetDataSet();
19 BinaryFormatter ser = new BinaryFormatter(); //序列化对象
20 MemoryStream ms = new MemoryStream(); //内存流
21 ser.Serialize(ms, ds);
22 byte[] buffer = ms.ToArray(); //字节流
23 return buffer;
24 }
25
26 [WebMethod(Description = "直接返回DataSetSurrogate对象,并用Binary序列化后的字节数组")]
27 public byte[] GetDataSetSurrogateBytes()
28 {
29 DataSet ds = GetDataSet();
30 DataSetSurrogate dss = new DataSetSurrogate(ds);
31 BinaryFormatter ser = new BinaryFormatter(); //序列化对象
32 MemoryStream ms = new MemoryStream(); //内存流
33 ser.Serialize(ms, dss);
34 byte[] buffer = ms.ToArray(); //字节流
35 return buffer;
36
37 }
38
39 [WebMethod(Description = "直接返回DataSetSurrogate对象,并用Binary序列化后并且ZIP压缩的字节数组")]
40 public byte[] GetDataSetSurrogateZipBytes()
41 {
42 DataSet ds = GetDataSet();
43 DataSetSurrogate dss = new DataSetSurrogate(ds);
44 BinaryFormatter ser = new BinaryFormatter(); //序列化对象
45 MemoryStream ms = new MemoryStream(); //内存流
46 ser.Serialize(ms, dss);
47 byte[] buffer = ms.ToArray(); //字节流
48 byte[] bufferZip = ComPress(buffer);
49 return buffer;
50 }
51 //压缩方法
52 public byte[] ComPress(byte[] data)
53 {
54 try
55 {
56 MemoryStream ms = new MemoryStream();
57 Stream zipStream = null;
58 zipStream = new GZipStream(ms, CompressionMode.Compress, true);
59 zipStream.Write(data, 0, data.Length);
60 zipStream.Close();
61 ms.Position = 0;
62 byte[] compressed_data = new byte[ms.Length];
63 ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
64 return compressed_data;
65 }
66 catch
67 {
68 return null;
69 }
70 }
我们可以在浏览器中查看下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 + "条记录";
}
{
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;
}
}
}
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;
}
}
}
在上例中,调用四个方法的效果是一样的,唯一不同的是,传输过程中,数据量大小和传输时间的差异。
浅谈WebService返回数据效率对比
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。