WebService uses dataset-uses binaryformatter to serialize to reduce the stream size

Source: Internet
Author: User
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

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.