First, what is WebService:
In simple terms, the Internet is used to access and use the online services between the enterprises, the Web site, some data, because of security issues, can not provide the database to other organizations to use, when the WebService service is available.
Ii. Creation of WebService
After creating the webservice, we can write the method of returning data in the file.
Iii. four forms of return data
The author's level is limited and only the return forms of these four types of data are listed:
(1) Direct return to the DataSet object
(2) Returns a DataSet object with binary serialized byte array
(3) Returns a byte array with binary serialization of the DataSetSurrogate object (
4) Returns a DataSetSurrogate object with binary serialized and zip-compressed byte array
Theoretically, the network transmission bytes and transmission time should be decremented, wherein, (3) (4) methods need to refer to the open source components provided by Microsoft: HTTP://SUPPORT.MICROSOFT.COM/KB/829740/ZH-CN
The following shows the four kinds of code that returns data, where (1) is the root of its three methods, all of which are to get a dataset as the root and then do the various conversion compression operations:
[WebMethod (Description ="directly returns a DataSet object")] PublicDataSet GetDataSet () {stringConnStr = system.configuration.configurationmanager.connectionstrings["Conn"]. ToString (); SqlConnection Conn=NewSqlConnection (CONNSTR); stringsql ="SELECT * from China_city"; Conn. Open (); SqlDataAdapter SDA=NewSqlDataAdapter (SQL, conn); DataSet DS=NewDataSet (" China"); Sda. Fill (DS); Conn. Close (); returnds; } [WebMethod (Description="directly returns a DataSet object and uses binary serialized byte array")] Public byte[] Getdatasetbytes () {DataSet DS=GetDataSet (); BinaryFormatter Ser=NewBinaryFormatter ();//Serializing ObjectsMemoryStream ms =NewMemoryStream ();//Memory Streamser. Serialize (MS, DS); byte[] buffer = Ms. ToArray ();//Byte stream returnbuffer; } [WebMethod (Description="returns the DataSetSurrogate object directly with a binary serialized byte array")] Public byte[] Getdatasetsurrogatebytes () {DataSet DS=GetDataSet (); DataSetSurrogate DSS=NewDataSetSurrogate (DS); BinaryFormatter Ser=NewBinaryFormatter ();//Serializing ObjectsMemoryStream ms =NewMemoryStream ();//Memory Streamser. Serialize (MS, DSS); byte[] buffer = Ms. ToArray ();//Byte stream returnbuffer; } [WebMethod (Description="returns the DataSetSurrogate object directly and serializes the byte array with binary serialization and zip compression")] Public byte[] Getdatasetsurrogatezipbytes () {DataSet DS=GetDataSet (); DataSetSurrogate DSS=NewDataSetSurrogate (DS); BinaryFormatter Ser=NewBinaryFormatter ();//Serializing ObjectsMemoryStream ms =NewMemoryStream ();//Memory Streamser. Serialize (MS, DSS); byte[] buffer = Ms. ToArray ();//Byte stream byte[] Bufferzip =ComPress (buffer); returnbuffer; } //Compression Method Public byte[] ComPress (byte[] data) { Try{MemoryStream ms=NewMemoryStream (); Stream ZipStream=NULL; ZipStream=NewGZipStream (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 ())); returnCompressed_data; } Catch { return NULL; } }
We can see the effect of the next webservice in the browser, in this page, there are four methods, these four methods is the above we wrote four methods of return data, click the method to return the corresponding data, so that our data provider code can be written, then, We write the method of invoking the data!
Iv. Calling Data
Client WebService Program
private void Button1_Click (object sender, EventArgs e) {Com.dzbsoft.www.Service1 ds = new Com.dzbsoft.www.Service1 ( ); New out WebService object DateTime dtbegin = DateTime.Now; DataSet DataSet = ds. Getnorthwinddataset (); This.label1.Text = string. Format ("time-consuming: {0}", Datetime.now-dtbegin); Binddata (DataSet); private void Button2_Click (object sender, EventArgs e) {Com.dzbsoft.www.Service1 ds = new Com.dzbsoft.www.Servic E1 (); 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 ("time-consuming: {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.Servic E1 (); 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 ("time-consuming: {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.Servic E1 (); 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 ("time-consuming: {0}", Datetime.now-dtbegin) + "" + zipbuffer.length; Binddata (DataSet); } private void Binddata (DataSet dataset) {This.dataGridView1.DataSource = dataset.tableS[0]; This.label5.Text = "Total:" + dataset.tables[0]. Rows.Count + "records"; }
In the method of data return, we use the compression of the data, so, on the caller side, we need to extract the code:
客户端UnZipClass程序
Client Unzipclass Program Public Static classUnzipclass { Public Static byte[] Decompress (byte[] data) { Try{MemoryStream ms=NewMemoryStream (data); Stream ZipStream=NULL; ZipStream=NewGZipStream (MS, compressionmode.decompress); byte[] Dc_data =NULL; Dc_data=Extractbytesfromstream (ZipStream, data. Length); returnDc_data; } Catch { return NULL; } } Public Static byte[] Extractbytesfromstream (Stream zipStream,intDataBlock) { byte[] data =NULL; intTotalbytesread =0; Try { while(true) {array.resize (refData, Totalbytesread + DataBlock +1); intBytesread =zipstream.read (data, Totalbytesread, DataBlock); if(Bytesread = =0) { Break; } totalbytesread+=Bytesread; } array.resize (refdata, Totalbytesread); returndata; } Catch { return NULL; } } }
In the example above, the effect of calling four methods is the same, the only difference being the difference in the amount of data and the transmission time during transmission.
On the development of. Net WebService