StreamPartialContent Class
Represents a content type that is uploaded as a stream and can be partially sent. This class is useful for scenarios where you need to send large amounts of data without loading the entire content into memory.
public sealed class StreamPartialContent : HttpContent
Syntax
Declaration public sealed class StreamPartialContent : HttpContent
Constructors
-
StreamPartialContent
(Stream content, long? maxBufferSize = null)
Initializes a new instance of the
StreamPartialContentclass.Parameters:
content: The stream that contains the HTTP content.maxBufferSize: An optional parameter specifying the maximum buffer size for reading from the stream.
-
StreamPartialContent
(Stream content, long? maxBufferSize, string mediaType)
Initializes a new instance of the
StreamPartialContentclass with a specified media type.Parameters:
content: The stream that contains the HTTP content.maxBufferSize: An optional parameter specifying the maximum buffer size for reading from the stream.mediaType: The MIME type of the content.
Properties
-
Headers
: HttpContentHeaders
Gets the HTTP headers for the content.
Methods
-
Dispose
(bool disposing)
Releases the unmanaged resources used by the
StreamPartialContentand optionally releases the managed resources. -
SerializeToStreamAsync
(Stream stream, CancellationToken cancellationToken)
Serializes the HTTP content to a stream asynchronously.
Parameters:
stream: The stream to write the content to.cancellationToken: A cancellation token that can be used to cancel the operation.
Returns:
A task that represents the asynchronous serialization operation. -
ComputeContentLengthAsync
(CancellationToken cancellationToken)
Computes the length of the content in bytes asynchronously.
Parameters:
cancellationToken: A cancellation token that can be used to cancel the operation.
Returns:
A task that represents the asynchronous operation. The value of the task includes the length of the content in bytes.
Remarks
The StreamPartialContent class is particularly useful when dealing with data sources that cannot be fully loaded into memory, such as large files or network streams.
It allows for efficient transmission of data by reading and writing in chunks.
When creating an instance, you provide a Stream object. The maxBufferSize parameter controls how much data is read from the stream at a time, which can be important for performance tuning.
The Headers property allows you to set standard HTTP headers like 'Content-Type' and 'Content-Disposition'.
The SerializeToStreamAsync method is the core method responsible for writing the stream's content to the destination stream.
The ComputeContentLengthAsync method might return -1 if the content length cannot be determined beforehand, which is common for streaming content.