Detailed steps for WCF large-volume data transfer

Source: Internet
Author: User
Tags serialization

1. Create a new Blank solution solution wcfsolution.

2. Add a class library project Dblibrary

First, add a serialization and reverse Sequence object class Myserialize

<summary>///Serialization Object///</summary> public static byte[] Serializearray<t> (T dataval
           UES) {byte[] b;
           Create a memory stream MemoryStream ms = new MemoryStream ();
               try {//Serialization object BinaryFormatter BinaryFormatter =new BinaryFormatter ();
               Serializes an object into a memory stream binaryformatter.serialize (MS, DataValues); Sets the starting position of the memory stream Ms.
               Position = 0; Read to byte array b = new byte[ms.
               Length]; Ms.
           Read (b, 0, b.length);
           catch (Exception ex) {throw ex; finally {Ms.
               Close (); Ms.
           Dispose ();
        return b; ///<summary>///deserialized object///</summary> public static T deserializearray<t
         > (byte[] datavalues) {  T mydatavalues;
           Create a memory stream MemoryStream ms = new MemoryStream ();
               try {//Serialization object BinaryFormatter BinaryFormatter =new BinaryFormatter (); Converts a byte array to a memory stream Ms.
               Write (datavalues, 0, datavalues.length); The location of the memory stream to the start of Ms.
               Position = 0;
           Deserialize into an object, creating exactly the same copy as the original object mydatavalues = (T) binaryformatter.deserialize (MS);
           catch (Exception ex) {throw ex; finally {Ms.
               Close (); Ms.
           Dispose ();
        return mydatavalues; }


Second, add a data structure class dbdata (can be declared according to the specific requirements)

[Serializable]
    public class Dbdata
    {public
        double begintime;
        Public ilist<double> TimeSpan;
     
    }

The third adds an Organization data class Dbhelp

Public classdbhelp
    {
       ///<summary>
        ///Serialization Object
        ///</summary> public
    static Dbdata GetData ()
        {return
           new dbdata ();  Specific data add yourself
        }
 
     
    }

3. Add a class library services in Wcfsolution, which is primarily used to define contracts.

Add a Igetbyte interface

<summary>
    ///service contract
    ///</summary>
    [ServiceContract (Name = "Igetbyte", Namespace = "http://") Www.weifangyh.com ")] public
    interface Igetbyte
    {
        [OperationContract]
      byte[]getdbdata ();
    }

Add a GetByte class to implement the Igetbyte interface

  public class Getbyte:igetbyte
    {public
        byte[]getdbdata ()
        {
           Dbdata db = Newdbhelp.getdata ();
           
           return Myserialize.serializearray (db);
        }
  


4. Add a console Application host in Wcfsolution, primarily as a host to start a WCF program.

A Add a app.config to configure server settings

<?xmlversion= "1.0"?> <configuration> <system.serviceModel> <bindings> <basichttpbin ding> <binding name= "basehttpbinding" transfermode= "streamed" maxbuffersize= "2147483647" maxReceivedMess Agesize= "2147483647" messageencoding= "Text" > <readerquotas maxdepth= "maxstringcontentlength=" 214 7483647 "maxarraylength=" 2147483647 "maxbytesperread=" 2147483647 "maxnametablecharcount=" 214748
     3647 "/> </binding> </basicHttpBinding> </bindings> <services> <service name= "Services.getbyte" behaviorconfiguration= "ServiceBehavior" > <endpoint address= "GetByte" "Binding=" BasicHttpBinding "contract=" Services.igetbyte "> </endpoint> <endpoin T address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>  


Two Write the start service code in the Main method

static void Main (String[]args)
        {
   ServiceHost host = Newservicehost (typeof (Getproductsslist));
           Host. Open ();
           Console.WriteLine (the service is started.) ");
}


5. Add a asp.net web site as a client in wcfsolution.

Run the host host first, add a service reference to the site, enter the service address in the address http://localhost:8050/GetByte go and determine

Automatically generate Web.config configuration files, where you need to modify some of the configuration to achieve the acceptance of large amounts of data, the configuration file is as follows:

<system.serviceModel> <behaviors> <endpointbehaviors > <behaviorna
              Me= "Clientbehavior" > <datacontractserializer maxitemsinobjectgraph= "2147483647"/> </behavior> </endpointBehaviors> </behaviors> <bindings> <ba sichttpbinding> <binding name= "Basichttpbinding_getbyte" closetimeout= "00:01:00" openTimeout= "00: 01:00 "receivetimeout=" 00:10:00 "sendtimeout= 00:10:00" allowcookies= "false" Bypassproxyonlocal= "false" HostName Comparisonmode= "StrongWildcard" maxbuffersize= "2147483647" maxbufferpoolsize= "2147483647" MaxReceivedMessageSiz E= "2147483647" messageencoding= "Text" textencoding= "Utf-8" transfermode= "streamed" Usedefaultwebproxy = "true" > <readerquotas maxdepth= "2147483647" maxstringcontentlength= "2147483647" Maxarrayleng Th= "2147483647" MaxBytesPerread= "2147483647" maxnametablecharcount= "2147483647"/> <security mode= "None" > <trans Port clientcredentialtype= "None" proxycredentialtype= "None" realm= ""/> <message clientcre Dentialtype= "UserName" algorithmsuite= "Default"/> </security> </binding> </basic 
        httpbinding> </bindings> <client> <endpoint address= "Http://localhost:8050/GetByte" binding= "BasicHttpBinding" bindingconfiguration= "Basichttpbinding_ getbyte" contract= "ServiceReference_ GetByte . GetByte "Name=" Basichttpbinding_ getbyte "/> </client> </system.serviceModel>

Then create a new page page that enables you to get data with service communication in your code

Getbyteclient getlistclient =newgetbyteclient ();
Dbdata  db= myserialize. deserializearray< dbdata> (Getlistclient.getdbdata ())

If you have any questions, please leave a message.

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.