Posted By

realB12 on 03/12/11


Tagged

thread Asynchronous sharepoint


Versions (?)

S002 Asynchronous WebPart Processing


 / Published in: C#
 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Xml;
  10. using System.Xml.XPath;
  11. using System.Xml.Xsl;
  12. using System.Reflection;
  13.  
  14. /*
  15. Source:
  16. Book[41]pages117-118
  17. With ASP.NET 2.0, the Page class can register asynchronous tasks by using the RegisterAsyncTask method.
  18. This not only decreases the page load time but also frees up the worker process threads of IIS.
  19. With asynchronous page tasks, the IIS worker thread is returned to the thread pool while the page is
  20. waiting for the asynchronous tasks to complete.
  21. In this example RSS View Web Part, an asynchronous task can be used to fetch the remote XML data, which
  22. will free the IIS worker thread while it is waiting for a response from the remote server.
  23. Before initiating the remote call, it is also a good idea to verify that the page is not in design mode,
  24. which can be done with a simple check of the Web Part Manager’s Display Mode.
  25. Classes that implement the IAsyncResult design pattern work well with asynchronous tasks and are a good
  26. design pattern for your own middle tier code.
  27. The following code demonstrates the asynchronous task for retrieving remote data using the WebRequest class.
  28. Below the oce is a simple XSLT file for an RSS Web Part application that will transform the output by using
  29. the XslCompiledTransform class. This file is compiled in as an embedded resource in this example, with the
  30. full name LitwareWebParts.Resources.RSS.xslt.
  31. (It is available as a resource stream by using the WebPartResources utility class in Listing 4-13.)
  32. You also will find an alternate XSLT file for transforming the feed into linked titles only when set to
  33.  RenderMode.Titles in this chapter’s code samples.
  34. */
  35. namespace LitwareWebParts {
  36.  
  37. public class RssViewWebPart : WebPart, IWebEditable {
  38. // xmlUrl and RenderMode properties omitted for clarity
  39.  
  40. private Stream xmlResponseStream = null;
  41. private WebRequest xmlReq;
  42.  
  43. // Handle any prerender tasks including async operation initiation
  44. protected override void OnPreRender(EventArgs e) {
  45. base.OnPreRender(e);
  46. if (string.IsNullOrEmpty(this.xmlUrl))
  47. return;
  48. // Check to see if we're in design mode and if so skip request
  49. if (this.WebPartManager.DisplayMode.AllowPageDesign) {
  50. this.Controls.Add(new LiteralControl("No display while in design mode."));
  51. return;
  52. }//if
  53.  
  54. try {
  55. Uri xmlUri = new Uri(this.xmlUrl);
  56. xmlReq = WebRequest.CreateDefault(xmlUri);
  57. xmlReq.Credentials = CredentialCache.DefaultCredentials;
  58. xmlReq.Timeout = 10000; // 10 seconds timeout
  59. this.Page.RegisterAsyncTask(
  60. new PageAsyncTask(new BeginEventHandler(BeginXmlRequest),
  61. new EndEventHandler(EndXmlRequest),
  62. new EndEventHandler(XmlRequestTimeout),
  63. null, true)
  64. );
  65. } catch (System.Security.SecurityException) {
  66. this.Controls.Add(
  67. new LiteralControl("Permission denied - set trust level to WSS_Medium."));
  68. }
  69. }//OnPreRender()
  70.  
  71. IAsyncResult BeginXmlRequest(object src, EventArgs args, AsyncCallback callback, object state) {
  72. return this.xmlReq.BeginGetResponse(callback, state);
  73. }
  74.  
  75. void XmlRequestTimeout(IAsyncResult ar) {
  76. Label timeoutLabel = new Label();
  77. timeoutLabel.Text = string.Format(
  78. "The request timed out while waiting for {0}.", this.XmlUrl);
  79. this.Controls.Add(timeoutLabel);
  80. }//XmlRequestTimeout
  81.  
  82. void EndXmlRequest(IAsyncResult ar) {
  83. WebResponse response = this.xmlReq.EndGetResponse(ar);
  84. this.xmlResponseStream = response.GetResponseStream();
  85. }//EndXmlRequest()
  86.  
  87. protected override void RenderContents(HtmlTextWriter writer) {
  88. base.RenderContents(writer);
  89. if (string.IsNullOrEmpty(this.xmlUrl) || this.xmlResponseStream == null)
  90. return;
  91. XslCompiledTransform transform = new XslCompiledTransform();
  92. string xslt;
  93. if (this.HeadlineMode == RenderMode.Full)
  94. xslt = @"Resources.RSS.xslt";
  95. else
  96. xslt = @"Resources.RssTitles.xslt";
  97. string resourceName = @"LitwareWebParts" + xslt;
  98. using (Stream res =
  99. Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
  100. using (XmlTextReader stylesheet = new XmlTextReader(res)) {
  101. transform.Load(stylesheet);
  102. }
  103. }
  104.  
  105. try {
  106. using (XmlReader reader = new XmlTextReader(this.xmlResponseStream)) {
  107. XmlTextWriter results = new XmlTextWriter(writer.InnerWriter);
  108. transform.Transform(reader, results);
  109. reader.Close();
  110. }
  111. } catch (Exception ex) {
  112. writer.Write(ex.Message);
  113. if (this.xmlResponseStream != null) {
  114. this.xmlResponseStream.Close();
  115. this.xmlResponseStream.Dispose();
  116. }
  117. }
  118. }//RenderContentsd()
  119.  
  120. }//class
  121. }//namespace
  122.  
  123.  

Report this snippet  

You need to login to post a comment.