The WCF REST Starter kit introduced a new mechanism for addressing this scenario, an “AdapterStream” utility class, which basically pushes small pieces of content (and thus, only
small memory buffers are used) to the client application as it becomes necessary.
Using this class i have implemented the Download service. It works fine. But i have to read the whole file in memory to use it with the AdapterStream. Questions
1>How does adapter stream pushes chunk to consumer?
2>Can someone suggest more efficient way of reading file in chunk and push to consumer? Using adapter stream or some other way if possible
my current code
[WebHelp(Comment = "Returns a pdf file")]
[WebGet(UriTemplate = "DownloadDocument?returnid={returnid}")]
[OperationContract]
Stream DownloadDocument(long returnid)
{
string filepath = System.Web.HttpContext.Current.Server.MapPath("Sample.pdf");
byte[] buffer = null;
using (FileStream st = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
int length = (int)st.Length;
buffer = new byte[length];
st.Read(buffer, 0, length);
}
WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
return new AdapterStream((writer) => writer.Write(buffer, 0, buffer.Length));
}
lax4u
Participant
1592 Points
902 Posts
How to use AdapterStream for downloading large file in WCF REST service?
Jan 06, 2012 03:56 PM|LINK
The WCF REST Starter kit introduced a new mechanism for addressing this scenario, an “AdapterStream” utility class, which basically pushes small pieces of content (and thus, only small memory buffers are used) to the client application as it becomes necessary.
Using this class i have implemented the Download service. It works fine. But i have to read the whole file in memory to use it with the AdapterStream. Questions
1>How does adapter stream pushes chunk to consumer?
2>Can someone suggest more efficient way of reading file in chunk and push to consumer? Using adapter stream or some other way if possible
my current code
[WebHelp(Comment = "Returns a pdf file")] [WebGet(UriTemplate = "DownloadDocument?returnid={returnid}")] [OperationContract] Stream DownloadDocument(long returnid) { string filepath = System.Web.HttpContext.Current.Server.MapPath("Sample.pdf"); byte[] buffer = null; using (FileStream st = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { int length = (int)st.Length; buffer = new byte[length]; st.Read(buffer, 0, length); } WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf"; return new AdapterStream((writer) => writer.Write(buffer, 0, buffer.Length)); }