
December 27, 2013 11:03 by
Robert
In this article, I showed you how to use Page Instrumentation, a new feature in ASP.NET 4.5 which might be useful for you during debugging.
ASP.NET 4.5 include a hidden gem called Page Instrumentation, very few people aware of this gem. You can use page instrumentation in ASP.NET 4.5 WebForm and MVC 5 or 4(assuming it targets 4.5). It allows you to inspect/instrument a web form or a mvc view during the rendering process. Page instrumentation are useful in scenarios when you have some performance issues regarding view engine rendering. In this article, I will show you how to use this feature in ASP.NET 4.5.

To start instrumenting a page, you need to inherit PageExecutionListener class.

The BeginContext will be called by a view engine before it renders the output for the specified context. Similarly, EndContext called by a view engine after it renders the output for the specified context. Both of these methods accept PageExecutionContext class as a parameter which have the following members.

All the properties of PageExecutionContext class are self explanatory. Let assume we have the following MVC view(for understanding how many times and when the BeginContext and EndContext will be invoke by the framework).

The Razor View Engine will emit the following C# code

Finally, here is a simple implementation of PageExecutionListener class which calculates the total time each time whenever something is written to the output response.

4898627a-0269-4ee1-ad40-a2e3445c5bba|0|.0

December 20, 2013 05:44 by
Robert
Sometimes your ASP.NET application needs to hook up some code before even the Application is started. Assemblies supports a custom attribute called PreApplicationStartMethod which can be applied to any assembly that should be loaded to your ASP.NET application, and the ASP.NET engine will call the method you specify within it before actually running any of code defined in the application.
Lets discuss how to use it using Steps :
1. Add an assembly to an application and add this custom attribute to the AssemblyInfo.cs. Remember, the method you speicify for initialize should be public static void method without any argument. Lets define a method Initialize. You need to write :
[assembly:PreApplicationStartMethod(typeof(MyInitializer.InitializeType), "InitializeApp")]
2. After you define this to an assembly you need to add some code inside InitializeType.InitializeApp method within the assembly.
public static class InitializeType
{
public static void InitializeApp()
{
// Initialize application
}
}
3. You must reference this class library so that when the application starts and ASP.NET starts loading the dependent assemblies, it will call the method InitializeApp automatically.
Warning
Even though you can use this attribute easily, you should be aware that you can define these kind of method in all of your assemblies that you reference, but there is no guarantee in what order each of the method to be called. Hence it is recommended to define this method to be isolated and without side effect of other dependent assemblies.
The method InitializeApp will be called way before the Application_start event or even before the App_code is compiled.
This attribute is mainly used to write code for registering assemblies or build providers.
d6566e11-b5bd-4715-ab11-d5524b6004fe|0|.0