private void btnStartStreaming_Click(object sender, RoutedEventArgs e) { ChannelFactory <SLApp.Web.IWcfDownloadService> factory = new ChannelFactory <Web.IWcfDownloadService>("CustomBinding_IWcfDownloadService_StreamedResponse"); SLApp.Web.IWcfDownloadService proxy = factory.CreateChannel(); SLApp.Web.DownloadRequest request = new SLApp.Web.DownloadRequest(); request.fileName = "test.bin"; request.fileSize = 1000000000L; // ~1GB Message input = Message.CreateMessage(factory.Endpoint.Binding.MessageVersion, SLApp.Web.Constants.DownloadAction, request); proxy.BeginDownload(input, new AsyncCallback(this.DownloadCallback), proxy); this.AddToDebug("Called proxy.BeginDownload"); }
void DownloadCallback(IAsyncResult asyncResult) { SLApp.Web.IWcfDownloadService proxy = (SLApp.Web.IWcfDownloadService)asyncResult.AsyncState; this.AddToDebug("Inside DownloadCallback"); try { Message response = proxy.EndDownload(asyncResult); this.AddToDebug("Got the response"); if (response.IsFault) { this.AddToDebug("Error in the server: {0}", response); } else { XmlDictionaryReader bodyReader = response.GetReaderAtBodyContents(); if (!bodyReader.ReadToDescendant("DownloadResult")) // Name of operation + "Result" { this.AddToDebug("Error, could not read to the start of the result"); } else { bodyReader.Read(); // move to content long totalBytesRead = 0; int bytesRead = 0; int i = 0; byte[] buffer = new byte[1000000]; do { bytesRead = bodyReader.ReadContentAsBase64(buffer, 0, buffer.Length); totalBytesRead += bytesRead; i++; if ((i % 100) == 0) { this.AddToDebug("Read {0} bytes", totalBytesRead); } } while (bytesRead > 0); this.AddToDebug("Read a total of {0} bytes", totalBytesRead); } } } catch (Exception e) { this.AddToDebug("Exception: {0}", e); } }