HttpContentFormatterCollection Class

Namespace
Kampute.HttpClient
Assembly
  • Kampute.HttpClient.dll

Definition

Represents the content formatters of an HttpRestClient, in the order they were added.
public sealed class HttpContentFormatterCollection : ICollection<IHttpContentFormatter>, IReadOnlyCollection<IHttpContentFormatter>
Inheritance
  • object
  • HttpContentFormatterCollection
Implements

Remarks

This collection holds the content formatters of an HttpRestClient. It selects the formatter that reads a response with GetReaderFor(string, Type), the formatter that writes a request payload with GetWriterFor(string, Type), and the media types of the Accept header with GetAcceptableMediaTypes(Type). When several formatters match, the one added first is used.

Media types that differ only in case are treated as the same media type. A formatter that is found is cached for later lookups of the same media type and type; a failed lookup is not cached.

Constructors

HttpContentFormatterCollection()

Initializes a new instance of the HttpContentFormatterCollection class.
public HttpContentFormatterCollection()

Properties

Count

Gets the number of formatters in the collection.
public int Count { get; }

Property Value

int
The number of formatters in the collection.

Methods

Add(IHttpContentFormatter)

Adds a formatter to the collection, which can hold one formatter of each type.
public void Add(IHttpContentFormatter formatter)

Parameters

formatter IHttpContentFormatter
The IHttpContentFormatter to add.

Exceptions

ArgumentNullException
Thrown if formatter is null.
ArgumentException
Thrown if an instance of the same type already exists in the collection.

Clear()

Removes all items from the collection.
public void Clear()

Contains(IHttpContentFormatter)

Determines whether the collection contains a specific IHttpContentFormatter.
public bool Contains(IHttpContentFormatter formatter)

Parameters

formatter IHttpContentFormatter
The IHttpContentFormatter to locate in the collection.

Returns

bool
true if formatter is found in the collection; otherwise, false.

Find<T>()

Finds the formatter of the specified type.
public T Find<T>()
	where T : IHttpContentFormatter

Type Parameters

T
The type of the formatter to find.

Returns

T
The formatter whose type is exactly T, or null if the collection has none.

Remarks

A formatter of a type derived from T does not match.

FindOrDefault<T>()

Finds the formatter of the specified type, or creates one with default options if the collection has none.
public T FindOrDefault<T>()
	where T : IHttpContentFormatter, new()

Type Parameters

T
The type of the formatter to find.

Returns

T
The formatter whose type is exactly T, or a new instance of T that is not added to the collection.

Remarks

A new instance is created on each call that finds no formatter, rather than shared, so that changing its options cannot affect other clients.

GetAcceptableMediaTypes(Type)

Retrieves the media types that the formatters in the collection can read into a specified model type.
public IEnumerable<string> GetAcceptableMediaTypes(Type modelType)

Parameters

modelType Type
The type of the object to read, or null if any content is acceptable.

Returns

IEnumerable<string>
The media types that can be read into modelType, in the order of the formatters, without duplicates.

Remarks

If modelType is null, this method returns only "*/*", signifying that all media types are acceptable.

Send-only formatters contribute no media types.

GetAcceptableMediaTypes(Type, Type)

Retrieves the media types that the formatters in the collection can read into a specified model type or error type.
public IEnumerable<string> GetAcceptableMediaTypes(Type modelType, Type errorType)

Parameters

modelType Type
The type of the object to read, or null if any content is acceptable.
errorType Type
The type of the error object to read, or null if errors are not read.

Returns

IEnumerable<string>
The media types that can be read into either type, model type first, without duplicates.

Remarks

If errorType is null, the result is the same as for GetAcceptableMediaTypes(Type).

If modelType is null and errorType is provided, the result includes the media types that can be read into errorType, followed by "*/*" to indicate that any content is acceptable for the model.

GetEnumerator()

Returns an enumerator that iterates through the collection.
public IEnumerator<IHttpContentFormatter> GetEnumerator()

Returns

IEnumerator<IHttpContentFormatter>
A IEnumerator<T> for IHttpContentFormatter.

GetReaderFor(string, Type)

Retrieves the first formatter in the collection that can read content of a specific media type into a specific model type.
public IHttpContentFormatter GetReaderFor(string mediaType, Type modelType)

Parameters

mediaType string
The media type of the content.
modelType Type
The type of the object to read.

Returns

IHttpContentFormatter
The first IHttpContentFormatter whose IHttpContentFormatter.CanRead(string, Type) returns true, or null if there is none.

Exceptions

ArgumentNullException
Thrown if mediaType or modelType is null.

GetWriterFor(string, Type)

Retrieves the first formatter in the collection that can write a payload of a specific type in a specific media type.
public IHttpContentFormatter GetWriterFor(string mediaType, Type payloadType)

Parameters

mediaType string
The media type of the content to create.
payloadType Type
The type of the payload to write.

Returns

IHttpContentFormatter
The first IHttpContentFormatter whose IHttpContentFormatter.CanWrite(string, Type) returns true, or null if there is none.

Exceptions

ArgumentNullException
Thrown if mediaType or payloadType is null.

Remove(IHttpContentFormatter)

Removes a formatter from the collection.
public bool Remove(IHttpContentFormatter formatter)

Parameters

formatter IHttpContentFormatter
The IHttpContentFormatter to remove from the collection.

Returns

bool
true if formatter was successfully removed from the collection; otherwise, false.

Explicit Interface Implementations

ICollection<IHttpContentFormatter>.IsReadOnly

Gets a value indicating whether the collection is read-only.
bool ICollection<IHttpContentFormatter>.IsReadOnly { get; }

Property Value

bool
Always false.

ICollection<IHttpContentFormatter>.CopyTo(IHttpContentFormatter[], int)

Copies the elements of the collection to an array, starting at a particular array index.
void ICollection<IHttpContentFormatter>.CopyTo(IHttpContentFormatter[] array, int arrayIndex)

Parameters

array IHttpContentFormatter[]
The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing.
arrayIndex int
The zero-based index in array at which copying begins.

IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()

Returns

IEnumerator
An IEnumerator object that can be used to iterate through the collection.