HttpContentFormatter Class
- Namespace
- Kampute.HttpClient.Content.Abstracts
- Assembly
- Kampute.HttpClient.dll
Definition
Provides a base class for content formatters that read and write a fixed set of media types.
public abstract class HttpContentFormatter : IHttpContentFormatter- Inheritance
- object
- HttpContentFormatter
- Implements
- Inherited by
Remarks
Constructors
HttpContentFormatter(IEnumerable<string>, IEnumerable<string>)
Initializes a new instance of the HttpContentFormatter class with the media types it reads and writes.
protected HttpContentFormatter(IEnumerable<string> readableMediaTypes, IEnumerable<string> writableMediaTypes)Parameters
readableMediaTypesIEnumerable<string>- The media types this formatter reads, in order of preference; empty for a send-only formatter.
writableMediaTypesIEnumerable<string>- The media types this formatter writes, in order of preference; empty for a receive-only formatter.
Exceptions
- ArgumentNullException
- Thrown if
readableMediaTypesorwritableMediaTypesisnull.
Properties
ReadableMediaTypes
Gets the media types this formatter reads.
public IReadOnlyCollection<string> ReadableMediaTypes { get; }Property Value
- IReadOnlyCollection<string>
- The media types this formatter reads, in order of preference.
WritableMediaTypes
Gets the media types this formatter writes.
public IReadOnlyCollection<string> WritableMediaTypes { get; }Property Value
- IReadOnlyCollection<string>
- The media types this formatter writes, in order of preference.
Methods
CanRead(string, Type)
Determines whether this formatter can read content of the specified media type into the specified model type.
public virtual bool CanRead(string mediaType, Type modelType)Parameters
Returns
- bool
trueif CanReadMediaType(string) acceptsmediaTypeand CanReadType(Type) acceptsmodelType; otherwise,false.
CanReadMediaType(string)
Determines whether this formatter can read content of the specified media type.
protected virtual bool CanReadMediaType(string mediaType)Parameters
mediaTypestring- The media type of the content, without parameters.
Returns
- bool
trueifmediaTypeis one of the ReadableMediaTypes, ignoring case; otherwise,false.
Remarks
CanRead(string, Type) calls this method with a non-null media type. A media type accepted only by an override is read but not advertised in the
Accept header, which lists ReadableMediaTypes.CanReadType(Type)
Determines whether this formatter can read objects of the specified type.
protected virtual bool CanReadType(Type modelType)Parameters
modelTypeType- The type of the object to read.
Returns
CanWrite(string, Type)
Determines whether this formatter can write a payload of the specified type in the specified media type.
public virtual bool CanWrite(string mediaType, Type payloadType)Parameters
mediaTypestring- The media type of the content to create.
payloadTypeType- The type of the payload to write.
Returns
- bool
trueifmediaTypeis one of the WritableMediaTypes and CanWriteType(Type) acceptspayloadType; otherwise,false.
CanWriteType(Type)
Determines whether this formatter can write payloads of the specified type.
protected virtual bool CanWriteType(Type payloadType)Parameters
payloadTypeType- The type of the payload to write.
Returns
CreateContent(object, string)
When overridden in a derived class, creates HTTP content that carries the specified payload in the specified media type.
protected virtual HttpContent CreateContent(object payload, string mediaType)Parameters
Returns
- HttpContent
- The HttpContent that carries
payload.
Exceptions
- NotSupportedException
- Thrown by the base implementation, for a formatter that does not write content.
Remarks
Write(object, string) calls this method after it has validated its arguments and checked them with CanWrite(string, Type).
GetReadableMediaTypes(Type)
Returns the media types that this formatter can read into the specified model type.
public virtual IEnumerable<string> GetReadableMediaTypes(Type modelType)Parameters
modelTypeType- The type of the object to read.
Returns
- IEnumerable<string>
- ReadableMediaTypes if CanReadType(Type) accepts
modelType; otherwise, an empty collection.
GetWritableMediaTypes(Type)
Returns the media types in which this formatter can write a payload of the specified type.
public virtual IEnumerable<string> GetWritableMediaTypes(Type payloadType)Parameters
payloadTypeType- The type of the payload to write.
Returns
- IEnumerable<string>
- WritableMediaTypes if CanWriteType(Type) accepts
payloadType; otherwise, an empty collection.
HasStructuredSyntaxSuffix(string, string)
Determines whether a media type ends with the specified structured syntax suffix.
protected static bool HasStructuredSyntaxSuffix(string mediaType, string suffix)Parameters
mediaTypestring- The media type to check, such as
application/vnd.example+json. suffixstring- The suffix, including its plus sign, such as
+json.
Returns
- bool
trueifmediaTypehas a subtype name beforesuffixand ends with it, ignoring case; otherwise,false.
Exceptions
- ArgumentNullException
- Thrown if
mediaTypeorsuffixisnull.
ReadAsync(HttpContent, Type, CancellationToken)
Asynchronously reads an object of the specified type from HTTP content.
public Task<object> ReadAsync(HttpContent content, Type modelType, CancellationToken cancellationToken = default)Parameters
contentHttpContent- The HttpContent to read.
modelTypeType- The type of the object to read.
cancellationTokenCancellationToken optional- A token for canceling the operation (optional).
Returns
Exceptions
- ArgumentNullException
- Thrown if
contentormodelTypeisnull.
ReadContentAsync(HttpContent, Type, CancellationToken)
When overridden in a derived class, asynchronously reads an object of the specified type from HTTP content.
protected virtual Task<object> ReadContentAsync(HttpContent content, Type modelType, CancellationToken cancellationToken)Parameters
contentHttpContent- The HttpContent to read.
modelTypeType- The type of the object to read.
cancellationTokenCancellationToken- A token for canceling the operation.
Returns
Exceptions
- NotSupportedException
- Thrown by the base implementation, for a formatter that does not read content.
Remarks
ReadAsync(HttpContent, Type, CancellationToken) calls this method after it has validated its arguments.
Write(object, string)
Creates HTTP content that carries the specified payload in the specified media type.
public HttpContent Write(object payload, string mediaType)Parameters
Returns
- HttpContent
- The HttpContent that carries
payload.
Exceptions
- ArgumentNullException
- Thrown if
payloadormediaTypeisnull. - NotSupportedException
- Thrown if CanWrite(string, Type) returns
falseformediaTypeand the type ofpayload.

A derived class passes the media types it reads and the media types it writes to the constructor. A receive-only formatter passes no writable media types and overrides ReadContentAsync(HttpContent, Type, CancellationToken); a send-only formatter passes no readable media types and overrides CreateContent(object, string); a two-way formatter does both. To limit the types a formatter handles, override CanReadType(Type) or CanWriteType(Type). To read media types that are not listed, such as every media type with a structured syntax suffix, override CanReadMediaType(string).
Media types are compared ignoring case. ReadAsync(HttpContent, Type, CancellationToken) and Write(object, string) validate their arguments before they call the overridable members.