查找Stream对象的WCF客户端长度? [英] Find Length of Stream object in WCF Client?

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

问题描述

我有一个WCF服务,它利用上传文档类。

现在在这之后,我想获得的文件的大小(流长度),更新fileAttribute的文件大小。

但这样做,WCF抛出一个异常说

 文件上传例外:不支持指定的方法是:System.NotSupportedException。
   在System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.get_Length()
   在eDMRMService.DocumentHandling.UploadDocument(UploadDocumentRequest要求)
 

谁能帮我解决这个。

解决方案
  

现在在这之后,我想获得的文件的大小(流长度),更新fileAttribute的文件大小。

没有,不这样做。如果你正在写一个文件,然后就的写入文件的。在最简单的:

 使用(var文件= File.Create(路径)){
    source.CopyTo(文件);
}
 

或4.0之前:

 使用(var文件= File.Create(路径)){
    byte []的缓冲区=新的字节[8192];
    INT读取;
    而((读取= source.Read(缓冲液,0,buffer.Length))大于0){
        file.Write(缓冲,0,读);
    }
}
 

(其不需要事先知道的长度)

请注意,某些WCF选项(整个邮件安全等)所需要的整个信息处理之前进行验证,这样可以永远的真正的流,因此:如果大小是巨大的,我建议你改用有一个API,客户端将其分解,并将其发送的部分(你再重新组装的服务器)。

I have a WCF Service, which uploads the document using Stream class.

Now after this, i want to get the Size of the document(Length of Stream), to update the fileAttribute for FileSize.

But doing this, the WCF throws an exception saying

Document Upload Exception: System.NotSupportedException: Specified method is not supported.
   at System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.get_Length()
   at eDMRMService.DocumentHandling.UploadDocument(UploadDocumentRequest request)

Can anyone help me in solving this.

解决方案

Now after this, i want to get the Size of the document(Length of Stream), to update the fileAttribute for FileSize.

No, don't do that. If you are writing a file, then just write the file. At the simplest:

using(var file = File.Create(path)) {
    source.CopyTo(file);
}

or before 4.0:

using(var file = File.Create(path)) {
    byte[] buffer = new byte[8192];
    int read;
    while((read = source.Read(buffer, 0, buffer.Length)) > 0) {
        file.Write(buffer, 0, read);
    }
}

(which does not need to know the length in advance)

Note that some WCF options (full message security etc) require the entire message to be validated before processing, so can never truly stream, so: if the size is huge, I suggest you instead use an API where the client splits it and sends it in pieces (which you then reassemble at the server).

这篇关于查找Stream对象的WCF客户端长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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