Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created June 11, 2014 20:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darrelmiller/d688282dfe8167fcc101 to your computer and use it in GitHub Desktop.
Save darrelmiller/d688282dfe8167fcc101 to your computer and use it in GitHub Desktop.
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