转:WebService常见调用方式

using System.Web.Services;


using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
namespace WebService1
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod(Description="直接返回DataSet对象")]
        public DataSet GetDataSet()
        {
            string sql = "select * from Customers";
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = db.ExecuteDataSet(CommandType.Text,sql);
            return ds;
        }

        [WebMethod(Description = "返回DataSet对象用Binary序列化后的字节数组")]
        public byte[] GetBytes()
        {
            DataSet ds = GetDataSet();
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.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 bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.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 bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, dss);
            byte[] buffer = ms.ToArray();
            byte[] Zipbuffer = Compress(buffer);
            return Zipbuffer;
        }

        //压缩压缩后的字节数组
        public byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            Stream zipStream = new GZipStream(ms, CompressionMode.Compress, true);
            zipStream.Write(data, 0, data.Length);
            zipStream.Close();
            ms.Position = 0;
            byte[] buffer = new byte[ms.Length];
            ms.Read(buffer, 0,int.Parse(ms.Length.ToString()));
            return buffer;
        }
    }
}
//客户端解压
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; } }
//前台调用    
Service1 s = new Service1(); //直接返回DataSet对象 protected void Button1_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); DataSet ds = s.GetDataSet(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop(); Label1.Text = string.Format("耗时:{0}毫秒", sw.ElapsedMilliseconds.ToString()); } //得到DataSet对象用Binary序列化后的字节数组 protected void Button2_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); byte[] buffer = s.GetBytes(); BinaryFormatter bf = new BinaryFormatter(); DataSet ds = bf.Deserialize(new MemoryStream(buffer)) as DataSet; GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop(); Label2.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString()); } //得到DataSetSurrogate对象用Binary序列化后的字节数组 protected void Button3_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); byte[] buffer = s.GetDataSetSurrogateBytes(); BinaryFormatter bf = new BinaryFormatter(); DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet ds = dss.ConvertToDataSet(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop(); Label3.Text = string.Format("耗时:{1}毫秒;数据大小:{0}", buffer.Length.ToString(), sw.ElapsedMilliseconds.ToString()); } //得到DataSetSurrogate对象用Binary序列化并ZIP压缩后的字节数组 protected void Button4_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); byte[] zipBuffer = s.GetDataSetSurrogateZipBytes(); byte[] buffer = UnZip.Decompress(zipBuffer); BinaryFormatter bf = new BinaryFormatter(); DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate; DataSet ds = dss.ConvertToDataSet(); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); sw.Stop(); Label4.Text = string.Format("耗时:{1}毫秒;数据大小:{0}",zipBuffer.Length.ToString(),sw.ElapsedMilliseconds.ToString()); }
原文地址:https://www.cnblogs.com/bantongshui/p/3232044.html