views:

521

answers:

1

I'm trying to pull a large amount of data out of a WCF web service. The request is fairly small and the response message will be very big. Currently the web service is throwing SystemOutOfMemory exceptions due a limitation on IIS6 for the memory it can allocated (~1.4GB).

I have read in some blogs that implementing streaming will solve my problem.

Can anybody share their experiences with this topic? I'm most interested in any sample client-side & service-side code that can be shared or any recommendations/best practices. MemoryStream vs FileStream? Return type should be Stream, Message, Byte[]?

My operation looks like the following: (typically it will return a big number of elements in the response array, ~200K elements)

MediumSizeResponseClass[] GetData(SmallSizeRequestClass request)
+2  A: 

If you want to stream back only the response, then use transferMode=streamedResponse in your binding configuration. This makes sure only the returned response will be streamed.

The return value of a streaming function must be a Stream. You can then read from that stream and do whatever you need to do with it (store it, analyse it, whatever).

So basically you'd have a service contract something like this:

[ServiceContract]
interface IYourService
{
    [OperationContract]
    Stream GetData(SmallSizeRequestClass request);
}

On the server, you basically just write to a stream, while on the client, you read from the stream.

Have you consulted the MSDN docs on WCF Streaming?

marc_s
Also see http://msdn.microsoft.com/en-us/library/ms789010.aspx for examples which can be found at the bottom of the page that marc linked to above.
Xiaofu
Thansk Mark, I'm somehow familiar with the basics of implementing streaming. I know there is different ways of implenting it, some developers will use a Message as return type as opposed to a Stream class. Some will use a FileStream while others a MemoryStream. I'm trying to collect feedback, cons an pros on the different implementations for Streaming in WCF.
Damian
@Damian: ok, excellent. Well, I haven't had much first-hand experience with streaming in WCF myself, but I would say: use whatever stream makes sense. It's gotta be a stream, a stream, a stream - but which type is largely irrelevant. If you already have files on the server - use FileStream. If you need to load a blob from a database table - use a MemoryStream. I'm not aware of any major issues with using one type of stream vs. another. But maybe someone else can enlighten us some more!
marc_s
Isn't the problem with the MemoryStream approach going to be that you'd end up loading all the contents before returning the stream? If you're returning large files you'd end up with a bottleneck wouldn't you?
spooner