WCF使用NetTcp传输文件

简介:

今天看了一些官方的资料和配置,简单写了一个WCF服务来传递一个文件,借此看看WCF传输大文件的能力,这里采用的是NetTcp绑定,之所以没有采用basicHttpBinding是因为考虑这种方式和WebService相近,大家都写的比较多了。

服务实现

服务中有一个上传二进制流的方法UpLoad

[ServiceContract]
public interface IAddService
{
[OperationContract]
void UpLoad(byte[] file);
}

(为了减少时间,采用了一点硬编码)

public class AddService:IAddService
{
public void UpLoad(byte[] file)
{
System.IO.File.WriteAllBytes("d:/8.rmvb", file);//将上传的文件放到D盘下并命名
}
}

服务的配置

App.config是WCF的重头戏,这里的配置直接影响到服务的成败和性能。

先定义一个netTcpBinding供服务使用

<bindings>
<netTcpBinding>
<binding name="netTcpBindConfig"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
transactionFlow="false"
transferMode="Buffered"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
listenBacklog="10"
maxBufferPoolSize="2147483647 "
maxBufferSize="2147483647 "
maxConnections="10"
maxReceivedMessageSize="2147483647 ">
<readerQuotas maxDepth="32"
maxStringContentLength="2147483647 "
maxArrayLength="2147483647 "
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<reliableSession ordered="true"
inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
</security>
</binding>
</netTcpBinding>
</bindings>

这个配置需要注意maxConnections="10" 这个选项,如果你想改成最大连接为100就会在运行时报下面的错误。查了一下MSDN,原来如果是windows7,xp,2000,vista在TCP的同时在线数量是有限制的,超出10就会报错。而如果想要更大的连接数,需要部署到windows server上。 

 如果想传输大文件,下面几个配置也是必不可少的

maxBufferPoolSize="2147483647 "

maxBufferSize="2147483647 "

maxReceivedMessageSize="2147483647 "

当然,还有配额的大小

<readerQuotas maxDepth="32maxStringContentLength="2147483647 maxArrayLength="2147483647 maxBytesPerRead="4096maxNameTableCharCount="16384/>

配置Behavior

<behaviors>
<serviceBehaviors>
<behavior name="WCFLibrary.UpdateUserBehavior">
<serviceMetadata/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<service behaviorConfiguration="WCFLibrary.UpdateUserBehavior" name="WCFLibrary.AddService">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:4506/AddService"/>
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" contract="WCFLibrary.IAddService" bindingConfiguration="netTcpBindConfig"></endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint>
</service>

客户端调用

服务配置好后,启动,客户端使用net.tcp://localhost:4506/AddService/mex引用这个服务以便生成本地代理

代码都是很简单的了

protected void Page_Load(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
AddService.AddServiceClient proxy = new AddService.AddServiceClient();
proxy.UpLoad(System.IO.File.ReadAllBytes("f:/8.rmvb"));
Response.Write(start+" 开 始---"+DateTime.Now+" 结 束");
}

 

 

测试结果

用时8秒种 

 文件信息:  

 

   

 

文件按照151M,传输时间是8秒来计算,大概用时为:151(M)/8(S)=18.875M/S.很不错的速度,不是吗?

 

关于WCF传输大文件,您有什么看法,欢迎交流。

 



     本文转自wengyuli 51CTO博客,原文链接:http://blog.51cto.com/wengyuli/586754,如需转载请自行联系原作者




相关文章
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一