WCF 和流式文件
WCF and streaming files
我有一个使用以下方法流式传输文件的 WCF 服务:
public class GetFileService : IGetFileService
{
public GetFileResponse GetFile(GetFileRequest request)
{
var fileStream = new FileStream(request.FileName, FileMode.Open, FileAccess.Read);
return new GetFileResponse
{
StreamResult = fileStream
};
}
}
[MessageContract]
public class GetFileResponse
{
[MessageBodyMember(Order = 1)]
public Stream StreamResult { get; set; }
}
我的 web.config 有一个 basicHttpBinding with transferMode="Streamed" 调用服务函数的客户端也是如此
当传输模式设置为流时,这意味着服务在发送之前不会在内存中缓冲整个文件,对吗?但是received(调用函数的客户端)呢?
如果我在接收方有如下代码:
GetFileServiceClient getFileService = new GetFileServiceClient();
using ( Stream stream = getFileService.GetFile(@"c:\Temp\pics.zip") )
{
const int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
using (FileStream outputStream = new FileStream(@"C:\Temp\pics2.zip", FileMode.Create, FileAccess.Write))
{
int bytesRead = stream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.Write(buffer, 0, bytesRead);
bytesRead = stream.Read(buffer, 0, bufferSize);
}
outputStream.Close();
}
}
客户端在调用
时是否没有收到整个文件?
getFileService.GetFile(@"c:\Temp\pics.zip")
或者是否接收到流指针并且仅当在 while 循环中执行对 Read 的调用时才流式传输文件?不知何故,我发现这很难消化。我感觉整个文件都是通过网络发送的,位于 WCF 通信层的某个地方。如果是这样,我该如何以正确的方式在客户端使用流,以便客户端的内存不会被可能非常大的文件占用。
谢谢
When the transfermode is set to streamed, it means that the service
does not buffer the entire file in memory before it sends it, right?
正确
But what about the received (the client that calls the function). If
I have code that looks like the following on the receiver side does
the client not receive the whole file in the call to
getFileService.GetFile(@"c:\Temp\pics.zip")
在某种程度上,您的网络基础设施正在做一些数据包缓冲,但在将其提供给客户端(或在客户端)之前,没有任何东西可以缓冲整个流。传输管理通信,并且可以使用 maxBufferSize、maxBufferPoolSize 和 maxReceivedMessageSize 属性在绑定中进行某种程度的控制
我有一个使用以下方法流式传输文件的 WCF 服务:
public class GetFileService : IGetFileService
{
public GetFileResponse GetFile(GetFileRequest request)
{
var fileStream = new FileStream(request.FileName, FileMode.Open, FileAccess.Read);
return new GetFileResponse
{
StreamResult = fileStream
};
}
}
[MessageContract]
public class GetFileResponse
{
[MessageBodyMember(Order = 1)]
public Stream StreamResult { get; set; }
}
我的 web.config 有一个 basicHttpBinding with transferMode="Streamed" 调用服务函数的客户端也是如此
当传输模式设置为流时,这意味着服务在发送之前不会在内存中缓冲整个文件,对吗?但是received(调用函数的客户端)呢? 如果我在接收方有如下代码:
GetFileServiceClient getFileService = new GetFileServiceClient();
using ( Stream stream = getFileService.GetFile(@"c:\Temp\pics.zip") )
{
const int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
using (FileStream outputStream = new FileStream(@"C:\Temp\pics2.zip", FileMode.Create, FileAccess.Write))
{
int bytesRead = stream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.Write(buffer, 0, bytesRead);
bytesRead = stream.Read(buffer, 0, bufferSize);
}
outputStream.Close();
}
}
客户端在调用
时是否没有收到整个文件? getFileService.GetFile(@"c:\Temp\pics.zip")
或者是否接收到流指针并且仅当在 while 循环中执行对 Read 的调用时才流式传输文件?不知何故,我发现这很难消化。我感觉整个文件都是通过网络发送的,位于 WCF 通信层的某个地方。如果是这样,我该如何以正确的方式在客户端使用流,以便客户端的内存不会被可能非常大的文件占用。
谢谢
When the transfermode is set to streamed, it means that the service does not buffer the entire file in memory before it sends it, right?
正确
But what about the received (the client that calls the function). If I have code that looks like the following on the receiver side does the client not receive the whole file in the call to
getFileService.GetFile(@"c:\Temp\pics.zip")
在某种程度上,您的网络基础设施正在做一些数据包缓冲,但在将其提供给客户端(或在客户端)之前,没有任何东西可以缓冲整个流。传输管理通信,并且可以使用 maxBufferSize、maxBufferPoolSize 和 maxReceivedMessageSize 属性在绑定中进行某种程度的控制