Implementing HttpModule Events


Events implemented in an HttpModule can be handled in the Global.asax file. These events are handled in the same manner as the ASP.NET HttpModule's events are handled. An event in an HttpModule is implemented like any other event. Your event should have two parameters in the following format:

 //C#  EventName(object sender, EventArgs e);  'VB  EventName(ByVal sender As Object, ByVal e As EventArgs) 

If you need to send data with the event, you should derive a class from the System.EventArgs class and add the necessary member variables and properties to hold your data. If no data is sent with the event, you should use the EventArgs class directly when raising the event.

Because the implementation details for events differ significantly between C# and VB, they are described separately. In either case, the hooking up of the event to the event handler in the Global.asax file is done automatically when the HttpModule is initialized .

VB.NET Event Implementation

Visual Basic .Net makes it easy to implement and use events. To implement an event, you need to add the following line to your class definition:

 Event MyEvent(ByVal sender As Object, ByVal e As EventArgs) 

To raise an event, you need to add the following line to the method that triggers the event:

 RaiseEvent MyEvent(sender, e) 

Sender is usually the object that initiates the event, and e is an EventArgs -derived class that passes data to the event handler.

Listing 25.3 show an HttpModule that handles an HttpApplication event and then fires its own event.

Listing 25.3 Visual Basic .NET Code Showing an Event Implemented in an HttpModule
 Public Class MyHttpModule    Implements IHttpModule    ' Implement the IHttpModule interface.    Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init        AddHandler context.BeginRequest, AddressOf OnBeginRequest    End Sub    Public Sub Dispose() Implements IHttpModule.Dispose    End Sub    ' Implement event to be handled in Global.asax file    Event MyEvent(ByVal sender As Object, ByVal e As EventArgs)    ' Implement the event handler    Public Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)        ' Do something here        RaiseEvent MyEvent(ME, New EventArgs())    End Sub  End Class 

In the OnBeginRequest method, Visual Basic .NET uses the ME keyword to pass the current class as a parameter when raising the event. C# uses this keyword for the same purpose.

C# Event Implementation

The way that C# implements events is not as simple as the way Visual Basic .NET does, and it forces you to deal more directly with how the compiler actually implements the event. To implement and use an event in C#, you need to do the following:

  • Declare a delegate outside the class scope.

  • Declare an event as a class member of the delegate type.

  • Call the event as you would a function, after first checking to be sure that it is not null when you wish to raise the event. A null event indicates that no event handler is connected to the event.

A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature. A delegate is equivalent to a type-safe function pointer or a callback. Events in .NET are implemented using the callback functionality delegates. Visual Basic .NET hides the use of delegates with the use of the Event and RaiseEvent keywords. When using events in C#, however, you must use the delegate directly.

A delegate is declared like this:

 public delegate void MyEventHandler(object sender, EventArgs e); 

An event is declared inside the class like this:

 public event MyEventHandler MyEvent; 

And finally, the event can be raised with the following code:

 if(MyEvent!=null)  {      MyEvent(this,new EventArgs());  } 

Listing 25.4 shows the complete C# implementation of an event in an HttpModule.

Listing 25.4 C# Code Showing an Event Implemented in an HttpModule
 public delegate void MyEventHandler(object sender, EventArgs e);  public class MyHttpModule : IHttpModule  {      // Declare events      public event MyEventHandler MyEvent;      // Constructor      public MyHttpModule()      {      }      // Implement IHttpModule Methods      public void Init(HttpApplication context)      {          context.BeginRequest += new EventHandler(OnBeginRequest);      }      public void Dispose()      {      }      // Implement the event handlers.      public void OnBeginRequest(Object sender, EventArgs e)      {          HttpApplication app=sender as HttpApplication;           // Do something here          if(MyEvent!=null)          {              MyEvent(this,new EventArgs());          }      }  } 

If you compare Listing 25.4 with the Visual Basic .NET implementation in Listing 25.3, you can see that it is much simpler to implement an event in Visual Basic .NET than in C# because Visual Basic .NET hides the use of delegates.


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233 © 2008-2017.
If you may any questions please contact us: