我有一个客户端 - 服务器WCF上传器/下载器。
以无限循环下载/上传文件(获取文件列表并上传/下载文件,直到有人停止)。
客户端的创建方式如下:
ChannelFactory<IContract> scf = new ChannelFactory<IContract>(
this._endPoint.Binding, this._endPoint.AddressFull);
this.Channel = scf.CreateChannel();
this.channelFactory = scf;
并且它的绑定是TCP客户端/服务器,并且是这样创建的:
public static CustomBinding GetCustomTcpBinding(TransferMode mode, long MaxBufferBufferPoolSize, long MaxReceivedMessageSize, int MaxBufferSize, TimeoutHelper Timeouts, StreamerType st)
{
MtomMessageEncodingBindingElement binaryEncoding = new MtomMessageEncodingBindingElement();
System.Xml.XmlDictionaryReaderQuotas rq = binaryEncoding.ReaderQuotas;
SetReaderQuotas(ref rq, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue);
TcpTransportBindingElement tcpTransport = new TcpTransportBindingElement();
tcpTransport.MaxReceivedMessageSize = 2147483647;// MaxReceivedMessageSize;
tcpTransport.MaxBufferSize = 2147483647;// MaxBufferSize;
tcpTransport.MaxBufferPoolSize = 2147483647;// MaxBufferBufferPoolSize;
tcpTransport.TransferMode = mode;
switch(st)
{
case StreamerType.Client:
{
//tcpTransport.ConnectionBufferSize = Consts.TCP_CLIENT_CONNECTION_BUFFER_SIZE;
break;
}
case StreamerType.Server:
{
tcpTransport.ConnectionBufferSize = Consts.TCP_SERVER_CONNECTION_BUFFER_SIZE;
tcpTransport.MaxOutputDelay = TimeSpan.FromSeconds(30);
break;
}
}
BindingElementCollection elements = new BindingElementCollection();
elements.Add(binaryEncoding);
elements.Add(tcpTransport);
CustomBinding binding = new CustomBinding(elements)
{
Name = "TcpUserNameBinding"
};
binding.SetTimeouts(Timeouts);
//binding.SetDefaultQuotas();
return binding;
}
和
long MaxBufferBufferPoolSize = 65536 * 4;
long MaxReceivedMessageSize = 2147483647;
int MaxBufferSize = 65536;
服务器是:
long MaxBufferBufferPoolSize = 65536 * 4;
long MaxReceivedMessageSize = 2147483647;
int MaxBufferSize = 65536;
下载/上传以块的形式流式传输,如果超过每个块大小,则在每次读取检查时流式传输,并允许获取更多或结束。
每个块大约2MB,然后进行下一次调用。
客户端的来电方式如下:
public void Call(Action<IContract> action)
{
var channel = this.Channel;
if (channel == null)
{
return;
}
action(channel);
return;
}
问题是有些调用速度较慢,有些调用较快,而我无法预测导致问题的原因。
要下载我使用:
using (fs = new Streamer.Common.StreamWithProgress(fsTemp = new FileStream(System.IO.Path.Combine(folder, fileName), System.IO.FileMode.OpenOrCreate)))
{
fs.Position = position;
var res = ClientConn.SafeCall(a => a.UploadFile(new Streamer.Common.RemoteFileInfo() { FileName = fileName, Length = length, Position = position, FileByteStream = fs }), true, position == 0);
position = res.Position;
hasNextChunk = res.HasNextChunk;
}