WebService在NET中的应用(二)

WebService在NET中的应用(一)对WebService进行了简单介绍,下面将对WebService性能大数据量网络传输处理进行总结,主要分为以下几个部分:

首先,新建一个解决方案,接着”添加“->”新建项目“



上图中,新建了一个”ASP.NET Web服务应用程序“项目,名称可以自己修改,确定后,查看解决方案资源管理器



打开DataSetService.asmx,查看代码

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace DataSetWebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class DataSetService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

一个简单的Web服务程序建立完成,里面有一个HelloWorld方法,接下来需要添加属于自己的方法


一、直接返回DataSet对象

[WebMethod(Description="直接返回DataSet对象")]
        public DataSet GetDataSet()
        {
            string sql = "select * from 表";
            SqlConnection conn = new SqlConnection("连接字符串");
            conn.Open();
            SqlDataAdapter dataAd = new SqlDataAdapter(sql, conn);
            DataSet DS = new DataSet("表名");
            dataAd.Fill(DS);
            conn.Close();
            return DS;
        }


二、返回DataSet对象用Binary序列化后的字节数组

[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;
        }



三、返回DataSetSurrogate对象用Binary序列化后的字节数组

[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;
        }



四、返回DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组

[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[] Zipbuffer = Compress(buffer);
            return Zipbuffer;
        }


public byte[] Compress(byte[] data)
        {
            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;
        }



一、二、三、四都是我们自己的方法,那么又该如何去调用WebService呢?这时候我们新建一个窗体程序,窗体程序建好后,添加Web引用

点击”此解决方案中的Web服务“,然后找到DataSetService,添加即可。


窗体后台

private void BindDataSet(DataSet DS)
        {
            this.dataGridView1.DataSource = DS.Tables[0];
        }


        private void button1_Click(object sender, EventArgs e)
        {
            DataSetService ds = new Test.DataSetService();
            DateTime dtBegin = DateTime.Now;
            DataSet DS = ds.GetDataSet();
            this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
            BindDataSet(DS);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataSetService ds = new Test.DataSetService();
            DateTime dtBegin = DateTime.Now;
            byte[] buffer = ds.GetDataSetBytes();
            DataSet DS = ds.GetDataSet();
            BinaryFormatter ser = new BinaryFormatter();
            DataSet dataset = ser.Deserialize(new MemoryStream(buffer)) as DataSet;
            this.label2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + buffer.Length.ToString());
            BindDataSet(DS);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataSetService ds = new Test.DataSetService();
            DateTime dtBegin = DateTime.Now;
            byte[] buffer = ds.GetDataSetSurrogateBytes();
            BinaryFormatter ser = new BinaryFormatter();
            DataSet DS = ds.GetDataSet();
            DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
            DataSet dataset = dss.ConvertToDataSet();
            this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + buffer.Length.ToString());
            BindDataSet(DS);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataSetService ds = new Test.DataSetService();
            DateTime dtBegin = DateTime.Now;
            byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();
            byte[] buffer = UnZipClass.Decompress(zipBuffer);
            BinaryFormatter ser = new BinaryFormatter();
            DataSet DS = ds.GetDataSet();
            DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
            DataSet dataset = dss.ConvertToDataSet();
            this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "          " + zipBuffer.Length.ToString());
            BindDataSet(DS);
        }


button1_Click、button2_Click、button3_Click、button4_Click依次对应上面的一、二、三、四(4个方法),在第四个方法中进行了ZIP压缩,获取数据就要解压,解压类中的方法如下:

public static class UnZipClass
    {
        /// <summary>
        /// Decompresses the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        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 = EtractBytesFormStream(zipStream, data.Length);
                return dc_data;
            }
            catch
            {
                return null;
            }
        }


        public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
        {
            try
            {
                byte[] data = null;
                int totalBytesRead = 0;
                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;
            }
        }
    }


评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值