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.