Pregunta

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.

¿Fue útil?

Solución

You can use WCF's Streaming Mode. From that page:

  1. To stream data, the OperationContract for the service must satisfy two requirements:

    a. The parameter that holds the data to be streamed must be the only parameter in the method. For example, if the input message is the one to be streamed, the operation must have exactly one input parameter. Similarly, if the output message is to be streamed, the operation must have either exactly one output parameter or a return value.

    b. At least one of the types of the parameter and return value must be either Stream, Message, or IXmlSerializable.

  2. Streaming must be enabled on the binding. You set a TransferMode property, which can take one of the following values:

    a. Buffered,

    b. Streamed, which enables streaming communication in both directions.

    c. StreamedRequest, which enables streaming the request only.

    d. StreamedResponse, which enables streaming the response only.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top