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
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
formatterIHttpContentFormatter- The IHttpContentFormatter to add.
Exceptions
- ArgumentNullException
- Thrown if
formatterisnull. - 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
formatterIHttpContentFormatter- The IHttpContentFormatter to locate in the collection.
Returns
Find<T>()
Finds the formatter of the specified type.
public T Find<T>()
where T : IHttpContentFormatterType Parameters
T- The type of the formatter to find.
Returns
- T
- The formatter whose type is exactly
T, ornullif 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 ofTthat 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
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
modelTypeType- The type of the object to read, or
nullif any content is acceptable. errorTypeType- The type of the error object to read, or
nullif 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
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
Returns
- IHttpContentFormatter
- The first IHttpContentFormatter whose IHttpContentFormatter.CanRead(string, Type) returns
true, ornullif there is none.
Exceptions
- ArgumentNullException
- Thrown if
mediaTypeormodelTypeisnull.
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
mediaTypestring- The media type of the content to create.
payloadTypeType- The type of the payload to write.
Returns
- IHttpContentFormatter
- The first IHttpContentFormatter whose IHttpContentFormatter.CanWrite(string, Type) returns
true, ornullif there is none.
Exceptions
- ArgumentNullException
- Thrown if
mediaTypeorpayloadTypeisnull.
Remove(IHttpContentFormatter)
Removes a formatter from the collection.
public bool Remove(IHttpContentFormatter formatter)Parameters
formatterIHttpContentFormatter- The IHttpContentFormatter to remove from the collection.
Returns
Explicit Interface Implementations
ICollection<IHttpContentFormatter>.IsReadOnly
Gets a value indicating whether the collection is read-only.
bool ICollection<IHttpContentFormatter>.IsReadOnly { get; }Property Value
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
arrayIHttpContentFormatter[]- The one-dimensional array that is the destination of the elements copied from the collection. The array must have zero-based indexing.
arrayIndexint- 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.

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
Acceptheader 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.