代码之家  ›  专栏  ›  技术社区  ›  Tony The Lion

WCF流长度

  •  3
  • Tony The Lion  · 技术社区  · 16 年前

    我有以下代码将(文件)流发送到WCF客户端:

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

    我希望能够在客户端获取发送流的长度,但流类不支持这种情况。

    最好的方法是什么?

    谢谢, 托尼

    1 回复  |  直到 16 年前
        1
  •  4
  •   Eugene Osovetsky    16 年前
    [MessageContract]
    public class SizedStreamMessage
    {
       [MessageHeader]
       public long streamSize;
    
       [MessageBody] //Has to be just one MessageBody for streaming to work!
       public Stream theStream;
    }
    

    然后:

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

    显然,它只适用于在不缓冲整个流的情况下可以在服务器端实际获取其大小的流(file stream应该有效,因为您总是可以在不实际读取文件的情况下获取文件的长度)。

    推荐文章