gzip流并使用WCF发送它

时间:2014-11-13 15:23:43

标签: c# wcf gzipstream

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

这里是代码服务器端:

    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必须写入另一个流,但我只想发送已经压缩的流。

由于

1 个答案:

答案 0 :(得分:0)

您应该创建自己的源自.net&#34; Stream&#34;并实现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;
    }