void  wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                return;
            }

            if (e.Error != null)
            {
                bool redownloadAttempted = WebClientFactory.RedownloadAttempted.Contains(wc);
                if (Utility.IsMessageLimitExceededException(e.Error) && !redownloadAttempted)
                {
                    // Re-issue the request which should serve it out of cache
                    // and helps us avoid the error which is caused by setting AllowReadStreamBuffering=false
                    // which was used to workaround the problem of SL4 and gzipped content
                    WebClientFactory.RedownloadStringAsync(wc, new Uri(File.Path, UriKind.RelativeOrAbsolute), e.UserState);
                }
                else
                {
                    if (redownloadAttempted)
                    {
                        WebClientFactory.RedownloadAttempted.Remove(wc);
                    }
                    OnGetFileAsTextFailed(new ExceptionEventArgs(e.Error, e.UserState));
                }
                return;
            }

            OnGetFileAsTextCompleted(new GetFileAsTextCompletedEventArgs()
            {
                FileContents = e.Result, UserState = e.UserState
            });
        }
示例#2
0
        public override void GetLayout(object userState, EventHandler <LayoutEventArgs> onCompleted, EventHandler <ExceptionEventArgs> onFailed)
        {
            if (!string.IsNullOrEmpty(LayoutFileContents))
            {
                try
                {
                    LayoutEventArgs args = ParseXamlFileContents(LayoutFileContents);
                    if (args != null)
                    {
                        if (onCompleted != null)
                        {
                            onCompleted(this, args);
                        }
                    }
                    else
                    {
                        if (onFailed != null)
                        {
                            onFailed(this, new ExceptionEventArgs(Resources.Strings.ExceptionUnableToParseFile, userState));
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (onFailed != null)
                    {
                        onFailed(this, new ExceptionEventArgs(ex, userState));
                    }
                }
                return;
            }

            if (string.IsNullOrEmpty(FileUrl))
            {
                throw new InvalidOperationException(Resources.Strings.ExceptionMustSpecifyFileUrl);
            }

            Uri uri = null;

            if (!Uri.TryCreate(FileUrl, UriKind.RelativeOrAbsolute, out uri))
            {
                throw new InvalidOperationException(Resources.Strings.ExceptionMustSpecifyValidFileUrl);
            }

            WebClient webClient = WebClientFactory.CreateWebClient();

            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(layoutFileDownloadCompleted);
            webClient.DownloadStringAsync(uri, new object[] { onCompleted, onFailed, userState });
        }
        public void GetFileAsTextAsync(object userState)
        {
            if (File == null)
            {
                throw new InvalidOperationException(Resources.Strings.ExceptionMustSpecifyFile);
            }

            if (File.IsUrl)
            {
                wc = WebClientFactory.CreateWebClient();
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                wc.DownloadStringAsync(new Uri(File.Path, UriKind.RelativeOrAbsolute), userState);
            }
            else
            {
                OnGetFileAsTextFailed(new ExceptionEventArgs(new Exception(Resources.Strings.ExceptionInvalidFileMustBeUrlForSilverlightApplication), userState));
            }
        }
        public WebClient BackgroundOpenReadAsync(Uri address, object userToken)
        {
            WebClient webClient = WebClientFactory.CreateWebClient();

            // Create a background worker thread to execute the async read without buffering which can only be done on a
            // background thread.
            BackgroundWorker worker = new BackgroundWorker()
            {
                WorkerReportsProgress      = false,
                WorkerSupportsCancellation = false,
            };

            worker.DoWork += (sender, args) =>
            {
                webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                webClient.OpenReadAsync(address, userToken);
            };
            worker.RunWorkerAsync();

            return(webClient);
        }