您的位置:知识库 » .NET技术

WCF使用NetTcp传输文件

作者: Leon Weng  来源: 博客园  发布时间: 2010-09-17 07:56  阅读: 12918 次  推荐: 3   原文链接   [收藏]  
摘要:今天看了一些官方的资料和配置,简单写了一个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上。

image

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

  maxBufferPoolSize="2147483647 "

  maxBufferSize="2147483647 "  

   maxReceivedMessageSize="2147483647 "

  当然,还有配额的大小:

<readerQuotas maxDepth="32" maxStringContentLength="2147483647 " maxArrayLength="2147483647 " maxBytesPerRead="4096" maxNameTableCharCount="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秒种:

image

  文件信息:

 

image image

image

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

3
0
标签:WCF

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻