WCF流长 [英] WCF Stream length

查看:111
本文介绍了WCF流长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共流下载(字符串路径)
{


{
的FileStream流=新的FileStream(路径,FileMode.Open,FileAccess.Read);

返回流;

}
赶上(异常前)
{
字符串错误= ex.Message;

返回NULL;
}
}



我希望能够得到的长度在客户端发送数据流,但流类不支持这一点。



什么是做的最好的方法是什么?



谢谢,
托尼


解决方案

  [MessageContract] 
公共类SizedStreamMessage
{
[的MessageHeader]
众长streamsize可;

[消息体] //必须是流媒体的工作只是一个消息体!
公共流theStream;
}

和则:

  [OperationContract的] 
公共SizedStreamMessage下载(字符串路径)
{
//在streamsize可填写...
//填写theStream ...
}

显然,它只会为你实际上可以得到工作流规模而言,在服务器端没有缓冲整个流(的FileStream应该工作,因为你总是可以得到该文件的长度,而不实际读取文件)。


I have the following code to send a (file) stream 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 at the client end, but the Stream class does not support this.

What would be the best way of doing it?

Thanks, Tony

解决方案

[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 the size for on the server-side without buffering the entire stream (FileStream should work 'cause you can always get the length of the file without actually reading the file).

这篇关于WCF流长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆