Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Embedded Resource Content
public class EmbeddedContent : HttpContent
{
private readonly Stream _Stream;
public EmbeddedContent(Type locatorType, string filename, MediaTypeHeaderValue contentType = null)
{
Headers.ContentType = contentType ?? new MediaTypeHeaderValue("application/octet-stream");
_Stream = locatorType.Assembly.GetManifestResourceStream(locatorType, filename);
if (_Stream == null) throw new ArgumentException("Cannot find embedded resource " + filename);
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
_Stream.CopyTo(stream);
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
return tcs.Task;
}
protected override bool TryComputeLength(out long length)
{
length = _Stream.Length;
return true;
}
protected override void Dispose(bool disposing)
{
_Stream.Dispose();
base.Dispose(disposing);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment