WCF stream length

I have the following code to send a stream (file) to a wcf client:

 public Stream Download( string path )
    {

        try
        {
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);

            return stream;

        }
        catch (Exception ex)
        {
            string error = ex.Message;

            return null;
        }
    }

I want to be able to get the length of the sent stream on the client side, but the Stream class does not support this.

What would be the best way to do this?

Thank you, Tony

+3
source share
1 answer
[MessageContract]
public class SizedStreamMessage
{
   [MessageHeader]
   public long streamSize;

   [MessageBody] //Has to be just one MessageBody for streaming to work!
   public Stream theStream;
}

And then:

[OperationContract]
public SizedStreamMessage Download(string path)
{
 //Fill in streamSize...
 //Fill in theStream...
}

Obviously, it will only work for streams that you can actually get on the server side without buffering the entire stream (FileStream should work, because you can always get the file length without actually reading the file).

+4
source

Source: https://habr.com/ru/post/1719312/


All Articles