Google Search

Google
 

Monday, August 25, 2008

Video: How Do I: Handle Application Events using an HttpModule?

Learn how to create an HttpModule that handles application events during the processing of a page request. The sample module will write information to a log file at various points in the page lifecycle. We also see how the HttpModule is configured for use in a sample web site.

The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video


Minute 0

Hi this is for spells in this video were to learn how to create an HT key people to her in ASP net for seeming to do to create a filter is to create a class that implements the IHT keep the module interface Soquel peering at a new project or solution and would include this in Visual Basic and will make a class library project and let s close HET be filtered DB craziness as is said in the DB could easily created C sharp let s call our class eight TTP filter to the screen name adhere to an end stereo and we need to have a reference

Minute 1

To the system Web namespace so let s add a reference here because that will be in our project by default Okay adhere to system Web had a reference for that is to show all files here and check out our references to become okay now roll set to start implementing our class and as I said it needs to implement the IHT keep the module interface lets put that in canal and Megan are life user here within into it at a few imports to our class so we can don t type the fully qualified namespace

Minute 2

then many hearing led us to a system Web and let s put system diagnostics and while were at it let s define a namespace here and let school in the HDI for how to why it s closed that off here are a unique implement two methods for the 80 IHD module interface is hard in our little squiggly line here so I ll just pursued into that hit enter and we get the dispose and end it interfaces that we need to implement for the minute event or method is called when the application in this case the above application is initialized and the dispose has called one age

Minute 3

Stamp so ridiculous here in our sample is right out some information to a text file at various points in the filtering process to show how you would capture information and at least one way that you can write it out I was to be a lot of options for writing out information if that s what you want to do solicit up with the private variables here in the first on the setup is a name for the file where we store things minimalist set up a file writer innocence of the screenwriter actually mandatory in our writer information and out and so this would be a system I O Stream writer and we will store the context variable information which comes in down to the mid method down below slit

Minute 4

Make a private rainbow handle calls this application contexts and said he is going to be an instance of a TTT key application of it has started here in a lesser known tidbit method and add a few things in here and firstly wanted to use create a file named so that we have some place to store the information of and for the demo over to do is take that filename variable in who we are going to take the application column context coming in here context object that is contained within the application and the request and down requests and will take a fiscal application path so it puts it in the folder where application running and let s just call this

Minute 5

HDTV PE cultic IPX kicks the then as I said is couple of the things we want us to our soda the next thing they ll do here is take that model wide application context variable and in the end we will store the inbound application context passed in mid method and it looks good for starters okay let s read out the first information for file here in the end it methods it to do that we need to set up our file writer can be created up above so that the new system I O Stream writer and here we need to pass the file name that we have stored in the module variable and it will set put true to indicate that we wanted opened

Minute 6

Be a pen mode so what will an array let s write out here right line and will just write in the current date and time is now to such strength and this is just didn t be a timestamp as to when we write the different things out the hour is in its second sailboat fractions of a second because this will be going through pretty quickly and right and then let s just write a brief message that says and it so we know that it is in this part of the program at this point in time and womb is flush out the contents of the writer

Minute 7

And four in this demo were that close to writer here so lets to the same sort of thing out there will title your let s just correct to bad there we go through what do the same sort of thing up in the dispose method so will copy this here at this up here so that we know when we re in the dispose method and will just change in it to dispose so this point now always done is write out some information for text file for the initialization and to dispose of the application I see you could do

Minute 8

Trust take a look at and this word document is included in the sample code downloads a look at this and are also available in the online documentation MSDN as you can see here there is a number of points in a processing in which you can intervene and what were Hindu leaders implement one of these as an example of how you would implant all the rest them up into a begin request event for one thing to note before we leave this and go back to Visual Studio is that this list here indicates the order in which these are events are fired so that if you want to intervene at the processor do things in a certain sequence you need to consider the sequence in which the events are fired so we ll go back or to Visual Studio now and let s implement the begin request event so in order to do that first thing we re going to need to do is win it will appear to our

Minute 9

Module line variables and we need is another one which will be an event handler for the begin request event so bad that I will call it the EB games request event handler and medicine be of type event him and we need to add that an event handler down here when the application initializes so want to then write down here and we need to a head handler then we want to add that to the application context begin request the event and he can see the list of events that we just saw the Word document and this will be implemented to map to and on the game

Minute 10

Request event with Chandler which were then ahead momentarily here know you didn t down and create that event handler county or demand and a proof to do so called on begin request and that s going to accept it he send their this object and the event are says that talks so this event handler will fire at the beginning of any request to our website so there is more and more little bit of cleanup that we should do with respect this event handler and what does that appear

Minute 11

In our dispose method and what will do here is that we will remove the handler and that s and application context begin request and we are removing the begin quest event and are then then we will just set that event handler damages from equaled nothing to now let s write some information out for file every time that begin request event fires so we ll take our code here very in file included down here in our begin request event

Minute 12

And we will just write out here on begin request since this event fires every time have never quest starts less said than a little more information now were riding out toward file here and let s take that application context and its go to the request object again and let s take the user agent information right that out in the pool that s right out of a couple more pieces here and will read out the user a host address and Leslie

Minute 13

Let s write out the one we read unless they do URL string and write that celebs galore should be all set here to test out Ari Kiki to your filter or who he had lessened information being written down when we initialize and dispose of the application as well as with the request in order to install and test our filter the pressing need to do is compile the assembly selects to that and everything looks good there now we need to add a simple website where we can install and test the filter when a new website is an ASP net website handle website DB

Minute 14

And then the first thing that will need to do in the website is that a reference to our assembly that contains REH cheeky PE module will add the reference in this case will just reference the class Library project in our solution and we need to go to the web config file and Ashley install the eight TT piedmont will do then we add an element to the HDTV modules section and we need to head the fully qualified class name and as each TTP filter VB namespace as HDI and class and as each TTP filter need to give it a unique identifying name test the HDTV filter and then we ll were given to it is covert hard to fault this

Minute 15

Ask page and to settle Texaco need to have text in order for the module or the folder to function but this so let us know that the page has fully loaded which is what testing HTP he filled her and then in order to see what s happening at different points will set a few breakpoints over here in our class at the beginning of each of the methods and data will allow us to see when we hit those different points in the application and on around this in Firefox rather than IE because if we run it in IE in the debug mode and then we shut down i e it shuts down the debug mode in Visual Studio and we would never see the dispose method being reached it would be reached and it would write out our text or text file but we would build to see it but we do it in Firefox which doesn t automatically shut down the debug mode Visual Studio we will see it reached that breakpoint

Minute 16

Sluts make our website the startup project here and let s fire things up in McGill started in me debug mode and will lead the Web config be modified to enable debugging in the first method please should hit is our ended method and there it is for the applications starting who will continue on from there and will next reach the fun begin requests which is handed we added to be called right when the request is first initiated

Minute 17

Going in the debug code in Visual Studio for what I ll do is only down to the local server and stop that and when I stopped that you see that we reach the dispose method will let it continue on actually shut everything down to look back over to her website here and just refresh and we should see our log file know which we do let s open that up and see here is the log file and it particles will resolve the debugger first we rode out from the initiative and then beyond begin requests when requests first started from the webpage and aide wrote out some information about the browser of IP address URL and then we get dispose event has hit Haitian shutdown so this particular sample we intervened in the processing of an HDP module by adding an event

Minute 18

Pour on begin requests and as I mentioned earlier there are a number of events where you can intervene and do various things here we were recording information but you could perform any activity that you need at any of these points in time in summary in this video we learned how to create an HT key piece filter by creating a class that implemented the IHT keep the module interface are too required methods dispose and admit which we implemented then there s a list of methods which will represent the various points in the processing of a page where you can intervene and to whatever functionality you need to do at those points in this case we implemented the begin requests

Minute 19

Load the source code that comes with this video take a look at it in more detail hope you found this video informative this is Chris Bell s thank you very much for your time

Click here to go to original video page

No comments: