C# - 如何读取 WCF 服务中的流
我有一个 WCF 服务,我想发送日志文件并在服务器上处理它。 合同是:
[OperationContract]
void LogFile(Stream file);
我在端点中使用 StreamedRequest。
我遇到的问题是我找不到读取服务中流的方法。
当我调试调用时,我看到 Stream 是以下实例:
System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream
我从客户端发送 MemoryStream。
那么...我怎样才能读取流呢?
谢谢。
编辑1: 我使用:
Stream serviceStream = new MemoryStream();
byte[] buffer = new byte[10000];
int bytesRead = 0;
do
{
bytesRead = file.Read(buffer, 0, buffer.Length);
serviceStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
serviceStream.Position = 0;
读取流,没有任何内容,始终为 0
I have a WCF service where I would like to send a log file and process it on the server.
The contract is:
[OperationContract]
void LogFile(Stream file);
And Im using StreamedRequest in the endpoint.
The problem I have is that I cant find a way to read the stream in the service.
When I debug the call, I see that the Stream is an instance of:
System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream
From the client Im sending a MemoryStream.
So... How can I read the stream?
Thanks.
Edit1:
im using:
Stream serviceStream = new MemoryStream();
byte[] buffer = new byte[10000];
int bytesRead = 0;
do
{
bytesRead = file.Read(buffer, 0, buffer.Length);
serviceStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
serviceStream.Position = 0;
to read the stream, nothing gets out, always 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不好的是,在客户端中我忘记将流的位置设置为 0,因此服务正在获取流的末尾位置
My bad, in the client I forgot to set the position of the stream to 0, so the service was getting the stream with the position at the end of it
不必担心提供给您的流的内部类型。只需像平常一样读取流(例如使用 StreamReader) 一切都应该没问题。请注意,您不需要在任一侧的流上调用
Dispose
或Close
,WCF 将处理该问题。Don't worry about the internal type of the stream given to you. Just read the stream as you normally would (e.g. with StreamReader) and everything should be fine. Note that you do not need to call
Dispose
orClose
on the stream on either side, WCF will handle that.