在 WCF 中传递文件流

本文关键字:文件 WCF | 更新日期: 2023-09-27 18:33:44

在我将文件流的引用从客户端传递到服务,并且服务开始将流下载给他之后,我如何从客户端确定到目前为止读取了多少字节(而我使用文件流对象)?

的目标是仅针对此文件计算客户端的上传速度,我能想到的唯一方法是这个。

在 WCF 中传递文件流

扩展 FileStream 或为其创建包装器。重写读取方法,并让计数器对读取的字节进行计数。

扩展(未正确实现,但应该足以解释)

   public class CountingStream : System.IO.FileStream {
      // provide appropriate constructors
      // may want to override BeginRead too
      // not thread safe
      private long _Counter = 0;
      public override int ReadByte() {
         _Counter++;
         return base.ReadByte();            
      }
      public override int Read(byte[] array, int offset, int count) {
         // check if not going over the end of the stream
         _Counter += count;
         return base.Read(array, offset, count);             
      }
      public long BytesReadSoFar {
         get {
            return _Counter;
         }
      }
   }