I need to return a big byte array from a WCF server.
The problem is – in order to return such an array – I need to create one – and when creating such an array – it automatically goes to the Large Object Heap – meaning that when the service is stressed – I’m getting a real problem in memory usage and management.
I thought about using unmanaged memory to avoid using large managed byte arrays – but still – how can I return such an array from a WCF service?
Is there any way of returning a “Stream” of bytes from a WCF service that does not include actually creating a managed byte array? I know that WCF itself uses a BufferManager – so if it just reads my unmanaged memory and uses its buffer management to store it before sending – I hopefully won’t have a problem.
You can use WCF’s Streaming Mode. From that page:
There are some decent examples on that page as well, including how to write back a custom stream (it’s toward the bottom).
For more background info on encoding, streaming and sessions, and some security considerations when using streaming, see this MSDN page.