/ Published in: C#
Expand |
Embed | Plain Text
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; using System.Reflection; /* Source: Book[41]pages117-118 With ASP.NET 2.0, the Page class can register asynchronous tasks by using the RegisterAsyncTask method. This not only decreases the page load time but also frees up the worker process threads of IIS. With asynchronous page tasks, the IIS worker thread is returned to the thread pool while the page is waiting for the asynchronous tasks to complete. In this example RSS View Web Part, an asynchronous task can be used to fetch the remote XML data, which will free the IIS worker thread while it is waiting for a response from the remote server. Before initiating the remote call, it is also a good idea to verify that the page is not in design mode, which can be done with a simple check of the Web Part Manager’s Display Mode. Classes that implement the IAsyncResult design pattern work well with asynchronous tasks and are a good design pattern for your own middle tier code. The following code demonstrates the asynchronous task for retrieving remote data using the WebRequest class. Below the oce is a simple XSLT file for an RSS Web Part application that will transform the output by using the XslCompiledTransform class. This file is compiled in as an embedded resource in this example, with the full name LitwareWebParts.Resources.RSS.xslt. (It is available as a resource stream by using the WebPartResources utility class in Listing 4-13.) You also will find an alternate XSLT file for transforming the feed into linked titles only when set to RenderMode.Titles in this chapter’s code samples. */ namespace LitwareWebParts { public class RssViewWebPart : WebPart, IWebEditable { // xmlUrl and RenderMode properties omitted for clarity private Stream xmlResponseStream = null; private WebRequest xmlReq; // Handle any prerender tasks including async operation initiation protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (string.IsNullOrEmpty(this.xmlUrl)) return; // Check to see if we're in design mode and if so skip request if (this.WebPartManager.DisplayMode.AllowPageDesign) { return; }//if try { xmlReq = WebRequest.CreateDefault(xmlUri); xmlReq.Credentials = CredentialCache.DefaultCredentials; xmlReq.Timeout = 10000; // 10 seconds timeout this.Page.RegisterAsyncTask( null, true) ); } catch (System.Security.SecurityException) { this.Controls.Add( } }//OnPreRender() IAsyncResult BeginXmlRequest(object src, EventArgs args, AsyncCallback callback, object state) { return this.xmlReq.BeginGetResponse(callback, state); } void XmlRequestTimeout(IAsyncResult ar) { timeoutLabel.Text = string.Format( "The request timed out while waiting for {0}.", this.XmlUrl); this.Controls.Add(timeoutLabel); }//XmlRequestTimeout void EndXmlRequest(IAsyncResult ar) { WebResponse response = this.xmlReq.EndGetResponse(ar); this.xmlResponseStream = response.GetResponseStream(); }//EndXmlRequest() protected override void RenderContents(HtmlTextWriter writer) { base.RenderContents(writer); if (string.IsNullOrEmpty(this.xmlUrl) || this.xmlResponseStream == null) return; string xslt; if (this.HeadlineMode == RenderMode.Full) xslt = @"Resources.RSS.xslt"; else xslt = @"Resources.RssTitles.xslt"; string resourceName = @"LitwareWebParts" + xslt; using (Stream res = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { transform.Load(stylesheet); } } try { transform.Transform(reader, results); reader.Close(); } } catch (Exception ex) { writer.Write(ex.Message); if (this.xmlResponseStream != null) { this.xmlResponseStream.Close(); this.xmlResponseStream.Dispose(); } } }//RenderContentsd() }//class }//namespace
You need to login to post a comment.
