Comparison of four methods for commonly used WebServices to return data

Source: Internet
Author: User

1. directly return the DataSet object

 

Feature: the DataSet object is returned directly.

Application scenarios: 1. intranet. 2. When the Internet and the data size are kb.

2. Return the byte array after the DataSet object is serialized using binary.

Features: The processing mode of byte array streams.

Application Scenario: big data exchange.

3. Return the byte array after the datasetsurrogate object is serialized using binary.

Features: serialization using open-source components provided by Microsoft is still the processing mode of byte streams. For more information, see: http://support.microsoft.com/kb/829740/zh-cn

Application Scenario: big data exchange.

4. Return the byte array after the datasetsurrogate object is serialized and zip in binary format.

Features: the open-source component provided by Microsoft is used to compress and transmit byte stream arrays, which is still the processing mode of byte streams. For more information, see: http://support.microsoft.com/kb/829740/zh-cn

Application Scenario: This method is recommended when the Internet environment needs to transmit large data volumes of network data. This is also a method that I strongly recommend to you.

The WebServices code is as follows:

  1. WebServices
  2. Using system;
  3. Using system. Collections. Generic;
  4. Using system. Web;
  5. Using system. Web. Services;
  6. Using system. Data;
  7. Using Microsoft. Practices. enterpriselibrary. Common;
  8. Using Microsoft. Practices. enterpriselibrary. Data;
  9. Using system. IO;
  10. Using system. Io. compression;
  11. Using system. runtime. serialization. formatters. Binary;
  12. Namespace webservice1
  13. {
  14. /// <Summary>
  15. /// Summary of service1
  16. /// </Summary>
  17. [WebService (namespace = "http://tempuri.org/")]
  18. [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
  19. [System. componentmodel. toolboxitem (false)]
  20. Public class service1: system. Web. Services. WebService
  21. {
  22. [Webmethod (description = "datasdatasdatas datasdatasdatas")]
  23. Public dataset getdataset ()
  24. {
  25. String SQL = "select * from MERs ";
  26. Database DB = databasefactory. createdatabase ();
  27. Dataset DS = dB. executedataset (commandtype. Text, SQL );
  28. Return Ds;
  29. }
  30. [Webmethod (description = "Returned DataSet object byte array after binary serialization")]
  31. Public byte [] getbytes ()
  32. {
  33. Dataset DS = getdataset ();
  34. Binaryformatter BF = new binaryformatter ();
  35. Memorystream MS = new memorystream ();
  36. BF. serialize (MS, DS );
  37. Byte [] buffer = Ms. toarray ();
  38. Return buffer;
  39. }
  40. [Webmethod (description = "returns the byte array of the datasetsurrogate object serialized in binary")]
  41. Public byte [] getdatasetsurrogatebytes ()
  42. {
  43. Dataset DS = getdataset ();
  44. Datasetsurrogate DSS = new datasetsurrogate (DS );
  45. Binaryformatter BF = new binaryformatter ();
  46. Memorystream MS = new memorystream ();
  47. BF. serialize (MS, DSS );
  48. Byte [] buffer = Ms. toarray ();
  49. Return buffer;
  50. }
  51. [Webmethod (description = "returns the datasetsurrogate object serialized in binary and zip compressed byte array")]
  52. Public byte [] getdatasetsurrogatezipbytes ()
  53. {
  54. Dataset DS = getdataset ();
  55. Datasetsurrogate DSS = new datasetsurrogate (DS );
  56. Binaryformatter BF = new binaryformatter ();
  57. Memorystream MS = new memorystream ();
  58. BF. serialize (MS, DSS );
  59. Byte [] buffer = Ms. toarray ();
  60. Byte [] zipbuffer = compress (buffer );
  61. Return zipbuffer;
  62. }
  63. // Compressed byte array
  64. Public byte [] compress (byte [] data)
  65. {
  66. Memorystream MS = new memorystream ();
  67. Stream zipstream = new gzipstream (MS, compressionmode. Compress, true );
  68. Zipstream. Write (data, 0, Data. Length );
  69. Zipstream. Close ();
  70. Ms. Position = 0;
  71. Byte [] buffer = new byte [Ms. Length];
  72. Ms. Read (buffer, 0, Int. parse (Ms. length. tostring ()));
  73. Return buffer;
  74. }
  75. }
  76. }

Copy code

The code for the client to call WebServices is as follows:

 

The client calls WebServices.

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. Web;
  4. Using system. Web. UI;
  5. Using system. Web. UI. webcontrols;
  6. Using webservicesclient. localhost;
  7. Using system. Data;
  8. Using system. runtime. serialization. formatters. Binary;
  9. Using system. IO;
  10. Using system. diagnostics;
  11. Namespace webservicesclient
  12. {
  13. Public partial class _ default: system. Web. UI. Page
  14. {
  15. Service1 S = new service1 ();
  16. Protected void page_load (Object sender, eventargs E)
  17. {
  18. }
  19. // Directly return the DataSet object
  20. Protected void button#click (Object sender, eventargs E)
  21. {
  22. Stopwatch Sw = new stopwatch ();
  23. Sw. Start ();
  24. Dataset DS = S. getdataset ();
  25. Gridview1.datasource = Ds. Tables [0]. defaultview;
  26. Gridview1.databind ();
  27. Sw. Stop ();
  28. Label1.text = string. Format ("Time consumed: {0} millisecond", SW. elapsedmilliseconds. tostring ());
  29. }
  30. // Obtain the byte array after the DataSet object is serialized using binary.
  31. Protected void button2_click (Object sender, eventargs E)
  32. {
  33. Stopwatch Sw = new stopwatch ();
  34. Sw. Start ();
  35. Byte [] buffer = S. getbytes ();
  36. Binaryformatter BF = new binaryformatter ();
  37. Dataset DS = BF. deserialize (New memorystream (buffer) as dataset;
  38. Gridview1.datasource = Ds. Tables [0]. defaultview;
  39. Gridview1.databind ();
  40. Sw. Stop ();
  41. Label2.text = string. Format ("Time consumed: {1} millisecond; data size: {0}", buffer. length. tostring (), SW. elapsedmilliseconds. tostring ());
  42. }
  43. // Obtain the byte array after the datasetsurrogate object is serialized using binary.
  44. Protected void button3_click (Object sender, eventargs E)
  45. {
  46. Stopwatch Sw = new stopwatch ();
  47. Sw. Start ();
  48. Byte [] buffer = S. getdatasetsurrogatebytes ();
  49. Binaryformatter BF = new binaryformatter ();
  50. Datasetsurrogate DSS = BF. deserialize (New memorystream (buffer) as datasetsurrogate;
  51. Dataset DS = DSS. converttodataset ();
  52. Gridview1.datasource = Ds. Tables [0]. defaultview;
  53. Gridview1.databind ();
  54. Sw. Stop ();
  55. Label3.text = string. Format ("Time consumed: {1} millisecond; data size: {0}", buffer. length. tostring (), SW. elapsedmilliseconds. tostring ());
  56. }
  57. // Obtain the byte array after the datasetsurrogate object is serialized and zip by binary.
  58. Protected void button4_click (Object sender, eventargs E)
  59. {
  60. Stopwatch Sw = new stopwatch ();
  61. Sw. Start ();
  62. Byte [] zipbuffer = S. getdatasetsurrogatezipbytes ();
  63. Byte [] buffer = unzip. Decompress (zipbuffer );
  64. Binaryformatter BF = new binaryformatter ();
  65. Datasetsurrogate DSS = BF. deserialize (New memorystream (buffer) as datasetsurrogate;
  66. Dataset DS = DSS. converttodataset ();
  67. Gridview1.datasource = Ds. Tables [0]. defaultview;
  68. Gridview1.databind ();
  69. Sw. Stop ();
  70. Label4.text = string. Format ("Time consumed: {1} millisecond; data size: {0}", zipbuffer. length. tostring (), SW. elapsedmilliseconds. tostring ());
  71. }
  72. }
  73. }

Copy code

The test results are shown in sequence:

 

2009-4-13.jpg(33.94 K) 12:21:12

 

Special instructions on the test results: because the test environment is local and the data volume is not large, the test results are not very close to the actual situation. If you have any conditions, you can test them, we also hope to provide the test results for your reference. (Wen/Shen Shan laolin)

The development environment is vs2008 SP1 + sqlserver2008sp1. The database is the northwind database.

 

Original article: http://www.pin5i.com/showtopic-23733.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.