-1

I have WCF service, which I specify to use via streames: This is Web.config

<services>
  <service name="StreamServiceBL">
    <endpoint address="" binding="basicHttpBinding"
      bindingConfiguration="StreamServiceBLConfiguration" contract="IStreamServiceBL" />
  </service>
</services>
<bindings>
   <basicHttpBinding>
    <binding name="StreamServiceBLConfiguration" transferMode="Streamed"/>
  </basicHttpBinding>
</bindings>

This is way I send my streames:

    private static MemoryStream SerializeToStream(object o)
    {
        var stream = new MemoryStream();
        IFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, o);
        return stream;
    }

    private void Somewhere()
    {
        //...
        streamServiceBLClientSample.MyFunc(SerializeToStream(myObject));
    }

And this is way I receive them:

[ServiceContract]
public interface IStreamServiceBL
{
    [OperationContract]
    public int MyFunc(Stream streamInput);
}

public class StreamServiceBL : IStreamServiceBL
{
    public int MyFunc(Stream streamInput)
    {
        //There I get exception: It can't to deserialize empty stream
        var input = DeserializeFromStream<MyType>(streamInput);

    }

    public static T DeserializeFromStream<T>(Stream stream)
    {
        using (var memoryStream = new MemoryStream())
        {
            CopyStream(stream, memoryStream);

            IFormatter formatter = new BinaryFormatter();
            memoryStream.Seek(0, SeekOrigin.Begin);
            object o = formatter.Deserialize(memoryStream); //Exception - empty stream
            return (T)o;
        }
    }

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[16 * 1024];
        int read;
        //There is 0 iteration of loop - input is empty
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, read);
        }
    }
}

So I spend not an empty stream, but I get an empty one. I get CopyStream code from there: How to get a MemoryStream from a Stream in .NET? and I think there is no mistakes in it, so as I understand I got empty stream.

Community
  • 1
  • 1

1 Answers1

2

It is hard to say why you get an empty stream in a general case, but in your sample case it is perfectly clear.

If you update the method SerializeToStream as follows everything should work as expected:

private static MemoryStream SerializeToStream(object o)
{
    var stream = new MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, o);
    // here we reset stream position and it can be read from the very beginning
    stream.Position = 0;  
    return stream;
}
ie.
  • 5,982
  • 1
  • 29
  • 44
  • No, it don't work. I add some new code. I hope it helps. – Rustam Salakhutdinov Mar 29 '15 at 22:04
  • @RustamSalahutdinov, have you really updated `SerializeToStream` (on the client side, not the server) as I suggested? I just tested and without my update I'm also getting an empty steam on the server, with the update I'm getting the expected data. – ie. Mar 29 '15 at 22:22
  • First, I insert it before serialize and don't see it. After you remind me - I replace it to right position and it works! Thank you very much! – Rustam Salakhutdinov Mar 29 '15 at 22:29