一起谈.NET技术,WCF使用NetTcp传输文件

  服务实现

  服务中有一个上传二进制流的方法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.很不错的速度,不是吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值