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上。

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.很不错的速度,不是吗?

 

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

posted @   翁玉礼  阅读(9924)  评论(16编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示