Introduction to WebService development (1)

Source: Internet
Author: User

1. What is WebService:

To put it simply, enterprises and websites access and use online services over the Internet. Some data cannot be provided to other organizations due to security issues, in this case, the WebService can be used to provide the service.

2. Create a WebService

After creating WebService, we can write the method for returning data in the file.

3. Four forms of returned data

The author's level is limited and only lists the return forms of the four types of data:

(1) directly return the DataSet object
(2) return the byte array after the DataSet object is serialized using Binary.
(3) return the byte array after the DataSetSurrogate object is serialized using Binary.
(4) return the byte array after the DataSetSurrogate object is serialized and Zip in Binary format.

Theoretically speaking, the network transmission byte and transmission time should be decreasing, among which (3) (4) method needs to reference the open source components provided by Microsoft: http://support.microsoft.com/kb/829740/zh-cn

The following shows the code for the four types of returned data. (1) is the root of the three methods. You must obtain a DataSet as the root, and then perform various conversion and compression operations:

1 [WebMethod (Description = "datasdatasdatas datasdatasdatas")]
2 public DataSet GetDataSet ()
3 {
4 string connStr = System. Configuration. ConfigurationManager. ConnectionStrings ["conn"]. ToString ();
5 SqlConnection conn = new SqlConnection (connStr );
6 string SQL = "select * from china_city ";
7 conn. Open ();
8 SqlDataAdapter sda = new SqlDataAdapter (SQL, conn );
9 DataSet ds = new DataSet ("China ");
10 sda. Fill (ds );
11 conn. Close ();
12 return ds;
13}
14
15 [WebMethod (Description = "directly return the DataSet object and use the byte array after Binary serialization")]
16 public byte [] GetDataSetBytes ()
17 {
18 DataSet ds = GetDataSet ();
19 BinaryFormatter ser = new BinaryFormatter (); // serialize the object
20 MemoryStream MS = new MemoryStream (); // memory stream
21 ser. Serialize (MS, ds );
22 byte [] buffer = ms. ToArray (); // byte stream
23 return buffer;
24}
25
26 [WebMethod (Description = "directly return the DataSetSurrogate object and use the Binary serialized byte array")]
27 public byte [] GetDataSetSurrogateBytes ()
28 {
29 DataSet ds = GetDataSet ();
30 DataSetSurrogate dss = new DataSetSurrogate (ds );
31 BinaryFormatter ser = new BinaryFormatter (); // serialize the object
32 MemoryStream MS = new MemoryStream (); // memory stream
33 ser. Serialize (MS, dss );
34 byte [] buffer = ms. ToArray (); // byte stream
35 return buffer;
36
37}
38
39 [WebMethod (Description = "directly return the DataSetSurrogate object and use Binary to serialize and ZIP the compressed byte array")]
40 public byte [] GetDataSetSurrogateZipBytes ()
41 {
42 DataSet ds = GetDataSet ();
43 DataSetSurrogate dss = new DataSetSurrogate (ds );
44 BinaryFormatter ser = new BinaryFormatter (); // serialize the object
45 MemoryStream MS = new MemoryStream (); // memory stream
46 ser. serialized (MS, dss );
47 byte [] buffer = ms. ToArray (); // byte stream
48 byte [] bufferZip = ComPress (buffer );
49 return buffer;
50}
51 // Compression Method
52 public byte [] ComPress (byte [] data)
53 {
54 try
55 {
56 MemoryStream MS = new MemoryStream ();
57 Stream zipStream = null;
58 zipStream = new gzistream (MS, CompressionMode. Compress, true );
59 zipStream. Write (data, 0, data. Length );
60 zipStream. Close ();
61 ms. Position = 0;
62 byte [] compressed_data = new byte [ms. Length];
63 ms. Read (compressed_data, 0, int. Parse (ms. Length. ToString ()));
64 return compressed_data;
65}
66 catch
67 {
68 return null;
69}
70}

 

We can view the effect of WebService in the browser. There are four methods available on this page. These four methods are the four methods we wrote to return data, click the method to return the corresponding data. In this way, we can write the code of the data provider. Next, we will write the method for calling the data!

 

Iv. Call data

Client WebService Program

 

Private void button#click (object sender, EventArgs e)
{
Com. dzbsoft. www. Service1 ds = new com. dzbsoft. www. Service1 (); // new outputs the WebService object
DateTime dtBegin = DateTime. Now;
DataSet dataSet = ds. GetNorthwindDataSet ();
This. label1.Text = string. Format ("Time consumed: {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 ("Time consumed: {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 ("Time consumed: {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 ("Time consumed: {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 + "record ";
}

 

 

In the data return method, we use data compression. Therefore, on the caller side, we need to decompress the code: client UnZipClass program.
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;
}
}
}

 

In the above example, the effects of calling the four methods are the same. The only difference is that the data size and transmission time are different during transmission.

 

Declaration: this blog is Copy of DotNetGeek.cn, original link http://www.dotnetgeek.cn/xuexiwebservice1.html

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.