我有一个WCF服务,它使用Stream
类上传文档。
此后,我想获取文档的大小(流的长度),以更新FileSize的fileAttribute。
但是这样做,WCF抛出一个异常说
Document Upload Exception: System.NotSupportedException: Specified method is not supported.
at System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.get_Length()
at eDMRMService.DocumentHandling.UploadDocument(UploadDocumentRequest request)
任何人都可以帮我解决这个问题。
答案 0 :(得分:6)
此后,我想获取文档的大小(流的长度),以更新FileSize的fileAttribute。
不,不要那样做。如果您正在编写文件,那么只需编写文件即可。最简单的是:
using(var file = File.Create(path)) {
source.CopyTo(file);
}
或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);
}
}
(不需要事先知道长度)
请注意,某些WCF选项(完整邮件安全性等)要求在处理之前验证整个邮件,因此永远不能真正流,因此:如果规模很大,我建议您改用客户端将其拆分并将其分段发送的API(然后在服务器上重新组装)。
答案 1 :(得分:0)
如果流不支持搜索,则无法使用Stream.Length
另一种方法是将流复制到字节数组并查找其累积长度。这涉及首先处理整个流,如果你不想这样做,你应该向你的WCF服务接口添加一个流长度参数