Skip to content

Instantly share code, notes, and snippets.

@poulfoged
Created September 8, 2015 08:06
Show Gist options
  • Save poulfoged/a2b7c6805db59ddaf390 to your computer and use it in GitHub Desktop.
Save poulfoged/a2b7c6805db59ddaf390 to your computer and use it in GitHub Desktop.
public class StreamFormatter : MediaTypeFormatter
{
private readonly MediaTypeHeaderValue _mediaType;
/// <summary>
/// Default constructor
/// </summary>
/// <param name="mediaType"></param>
public StreamFormatter(MediaTypeHeaderValue mediaType)
{
_mediaType = mediaType;
}
/// <summary>
/// Queries whether this <see cref="T:System.Net.Http.Formatting.MediaTypeFormatter"/> can deserialize an object of the specified type.
/// </summary>
/// <returns>
/// true if the <see cref="T:System.Net.Http.Formatting.MediaTypeFormatter"/> can deserialize the type; otherwise, false.
/// </returns>
/// <param name="type">The type to deserialize.</param>
public override bool CanReadType(Type type)
{
return false;
}
/// <summary>
/// Sets the default headers for content that will be formatted using this formatter. This method is called from the <see cref="T:System.Net.Http.ObjectContent"/> constructor. This implementation sets the Content-Type header to the value of mediaType if it is not null. If it is null it sets the Content-Type to the default media type of this formatter. If the Content-Type does not specify a charset it will set it using this formatters configured <see cref="T:System.Text.Encoding"/>.
/// </summary>
/// <param name="type">The type of the object being serialized. See <see cref="T:System.Net.Http.ObjectContent"/>.</param><param name="headers">The content headers that should be configured.</param><param name="mediaType">The authoritative media type. Can be null.</param>
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
base.SetDefaultContentHeaders(type, headers, _mediaType);
}
/// <summary>
/// Queries whether this <see cref="T:System.Net.Http.Formatting.MediaTypeFormatter"/> can serializean object of the specified type.
/// </summary>
/// <returns>
/// true if the <see cref="T:System.Net.Http.Formatting.MediaTypeFormatter"/> can serialize the type; otherwise, false.
/// </returns>
/// <param name="type">The type to serialize.</param>
public override bool CanWriteType(Type type)
{
return type.IsSubclassOf(typeof(Stream)) || type == typeof(Stream);
}
/// <summary>
/// Asynchronously writes an object of the specified type.
/// </summary>
/// <returns>
/// A <see cref="T:System.Threading.Tasks.Task"/> that will perform the write.
/// </returns>
/// <param name="type">The type of the object to write.</param><param name="value">The object value to write. It may be null.</param><param name="writeStream">The <see cref="T:System.IO.Stream"/> to which to write.</param><param name="content">The <see cref="T:System.Net.Http.HttpContent"/> if available. It may be null.</param><param name="transportContext">The <see cref="T:System.Net.TransportContext"/> if available. It may be null.</param><param name="cancellationToken">The token to cancel the operation.</param><exception cref="T:System.NotSupportedException">Derived types need to support writing.</exception>
public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
{
using (var source = (Stream)value)
await source.CopyToAsync(writeStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment