如何在使用 WCF 传输大文件时显示反馈
我正在通过 WCF 发送大文件,并使用 TransferMode="Streamed" 来使其正常工作,并且工作正常。
问题是有时这些文件太大了,我想向客户提供一些有关进度的反馈。
有人有关于如何完成这个任务的完美解决方案/想法吗?
编辑:我不命令在任何一方(客户端或服务器)读取文件,如果我这样做,我可以只提供有关流的读取功能的反馈。
EDIT2:我的代码的一部分,帮助其他人理解我的问题
这是我的合同
[OperationContract]
FileTransfer Update(FileTransfer request);
,这是 FileTransfer 的定义,
[System.ServiceModel.MessageContractAttribute(WrapperName = "FileTransfer", WrapperNamespace = "http://tempuri.org/", IsWrapped = true)]
public class FileTransfer : IDisposable
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "dummy", Order = 0)]
public Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
所以,在我的服务(托管在 IIS 上)我只有这样的东西:
request.FileByteStream;
WCF 本身读取流,对吧?
我希望这可以帮助人们理解我的问题...如果您需要更多信息,请告诉我
I'm sending large files over WCF and I'm using transferMode="Streamed" in order to get this working, and it is working fine.
The thing is sometimes those files are just too big, and I want to give the client some sort of feedback about the progress.
Does anybody have a godd solution/idea on how to acomplish this?
EDIT: I don't command the read of the file in either side (client or server) if I did I could just give feedback on the read function of the stream.
EDIT2: part of my code to help others understand my problem
Here's my contract
[OperationContract]
FileTransfer Update(FileTransfer request);
and here's the definition of FileTransfer
[System.ServiceModel.MessageContractAttribute(WrapperName = "FileTransfer", WrapperNamespace = "http://tempuri.org/", IsWrapped = true)]
public class FileTransfer : IDisposable
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "dummy", Order = 0)]
public Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
so, in my service (hosted on IIS) I just have something like this:
request.FileByteStream;
and WCF itself reads the stream, right?
I hope this helps people out to understand my problem... please let me know if you need further info
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将总流大小添加为自定义 Soap 标头(使用 MessageContracts)怎么样?然后,您可以在客户端上以块的形式处理流(例如在循环中读取到定义大小的缓冲区),并且对于每个块,您可以通知客户端有关在预期大小的上下文中处理的增量。
What about adding total stream size as custom Soap header (use MessageContracts). Then you can process the stream on client in chunks (like reading to buffer of defined size in loop) and for each chunk you can notify client about processed increment in context of expected size.
我现在看到的唯一方法是创建另一个操作来报告流操作读取的字节数。这需要在服务器端激活会话和多线程,并实现来自客户端的异步调用以及对“进度报告”操作的调用。
客户端知道流的大小(假设客户端是发送者),它可以从已知的总大小和服务器报告的大小中提取进度百分比。
编辑:
我的评论是在客户端正在上传数据的假设下起作用的。因此,服务器知道它已经从流中读取的大量数据,而客户端知道数据的大小。
如果服务器公开一个报告迄今为止已读取的数据量的操作,则客户端将能够通过调用此操作来计算进度百分比。
The only way I see right now is by creating another operation that report the number of bytes read by the streamed operation. This would require activating sessions and multi-threading at the server side and implementing a asynchronous call from the client together with calls to the "progress reporting" operation.
The client knows the size of the stream (assuming the client is the sender), it can extract a progress percentage from the known total size and the reported size from the server.
EDIT:
My comment works under the assumption that the client is uploading data. So the server knows of much data it has already read from the stream while the client knows the size of the data.
If the server exposes an operation that reports the volume of data it has read so far, the client will be able to calculate the progress percentage by calling this operation.