Created
June 11, 2014 20:56
-
-
Save darrelmiller/d688282dfe8167fcc101 to your computer and use it in GitHub Desktop.
Embedded Resource Content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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