zoukankan      html  css  js  c++  java
  • Web Service 性能提升方式

    1、创建项目,其项目结构如下:

    项目结构说明:

         DataSetWebService   为Web Service Sites ,用于提供服务;

         Test     为windows form 测试程序, 用于测试效果。

    服务提供程序代码

    DataSetService.asmx

    <%@ WebService Language="C#" CodeBehind="DataSetService.asmx.cs" Class="DataSetWebService.DataSetService" %>

    DataSetService.asmx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    using System.IO;
    using System.Data.SqlClient;
    using System.IO.Compression;
    using System.Data;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace DataSetWebService
    {
        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class DataSetService : System.Web.Services.WebService
        {
    
            [WebMethod(Description="Directly return the dataset object.")]
            public DataSet GetDataSet()
            {
                string sql = "Select * from ShangJianData";
                SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=WL_Data;");
                sqlConn.Open();
    
                SqlDataAdapter sda = new SqlDataAdapter(sql, sqlConn);
                DataSet ds = new DataSet("ShangJianData");
                sda.Fill(ds);
                sqlConn.Close();
                return ds;
            }
    
            [WebMethod(Description = "Binary formatter.")]
            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 = "Dataset Surrogate formatter.")]
            public byte[] GetDataSetSurrogateBytes()
            {
                DataSet ds = GetDataSet();
                DataSetSurrogate dss = new DataSetSurrogate(ds);
                BinaryFormatter ser = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                ser.Serialize(ms, dss);
                byte[] bytes = ms.ToArray();
                return bytes;
            }
    
    
            [WebMethod(Description = "Dataset Surrogate formatter and then compress array.")]
            public byte[] GetDataSetSurrogateZipBytes()
            {
                DataSet ds = GetDataSet();
                DataSetSurrogate dss = new DataSetSurrogate(ds);
                BinaryFormatter ser = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                ser.Serialize(ms, dss);
                byte[] bytes = ms.ToArray();
                byte[] Zipbuffer = Compress(bytes);
    
                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;
            }
    
        }
    }

     客户端测试程序代码如下:

    Form.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    using System.IO;
    using System.Data.SqlClient;
    using System.IO.Compression;
    using System.Data;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace Test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void BindDataSet(DataSet ds)
            {
                this.dataGridView1.DataSource = ds.Tables[0];
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient();
                DateTime dtBegin = DateTime.Now;
                DataSet ds = cdw.GetDataSet();
                this.label1.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin);
                BindDataSet(ds);
               
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient();
                DateTime dtBegin = DateTime.Now;
                byte[] buffer = cdw.GetDataSetBytes();
                BinaryFormatter ser = new BinaryFormatter();
                DataSet ds = 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)
            {
                com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient();
                DateTime dtBegin = DateTime.Now;
                byte[] buffer = cdw.GetDataSetSurrogateBytes();
                BinaryFormatter ser = new BinaryFormatter();
                DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
                DataSet ds = dss.ConvertToDataSet();
                this.label3.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin +"大小:" + buffer.Length.ToString());
                BindDataSet(ds);
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                com.dzbsoft.www.DataSetServiceSoapClient cdw = new com.dzbsoft.www.DataSetServiceSoapClient();
                DateTime dtBegin = DateTime.Now;
                byte[] zipbuffer = cdw.GetDataSetSurrogateZipBytes();
                byte[] buffer = UnZipClass.Decompress(zipbuffer);
                BinaryFormatter ser = new BinaryFormatter();
                DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
                DataSet ds = dss.ConvertToDataSet();
                this.label4.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin + "大小:" + buffer.Length.ToString());
                BindDataSet(ds);
            }
    
    
    
    
    
        }
    }


    UnZipClass.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
    
    namespace Test
    {
        public static  class UnZipClass
        {
            public static byte[] Decompress(byte[] data)
            {
                try
                {
                    MemoryStream ms = new MemoryStream(data);
                    Stream zipStream = new GZipStream(ms, CompressionMode.Decompress);
                    byte[] dc_data = EtractBytesFormStream(zipStream, data.Length);
                    return dc_data;
                }
                catch (System.Exception ex)
                {
                    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 (System.Exception ex)
                {
                    return null;
                }
    
            }
        }
    }
  • 相关阅读:
    20080531 Windows 下安装 Bugzilla
    20080823 windows + apache + mod_python 的安装
    20080519 在 Windows Server 2003 下安装 SQL Server 2000 提示“无法验证产品密钥”
    20080508 Borland CodeGear 卖了
    20080520 Javascript 随机数产生办法
    20090613 批量操作 Windows Live Mail 邮件的办法
    20080726 Castle项目创始人加入微软
    20080511 php send_mail()
    20080618 ASP.NET Ajax clientside framework failed to load
    20081105 Microsoft Word 2007 中鼠标操作失效的解决办法
  • 原文地址:https://www.cnblogs.com/tianjinquan/p/2646624.html
Copyright © 2011-2022 走看看