Wednesday, December 10, 2008

Deleting subdirectory restarts AppDomain

When you deletes a subfolder either manually or programatically from the application root folder of IIS, the ASP.NET runtime automatically restarts AppDomain. To fix this, we will need to turn off the folder monitoring in the Application_Start of the Global.asax.vb, this does not apply on bin folder.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)


StopMonitorFolderChange()


End Sub


Private Sub StopMonitorFolderChange()


Dim p As System.Reflection.PropertyInfo = GetType(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", _


System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Public] Or System.Reflection.BindingFlags.[Static])


Dim o As Object = p.GetValue(Nothing, Nothing)


Dim f As System.Reflection.FieldInfo = o.[GetType]().GetField("_dirMonSubdirs", _


System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.IgnoreCase)


Dim monitor As Object = f.GetValue(o)


Dim m As System.Reflection.MethodInfo = monitor.[GetType]().GetMethod("StopMonitoring", _


System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)


m.Invoke(monitor, New Object() {})


End Sub

Reference:

issue C#: Delete directory kills session

Tuesday, December 9, 2008

HttpHandler

Basics

  • An HTTP Handler is a .NET class that executes whenever you make a request for a file at a certain path.
  • The Page class is an HTTP Handler because it implements the IHttpHandler interface.
  • The IsReusable property indicates whether ASP.NET can keep the handler in memory to service multiple requests, or if it must create a new instance of the handler for every request.
  • Unless you maintain some sort of state in the handler, which is uncommon, IsReusable should always return true.
  • Two ways to create an HTTP Handler:
    • Generic Handler (.ashx): you cannot execute a Generic Handler whenever someone requests a file with the extension .gif. If you need more control over when an HTTP Handler executes, create a class implements IHttpHandler.
    • Custom class implement the IHttpHandler interface.

Class implements IHttpHandler

  • Register HTTP Handlers & Configure HTTPHandler Extension in IIS.
    • IIS 6.0
      • In the httpHandlers section of the web.config, add an entry for the file-name extension. Specify the following four attributes:
        • path: specify the path associated with the handler.
        • verb: specify the HTTP verbs, such as GET or POST, associated with the handler. You can specify multiple verbs in a comma-separated list. You can represent any verb with the * wildcard.
        • type: specify the name of the class that implements the handler.
        • validate: specify whether the handler is loaded during application startup. When true, the handler is loaded at startup. When false, the handler is not loaded until a request associated with the handler is made.
      • add the file extension to map to Aspnet_isapi.dll
      • Verify that file exists check box
        • Check. The file-name extension represents a physical file in the application. If the requested file does not exist on disk, IIS displays an error.
        • Uncheck. The file-name extension does not represent a physical file. Instead, the extension is handled dynamically by a class that is mapped to the extension in ASP.NET.
    • IIS 7.0 (Classic or Integrated mode)
      • Using IIS Manager in IIS 7.0 to add a custom handler extension is equivalent to registering the handler extension in the Web.config file of an ASP.NET application. The registration adds a handler element in the handlers section of the system.webServer group.
      • In the httpHandlers section of the web.config, add an entry for the file-name extension.

References:

ASP.NET 2.0 Unleashed

How to: Configure an HTTP Handler Extension in IIS

How to: Register HTTP Handlers