Recipe 13.5. Uncovering Problems Within Web Application Components


Problem

You want to identify problems within a component of your web application, but your attempts to do so don't seem to work. When you make a call to Trace.Write in the business object, you get a compilation error or the debugger jumps right over the call and no output appears in the trace sequence.

Solution

Import the System.Web namespace and reference the current HTTP context when performing a trace.Write from within the component.

In the component class, use the .NET language of your choice to:

  1. Import the System.Web namespace.

  2. Reference the current HTTP context when performing a TRace.Write,asin HTTPContext.Current.Trace.Write.

The sample component we've written to illustrate this solution appears in Examples 13-7 (VB) and 13-8 (C#). Example 13-9 shows the .aspx file used to test the sample component. The code-behind for the test page appears in Examples 13-10 (VB) and 13-11 (C#). Figure 13-4 shows some sample output, including the resulting trace sequence.

Discussion

For trace.Write to work from within a component, you must be able to access the context for the current HTTP request. The easiest way to accomplish this is to import the System.Web namespace and access the HTTPContext.Current property from within the component.

If a component is not part of your web application, you will need to add a reference to the System.Web.dll assembly in your project. You do this in Visual Studio by selecting the project containing the component in the Solution Explorer. Right-click, and then select Add Reference. In the .NET tab of the displayed dialog box, select System.Web.dll from the list of components, click Select, and click OK.


The HTTPContext provides access to the trace object that allows your application to write trace information, as shown in Examples 13-7 (VB) and 13-8 (C#).

There is one major caveat to this sample: the disadvantage of referencing the current HTTP context in your component is that it does not allow the component to be used in non-web applications. If you need to share components in web and non-web applications, you may want to consider creating a Listener subclass instead, as described in Recipe 13.5.


See Also

Recipe 13.5

Figure 13-4. Trace sequence from testing the component


Example 13-7. The business service class (.vb)

 Option Explicit On Option Strict On Imports System Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides a "web" component for demonstrating outputting ''' trace information from within a class ''' </summary> Public Class CH13TestWebComponentVB Private mStr As String '''*********************************************************************** ''' <summary> ''' This property provides the ability to get/set the string in the object ''' </summary> Public Property theString( ) As String Get Return (mStr) End Get Set(ByVal Value As String) mStr = Value End Set End Property 'theString '''*********************************************************************** ''' <summary> ''' This routine provides the ability to add the passed string to the ''' private string in this object one or more times. ''' </summary> ''' ''' <param name="stringToAdd">Set to the string that is part of this object ''' </param> ''' <param name="numberOfCopies">Set to the number of copies to add ''' </param> Public Sub addToString(ByVal stringToAdd As String, _ ByVal numberOfCopies As Integer) Dim counter As Integer Dim startTime As DateTime Dim elapsedTime As TimeSpan Dim averageTime As Double 'output trace message indicating the start of the concatenations HttpContext.Current.Trace.Write("In Component", _ "Before performing concatenations") 'concatenation the passed string as requested startTime = DateTime.Now( ) For counter = 1 To numberOfCopies mStr &= stringToAdd Next 'output trace message indicating the end of the concatenations HttpContext.Current.Trace.Write("In Component", _ "After performing concatenations") 'calculate the elapsed time for the string concatenations elapsedTime = DateTime.Now.Subtract(startTime) 'Write average time per concatenation in milliseconds to trace sequence averageTime = elapsedTime.TotalMilliseconds / numberOfCopies HttpContext.Current.Trace.Write("In Component", _ "Aver/concat = " &averageTime.ToString("0.0000")) End Sub 'addToString '''*********************************************************************** ''' <summary> ''' This constructor creates the object and initializes the variables ''' in the object ''' </summary> Public Sub New( ) 'initialize string in object mStr = "" End Sub 'New End Class 'CH13TestWebComponentVB End Namespace 

Example 13-8. The business service class (.cs)

 using System; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.Caching; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides a "web" component for demonstrating outputting /// trace information from within a class /// </summary> public class CH13TestWebComponentCS { private String mStr; ///*********************************************************************** /// <summary> /// This property provides the ability to get/set the string in the object /// </summary> public String theString { get { return(mStr); } set { mStr = value; } } // theString ///*********************************************************************** /// <summary> /// This routine provides the ability to add the passed string to the /// private string in this object one or more times. /// </summary> /// /// <param name="stringToAdd">Set to the string that is part of this object /// </param> /// <param name="numberOfCopies">Set to the number of copies to add /// </param> public void addToString(String stringToAdd, int numberOfCopies) { int counter; DateTime startTime; TimeSpan elapsedTime; Double averageTime; // output trace message indicating the start of the concatenations HttpContext.Current.Trace.Write("In Component", "Before performing concatenations"); // concatenation the passed string as requested startTime = DateTime.Now; for (counter = 1; counter <numberOfCopies; counter++) { mStr += stringToAdd; } // output trace message indicating the end of the concatenations HttpContext.Current.Trace.Write("In Component", "After performing concatenations"); // calculate the elapsed time for the string concatenations elapsedTime = DateTime.Now.Subtract(startTime); // Write average time per concatenation in milliseconds to trace sequence averageTime = elapsedTime.TotalMilliseconds / numberOfCopies; HttpContext.Current.Trace.Write("In Component", "Aver/concat = " + averageTime.ToString("0.0000")); } // addToString ///*********************************************************************** /// <summary> /// This constructor creates the object and initializes the variables /// in the object /// </summary> public CH13TestWebComponentCS( ) { // initialize string in object mStr = ""; } // CH13TestWebComponentCS } // CH13TestWebComponentCS } 

Example 13-9. Code to test tracing in the component (.aspx)

 <%@ Page Trace="True" Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH13TestTraceWithinWebComponentVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH13TestTraceWithinWebComponentVB" Title="Test Trace Within Web Component" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Tracing Within Web Components (VB) </div> </asp:Content> 

Example 13-10. Code to test tracing in the component code-behind (.vb)

 Option Explicit On Option Strict On Imports System Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides the code-behind for ''' CH13TestTraceWithinWebComponentVB.aspx ''' </summary> Partial Class CH13TestTraceWithinWebComponentVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim webComponent As CH13TestWebComponentVB 'create the "web aware" component webComponent = New CH13TestWebComponentVB 'add a string to the string in the component 1000 times webComponent.addToString("1234567890", _ 1000) End Sub 'Page_Load End Class 'CH13TestTraceWithinWebComponentVB End Namespace 

Example 13-11. Code to test tracing in the component code-behind (.cs)

 using System;  using System.Configuration; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides the code-behind for /// CH13TestTraceWithinWebComponentCS.aspx /// </summary> public partial class CH13TestTraceWithinWebComponentCS : System.Web.UI.Page { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) { CH13TestWebComponentCS webComponent = null; // create the "web aware" component webComponent = new CH13TestWebComponentCS( ); // add a string to the string in the component 1000 times webComponent.addToString("1234567890", 1000); } // Page_Load } // CH13TestTraceWithinWebComponentCS } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net