WCF大数据量传输的详细步骤

1.  新建一个空白解决方案解决方案WcfSolution。

2.  添加一个类库项目DBLibrary

首先添加一个序列化和反序列对象类MySerialize

[csharp] view plain copy
  1. ///<summary>  
  2.         ///序列化对象  
  3.         ///</summary>  
  4.     public static byte[] SerializeArray<T>(T DataValues)  
  5.         {  
  6.            byte[] b;  
  7.            // 创建一个内存流  
  8.            MemoryStream ms = new MemoryStream();  
  9.            try  
  10.            {  
  11.                // 序列化对象  
  12.                BinaryFormatter binaryFormatter =newBinaryFormatter();  
  13.                // 将对象序列化为内存流  
  14.                binaryFormatter.Serialize(ms, DataValues);  
  15.                // 设置内存流的起始位置  
  16.                ms.Position = 0;  
  17.                // 读入到byte数组  
  18.                b = new byte[ms.Length];  
  19.                ms.Read(b, 0, b.Length);  
  20.            }  
  21.            catch (Exceptionex)  
  22.            {  
  23.                throw ex;  
  24.            }  
  25.             finally  
  26.            {  
  27.                ms.Close();  
  28.                ms.Dispose();  
  29.            }  
  30.            return b;  
  31.         }  
  32.    
  33.         ///<summary>  
  34.         ///反序列化对象  
  35.         ///</summary>  
  36.         public static TDeserializeArray<T>(byte[] DataValues)  
  37.         {  
  38.            T MyDataValues;  
  39.            // 创建一个内存流  
  40.            MemoryStream ms = new MemoryStream();  
  41.            try  
  42.            {  
  43.                // 序列化对象  
  44.                BinaryFormatter binaryFormatter =newBinaryFormatter();  
  45.                // 将byte数组到内存流               
  46.                ms.Write(DataValues, 0, DataValues.Length);  
  47.                // 将内存流的位置到最开始位置                
  48.                ms.Position = 0;  
  49.                // 反序列化成对象,创建出与原对象完全相同的副本           
  50.                MyDataValues = (T)binaryFormatter.Deserialize(ms);  
  51.            }  
  52.            catch (Exceptionex)  
  53.            {  
  54.                throw ex;  
  55.            }  
  56.            finally  
  57.            {  
  58.                ms.Close();  
  59.                ms.Dispose();  
  60.            }  
  61.            return MyDataValues;  
  62.         }  


其二  添加一个数据结构类DBData(可以根据具体要求来声明)

[csharp] view plain copy
  1. [Serializable]  
  2.     public class DBData  
  3.     {  
  4.         public double BeginTime;  
  5.         public IList<double> TimeSpan;  
  6.        
  7.     }  

其三 添加一个组织数据类DBHelp

[csharp] view plain copy
  1. public classDBHelp  
  2.     {  
  3.        ///<summary>  
  4.         ///序列化对象  
  5.         ///</summary>  
  6.     public static DBData GetData()  
  7.         {  
  8.            return new DBData();  // 具体数据自己添加  
  9.         }  
  10.    
  11.        
  12.     }  

3.  在WcfSolution中添加一个类库Services,主要是用于定义契约。

添加一个IGetByte接口

[csharp] view plain copy
  1. ///<summary>  
  2.     ///服务契约  
  3.     ///</summary>  
  4.     [ServiceContract(Name = " IGetByte ",Namespace ="http://www.weifangyh.com")]  
  5.     public interface IGetByte  
  6.     {  
  7.         [OperationContract]  
  8.       byte[]GetDBData();  
  9.     }  

添加一个GetByte类,实现IGetByte接口

  

[csharp] view plain copy
  1. public class GetByte: IGetByte  
  2.   {  
  3.       public byte[]GetDBData()  
  4.       {  
  5.          DBData db = newDBHelp.GetData();  
  6.            
  7.          return MySerialize.SerializeArray(db);  
  8.       }  
  9. }  


4.  在WcfSolution中添加一个控制台应用程序Host,主要作用是作为启动wcf程序的宿主。

一.添加一个app.config用来配置服务器设置

[html] view plain copy
  1. <?xmlversionxmlversion="1.0"?>  
  2. <configuration>  
  3.  <system.serviceModel>  
  4.    <bindings>  
  5.         <basicHttpBinding>  
  6.             <bindingnamebindingname="basehttpbinding" transferMode="Streamed"maxBufferSize="2147483647"maxReceivedMessageSize="2147483647"messageEncoding="Text">  
  7.                 <readerQuotasmaxDepthreaderQuotasmaxDepth="32"maxStringContentLength="2147483647"maxArrayLength="2147483647"  
  8.                           maxBytesPerRead="2147483647"maxNameTableCharCount="2147483647" />  
  9.             </binding>  
  10.         </basicHttpBinding>  
  11.    </bindings>  
  12.      
  13.    <services>  
  14.      <servicenameservicename="Services.GetByte"behaviorConfiguration="serviceBehavior">  
  15.           <endpointaddressendpointaddress="GetByte" binding="basicHttpBinding"contract="Services.IGetByte">  
  16.               
  17.           </endpoint>  
  18.         <endpointaddressendpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>  
  19.         <host>  
  20.           <baseAddresses>  
  21.             <addbaseAddressaddbaseAddress="http://localhost:8050"/>  
  22.           </baseAddresses>  
  23.         </host>  
  24.      </service>  
  25.    </services>  
  26.    
  27.    <!--这个节使用来配置服务的行为-->  
  28.    <behaviors>  
  29.      <serviceBehaviors>  
  30.         <behaviornamebehaviorname="serviceBehavior">  
  31.           <!--指定元数据使用http的get方式获取-->  
  32.           <serviceMetadatahttpGetEnabledserviceMetadatahttpGetEnabled="true"/>  
  33.             <dataContractSerializermaxItemsInObjectGraphdataContractSerializermaxItemsInObjectGraph="2147483647"/>  
  34.           
  35.         </behavior>  
  36.      </serviceBehaviors>  
  37.    </behaviors>  
  38.  </system.serviceModel>  
  39. <startup><supportedRuntimeversionsupportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.0"/></startup></configuration>  
  40.    


二.在Main方法中写启动服务代码

 

[csharp] view plain copy
  1. static void Main(string[]args)  
  2.         {  
  3.    ServiceHost host = newServiceHost(typeof(GetProductssList));  
  4.            host.Open();  
  5.            Console.WriteLine("服务已启动!");  
  6. }  


5.  在WcfSolution中添加一个asp.net网站当作客户端。

先把Host宿主运行起来,网站中右键添加服务引用,在地址中输入服务地址http://localhost:8050/GetByte前往然后确定

自动生成web.config配置文件,在这里需要修改些配置,以实现接受大数据量,配置文件如下:

[html] view plain copy
  1. <system.serviceModel>  
  2.        
  3.      <behaviors>  
  4.           <endpointBehaviors >  
  5.               <behaviornamebehaviorname="ClientBehavior">  
  6.                   <dataContractSerializermaxItemsInObjectGraphdataContractSerializermaxItemsInObjectGraph="2147483647" />  
  7.               </behavior>  
  8.           </endpointBehaviors>  
  9.            
  10.      </behaviors>  
  11.    <bindings>  
  12.      <basicHttpBinding>  
  13.         <bindingnamebindingname="BasicHttpBinding_GetByte"closeTimeout="00:01:00"  
  14.           openTimeout="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:10:00"  
  15.           allowCookies="false"bypassProxyOnLocal="false"hostNameComparisonMode="StrongWildcard"  
  16.           maxBufferSize="2147483647"maxBufferPoolSize="2147483647"maxReceivedMessageSize="2147483647"  
  17.           messageEncoding="Text"textEncoding="utf-8"transferMode="Streamed"  
  18.           useDefaultWebProxy="true">  
  19.           <readerQuotasmaxDepthreaderQuotasmaxDepth="2147483647"maxStringContentLength="2147483647"  
  20.             maxArrayLength="2147483647"maxBytesPerRead="2147483647"maxNameTableCharCount="2147483647" />  
  21.           <securitymodesecuritymode="None">  
  22.             <transportclientCredentialTypetransportclientCredentialType="None"proxyCredentialType="None"  
  23.               realm="" />  
  24.             <messageclientCredentialTypemessageclientCredentialType="UserName"algorithmSuite="Default" />  
  25.           </security>  
  26.         </binding>  
  27.      </basicHttpBinding>  
  28.    </bindings>  
  29.    <client>  
  30.      <endpointaddressendpointaddress="http://localhost:8050/ GetByte "  
  31.         binding="basicHttpBinding"bindingConfiguration="BasicHttpBinding_ GetByte"  
  32.         contract="ServiceReference_ GetByte.GetByte "  
  33.         name="BasicHttpBinding_ GetByte" />  
  34.    </client>  
  35.  </system.serviceModel>  

然后新建page页面,在代码中实现与服务通信获取数据

  

[csharp] view plain copy
  1. GetByteClient getListClient =newGetByteClient ();  
  2. DBData  db= MySerialize. DeserializeArray< DBData> (getListClient.GetDBData())  

 

一切搞定,如有疑问欢迎留言

转载于:https://www.cnblogs.com/tonykan/archive/2013/01/22/2870802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值