如何加快缓冲的WCF传输

本文关键字:WCF 传输 缓冲 何加快 | 更新日期: 2023-09-27 18:04:43

我在WCF中使用Buffered transferMode方案将压缩文件从服务器传输到客户端。传输发生在回调中,3.7兆字节的文件只需要2分钟多一点就可以传输。因为我有很多其他的功能使用这个缓冲模式工作,我宁愿不改变方案。

作为参考,这里是我使用的绑定

<bindings>
  <netTcpBinding>
    <binding name="NetTcpBinding_MyIIService"
      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="4967294"
      maxBufferSize="4967294"
      maxConnections="10"
      maxReceivedMessageSize="104857600">
      <readerQuotas maxDepth="104857600"
             maxStringContentLength="104857600"
             maxArrayLength="104857600"
             maxBytesPerRead="104857600"
             maxNameTableCharCount="104857600"/>
      <reliableSession ordered="true"
             inactivityTimeout="20:00:10"
             enabled="true"/>
      <security mode="None">
        <transport clientCredentialType="Windows"
             protectionLevel="EncryptAndSign"/>
        <message clientCredentialType="Windows"/>
      </security>
    </binding>
  </netTcpBinding>
</bindings> 

我已经尝试了很多不同的大小变化,似乎没有什么关系,除非他们设置为小。

在服务器上的回调中,我使用以下代码片段将二进制(zip)文件发送到客户机。这里我使用Base64编码将zip文件转换为ASCII。(顺便说一下,将zip文件传输为二进制文件需要相同的时间,但到达时已损坏)

byte[] binaryData;
System.IO.FileStream inFile;
inFile = new System.IO.FileStream(path2ZipFile,
             System.IO.FileMode.Open,
             System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
            long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
inFile.Close();
string convertedToBase64 = System.Convert.ToBase64String(binaryData, 0,
                           binaryData.Length);
handler(guest, zipFilename, convertedToBase64);

在我的Google Drive共享中删除一个文件大约需要15秒。怎么了?我怎样才能提高我的转帐率?

如何加快缓冲的WCF传输

如果您已经尝试了其他所有方法,您可能会尝试压缩。简而言之,在主机端压缩数据需要1/2秒,在客户端解压缩数据需要1/2秒,但是您将通过网络发送的总体有效负载减少了大约1/3。

您可以在使用System.Convert将byte()数组更改为字符串之前压缩它。ToBase64String或者你可以在发送前压缩base64字符串。这里有一个c#的压缩/解压缩类:

using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace CompressString
{
internal static class StringCompressor
{
    /// <summary>
    /// Compresses the string.
    /// </summary>
    /// <param name="text">The text.</param>
    /// <returns></returns>
    public static string CompressString(string text)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(text);
        var memoryStream = new MemoryStream();
        using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
        {
            gZipStream.Write(buffer, 0, buffer.Length);
        }
        memoryStream.Position = 0;
        var compressedData = new byte[memoryStream.Length];
        memoryStream.Read(compressedData, 0, compressedData.Length);
        var gZipBuffer = new byte[compressedData.Length + 4];
        Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
        Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
        return Convert.ToBase64String(gZipBuffer);
    }
    /// <summary>
    /// Decompresses the string.
    /// </summary>
    /// <param name="compressedText">The compressed text.</param>
    /// <returns></returns>
    public static string DecompressString(string compressedText)
    {
        byte[] gZipBuffer = Convert.FromBase64String(compressedText);
        using (var memoryStream = new MemoryStream())
        {
            int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
            memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
            var buffer = new byte[dataLength];
            memoryStream.Position = 0;
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
            {
                gZipStream.Read(buffer, 0, buffer.Length);
            }
            return Encoding.UTF8.GetString(buffer);
        }
    }
}
}
祝你好运。我发现这个压缩工具快速且易于使用。当然,这段代码只有在两端都使用。net时才有效。如果你有一个iOS或Android客户端,你需要使用一个与操作系统无关的压缩工具。