WebService-serialization and deserialization (optimized for online dataset)

Source: Internet
Author: User
Only return forms of the four types of data are listed:

(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.

Datasetsurrogate class: the class for compressing dataset provided by Microsoft at 2.0. Download/files/fujinliang/datasetsurrogate.rar

The Code is as follows:

 

[Webmethod (description = "datasdatasdatas datasdatasdatas")]
Public dataset getdataset ()
{
String connstr = system. configuration. configurationmanager. receivettings ["connstring"]. tostring ();
Sqlconnection conn = new sqlconnection (connstr );
String SQL = "select * From base_dic ";
Conn. open ();
Sqldataadapter SDA = new sqldataadapter (SQL, Conn );
Dataset DS = new dataset ("China ");
SDA. Fill (DS );
Conn. Close ();
Return Ds;
}

[Webmethod (description = "directly return the DataSet object and use the byte array after binary serialization")]
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 = "directly returning the datasetsurrogate object and the byte array after binary serialization")]
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;
}

[Webmethod (description = "directly returning the datasetsurrogate object, and compressing the byte array after binary serialization")]
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 [] bufferzip = compress (buffer );
Return bufferzip;
}

// Compression Method
Public byte [] compress (byte [] data)
{
Try
{
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;
}
Catch
{
Return NULL;
}
}

The above code is relatively large on the Internet, and the transmission of objects can be optimized after testing. The data tested above is as follows:

(1) directly return the DataSet object

The speed is 00:00:01.
(2) return the byte array after the DataSet object is serialized using binary.

The speed is faster than (1), and the transfer size is 30589.
(3) return the byte array after the datasetsurrogate object is serialized using binary.

The speed is faster than (1), and the transfer size is 12450.
(4) return the byte array after the datasetsurrogate object is serialized and zip in binary format.

The speed is faster than (1), and the transfer size is 4540.

The speed of the four methods is similar to that of the 2, 3, and 4, but my data volume is not large. In the case of a large amount of data, this advantage will be significantly improved, so we recommend that you use (4) method to increase the performance of Object Instantiation

 

The above example only optimizes dataset. We know that if the composite type is directly used in WebService, a large amount of WSDL description will be generated, so the generated XML will be very large,

To optimize the composite type of WebService, the class library must be the same on the client and server, and must be marked as [serialization]. Otherwise, an error will be reported, in the datasetsurrogate above, a new class library must be created and referenced on the client and server.

For other object objects, we can also implement composite types through serialization.

Disadvantages: WebService provides a fast and convenient method for calling between different languages. However, in cross-platform scenarios, the serialization and deserialization methods are different for Binary Conversion, therefore, this function will be restricted, depending on the business requirements of the specific platform.

 

Example program: Server/files/fujinliang/webservice.rar

Client:/files/fujinliang/windowsformsapplication1.rar

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.