WCF streaming is helpful in all those cases when you need to move a lot of data between the client and the service in a way that keeps memory consumption under control (see Large Data and Streaming on MSDN for more details if you are not familiar with the topic). Streaming does impose some restrictions. Among them is that your operations can only have one input and/or output parameter and they can only be only one of the following:a Stream
a Message
a class implementing IXmlSerializable
I wanted to expose a very simple service to upload and download files through WCF. And because I wanted to be able to pass both the file content as a stream and at least the file name, I could not just use a Stream but had to resort to using Messages: the headers would convey the file information (such as the file type) and the body the content of the file itself through a stream. I therefore defined the service as follows:private void IssueDownloadRequest(string localFile, string serviceUrl, FileDownloadMessage request)
{
this.service = this.proxy.OpenProxy(serviceUrl); try
{
using (FileDownloadReturnMessage response = this.service.DownloadFile(request))
{
if (response != null && response.FileByteStream != null)
{
SaveFile(response.FileByteStream, localFile);
}
} this.proxy.CloseProxy(this.service);
}
catch (Exception e)
{
throw new FileTransferProxyException("Error while downloading the file", e);
}
finally
{
// we expect the stream returned from the server to be closed by the
// server itself so nothing to be done with it here. Just abort
// the proxy if needed.
this.proxy.AbortProxyIfNeeded(this.service);
}
}private static void SaveFile(Stream saveFile, string localFilePath)
{
const int bufferSize = 65536; // 64KRead more: Stefano Ricciardi Part 1, Part 2, Part 3
a Message
a class implementing IXmlSerializable
I wanted to expose a very simple service to upload and download files through WCF. And because I wanted to be able to pass both the file content as a stream and at least the file name, I could not just use a Stream but had to resort to using Messages: the headers would convey the file information (such as the file type) and the body the content of the file itself through a stream. I therefore defined the service as follows:private void IssueDownloadRequest(string localFile, string serviceUrl, FileDownloadMessage request)
{
this.service = this.proxy.OpenProxy(serviceUrl); try
{
using (FileDownloadReturnMessage response = this.service.DownloadFile(request))
{
if (response != null && response.FileByteStream != null)
{
SaveFile(response.FileByteStream, localFile);
}
} this.proxy.CloseProxy(this.service);
}
catch (Exception e)
{
throw new FileTransferProxyException("Error while downloading the file", e);
}
finally
{
// we expect the stream returned from the server to be closed by the
// server itself so nothing to be done with it here. Just abort
// the proxy if needed.
this.proxy.AbortProxyIfNeeded(this.service);
}
}private static void SaveFile(Stream saveFile, string localFilePath)
{
const int bufferSize = 65536; // 64KRead more: Stefano Ricciardi Part 1, Part 2, Part 3
0 comments:
Post a Comment