Example #1
0
        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);
            }
        }