【问题标题】:gzip a stream and send it with WCFgzip 流并使用 WCF 发送
【发布时间】:2014-11-13 15:23:42
【问题描述】:

我尝试使用 WCF 发送压缩流。

这里是代码服务器端:

    static void Main(string[] args)
    {
        var baseAddress = new Uri("http://localhost:2016/TransferServer");
        var host = new ServiceHost(typeof(TransferServer), baseAddress);
        var binding = new BasicHttpBinding
        {
            TransferMode = TransferMode.Streamed,
            MaxReceivedMessageSize = long.MaxValue,
            MaxBufferSize = 65535,
        };
        host.AddServiceEndpoint(typeof(ITransferServer), binding, baseAddress);
        var smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        host.Description.Behaviors.Add(smb);
        host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;

        host.Open();

        Console.Read();
    }

TransferServer 类:

public class TransferServer : ITransferServer
{
    public void Transfer(Stream stream)
    {
        using (var gz = new GZipStream(stream, CompressionMode.Decompress))
        using (var fs = new FileStream("test.bin", FileMode.Create))
        {
            gz.CopyTo(fs);
        }
    }
}

客户端:

    public void SendStream(Stream stream)
    {
        var client = new TransferServerClient(
            new BasicHttpBinding {MaxReceivedMessageSize = long.MaxValue, TransferMode = TransferMode.Streamed},
            new EndpointAddress(@"http://localhost:2016/TransferServer"));
        client.Open();

        client.TransferDump( ??? gzipped stream ???);
    }

具有压缩的 GzipStream 必须写入另一个流,但我只想发送一个已经 gzip 压缩的流。

谢谢

【问题讨论】:

    标签: c# wcf gzipstream


    【解决方案1】:

    您应该创建自己的从 .net "Stream" 派生的流并实现 Read 方法

    public override int Read(byte[] buffer, int offset, int count)
        {
            while (count > 0)
            {
                //Here you should utilize GZipStream
                Array.Copy(gzipBuffer, position, buffer, offset, count);
            }
    
            return processedBytes;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多