[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; } }
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; } } }
01.
using
System;
02.
using
System.Web;
03.
using
System.Web.Services;
04.
using
System.Web.Services.Protocols;
05.
06.
[WebService(Namespace =
"http://tempuri.org/"
)]
07.
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
08.
//若要允许使用 <a href="http://www.it165.net/pro/webasp/" target="_blank" class="keylink">ASP</a>.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
09.
// [System.Web.Script.Services.ScriptService]
10.
public
class
GetWebService : System.Web.Services.WebService
11.
{
12.
13.
[WebMethod]
14.
public
string
HelloWorld()
15.
{
16.
int
res = 0;
17.
for
(
long
i = 0; i < 1000000000; i++)
//循环10亿次,目的是模仿大批量操作,这里至少需要数秒的操作以便看出异步的效果
18.
{
19.
res++;
20.
}
21.
return
" Hello World"
;
22.
}
23.
24.
}
01.
localhost.GetWebService webservice =
new
localhost.GetWebService();
//通过2.0的添加WEB引用需要这种方式new出webservice对象
02.
03.
ServiceReference1.GetWebServiceSoapClient getWebService =
new
ServiceReference1.GetWebServiceSoapClient();
//通过添加服务引用需要这种方式new出webservice对象
04.
05.
//同步调用webservice
06.
private
void
btnSyn_Click(
object
sender, EventArgs e)
07.
{
08.
string
res = webservice.HelloWorld();
09.
this
.textBox1.Text +=
"完成了"
;
10.
this
.textBox1.Text += res + System.Environment.NewLine;
11.
}
12.
13.
//异步调用webservice
14.
private
void
btnAsyn_Click(
object
sender, EventArgs e)
15.
{
16.
//给HelloWorld方法注册调用完成时执行的方法AsyncHelloWorldComplete
17.
webservice.HelloWorldCompleted +=
new
localhost.HelloWorldCompletedEventHandler(AsyncHelloWorldComplete);
18.
//开始异步调用
19.
webservice.HelloWorldAsync();
20.
this
.textBox1.Text +=
"完成了"
+ System.Environment.NewLine;
21.
}
22.
23.
//完成webservice操作时会执行的方法
24.
void
AsyncHelloWorldComplete(
object
sender, localhost.HelloWorldCompletedEventArgs e)
25.
{
26.
string
res = e.Result;
27.
this
.textBox1.Text += res + System.Environment.NewLine;
28.
}