I have a WCF service that streams files with the following method:
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; }
}
My web.config has a basicHttpBinding with transferMode="Streamed" and so does the client that calls the service function
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:
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();
}
}
does the client not receive the whole file in the call to
getFileService.GetFile(@"c:\Temp\pics.zip")
or does receive a stream pointer and the file is streamed only when calls to Read are performed in the while loop? Somehow I find that difficult to digest. I have the feeling that the whole file is sent through the network, and is lies somewhere in the WCF communication layer. If that is true, how do I go about using the stream in the client side in the correct way so that the client's memory is not occupied by what could be extremely large files.
Thanks
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?
Correct
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")
At some level, your network infrastructure is doing some amount of packet buffering, but there's nothing that buffers the entire stream before giving it to the client (or on the client side). The transport is managing the communication, and can be somewhat controlled in the binding using the maxBufferSize, maxBufferPoolSize, and maxReceivedMessageSize properties