服务实现
服务中有一个上传二进制流的方法UpLoad:
[ServiceContract]
public interface IAddService
{
[OperationContract]
void UpLoad( byte [] file);
}
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盘下并命名
}
}
{
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 >
< 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
="32"
maxStringContentLength
="2147483647 "
maxArrayLength
="2147483647 "
maxBytesPerRead
="4096"
maxNameTableCharCount
="16384"
/>
<
behaviors
>
< serviceBehaviors >
< behavior name ="WCFLibrary.UpdateUserBehavior" >
< serviceMetadata />
< serviceDebug includeExceptionDetailInFaults ="false" />
</ behavior >
</ serviceBehaviors >
</ 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 >
< 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 + " 结 束 " );
}
{
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.很不错的速度,不是吗?