Dataset serialization in WebService
Server code:
========================================================== ================================
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. xml. LINQ;
Using system. xml;
Using system. Data. sqlclient;
Using system. runtime. serialization. formatters. Binary;
Using compressdataset;
Using system. IO;
Namespace webservice1
{
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Toolboxitem (false)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
// [System. Web. Script. Services. scriptservice]
Public class service1: system. Web. Services. WebService
{
[Webmethod (description = "Get dataset and perform operations")]
Public bool backupusertable (byte [] DS)
{
// Get the dataset serialized and deserialized from the user
Memorystream MS = new memorystream (DS );
Binaryformatter BF = new binaryformatter ();
Object o = BF. deserialize (MS );
Datasetsurrogate DSS = (datasetsurrogate) O;
Dataset dataset = DSS. converttodataset ();
// Custom operation content
//***
Return false;
}
}
}
Client code:
========================================================== ======================================
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 compressdataset;
Using system. IO;
Using system. runtime. serialization. formatters. Binary;
Namespace windowsclient
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
/// <Summary>
/// Method for updating the Server Database
/// </Summary>
/// <Param name = "username"> </param>
Public void updateinfo ()
{
// Encapsulate Dataset
Dataset DS = new dataset ();
Datatable table = new datatable ();
Table. Columns. Add ("");
Table. Columns. Add ("B ");
Table. Columns. Add ("C ");
For (INT I = 0; I <10; I ++)
{
Datarow ROW = table. newrow ();
Row ["A"] = I. tostring () + "";
Row ["B"] = I. tostring () + "B ";
Row ["C"] = I. tostring () + "C ";
}
// Serialize the dataset
Datasetsurrogate DSS = new datasetsurrogate (DS );
Memorystream MS = new memorystream ();
Binaryformatter BF = new binaryformatter ();
BF. serialize (MS, DSS );
// Load the stream into the Array
Byte [] IO = Ms. toarray ();
// Call WebService
Servicereferenceuserservice. service1soapclient userservice = new windowsclient. servicereferenceuserservice. service1soapclient ();
// Operation
If (userservice. backupusertable (IO ))
{
MessageBox. Show ("successful! "," Message ", messageboxbuttons. OK, messageboxicon. information );
}
}
}
}
To be referenced:
Datasetsurrogate. dll: http://download.csdn.net/source/2294498
Conclusion:
========================================================== ==============================
The binaryformatter method is used to improve the efficiency.
Furthermore, the datasetsurrogate class provided by Microsoft can further compress the data size. datasetsurrogate comes with. NET 2.0. This is a comparison result.
|
Soapformatter |
Binaryformatter |
Bytes after dataset serialization |
1,953,078 |
1,448,399 |
Number of bytes after datasetsurrogate serialization |
2,371,942 |
575,684 |