NonOwningContent Class
- Namespace
- Kampute.HttpClient.Content
- Assembly
- Kampute.HttpClient.dll
Definition
Represents an HTTP content that sends another content unchanged, without taking ownership of it.
public sealed class NonOwningContent : HttpContentDecorator- Inheritance
- object
- HttpContent
- HttpContentDecorator
- NonOwningContent
Remarks
Constructors
NonOwningContent(HttpContent)
Initializes a new instance of the NonOwningContent class.
public NonOwningContent(HttpContent content)Parameters
contentHttpContent- The HTTP content to send. It is not disposed when this instance is disposed.
Exceptions
- ArgumentNullException
- Thrown when
contentisnull.
Methods
SerializeToStreamAsync(Stream, TransportContext)
Writes the original content to a stream.
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)Parameters
streamStream- The stream to write to.
contextTransportContext- The transport context.
Returns
- Task
- A task that represents the asynchronous operation.
TryComputeLength(out long)
Tries to compute the length of the content.
protected override bool TryComputeLength(out long length)Parameters
lengthlong- When this method returns, contains the length of the original content in bytes, if it is known.
Returns
Extension Methods
| AsDeflate(this HttpContent, CompressionLevel) | Wraps the content in content that compresses it with Deflate as it is sent. |
| AsGzip(this HttpContent, CompressionLevel) | Wraps the content in content that compresses it with GZip as it is sent. |
| FindCharacterEncoding(this HttpContent) | Returns the character encoding named by the charset parameter of the Content-Type header. |
| IsReusable(this HttpContent) | Determines whether the content can be sent more than once, as a retry requires. |

This class passes the headers and body of the original content through unchanged. Disposing an instance of this class does not dispose the original content, so the original content stays usable and its owner remains responsible for disposing it.
Use this class when a component that disposes the content it receives must not end the life of the original content. For example, HttpClient on .NET Framework disposes the content of every request it sends, which prevents the same content from being sent again. Sending a NonOwningContent instead keeps the original content available for another attempt.
The original content can be sent again only if it is reusable. For example, a StreamContent over a stream that cannot be read twice remains non-reusable when wrapped.