gzip a stream and send it with WCF

392 views Asked by At

I try to send a gzipped stream with WCF.

Here the code server side :

    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();
    }

The TransferServer class :

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);
        }
    }
}

Client side :

    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 with compression had to write into another stream, but I just want to send an already gzipped stream.

Thanks

1

There are 1 answers

0
Max Kilovatiy On

You should create your own stream derived from .net "Stream" and implement Read method

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;
    }