Tuesday, June 19, 2012

A first chance exception of type 'System.NullReferenceException'

While debugging an ASP.NET application, I kept encountering errors similar to the following:

A first chance exception of type 'System.NullReferenceException' occurred in MyApp.dll
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.VisualBasic.dll

At first I was simply ignoring the error since my application appears to be working, and except for the assembly DLL name, it doesn't tell me which source file or line number the error is occurring in. Then I happen to find this nice little feature to help me. Go to the Debug menu -> Exceptions -> then drill down to Common Language Runtime Exceptions -> System and look for the specific exception that you're having problems with.




Check the Thrown checkbox, uncheck the User-unhandled checkbox and rerun. Visual Studio will now break at the point where the exception occurs.

Saturday, June 9, 2012

Adding httpModule Handlers in web.config Not Working in IIS 7

There are several tutorials on how to use HTTP modules to extend ASP.NET processing. If you google httpModules, the first several entries point to the Microsoft websites that teach you how to create and configure them. I used this one in particular.

Extending ASP.NET Processing with HTTP Modules

If you follow all the instructions and copy and paste their code, it may not work for you if you are using IIS 7. The problem is that the tutorial was not updated for later versions of IIS (as of the posting of this blog article) and IIS 7 changed the configuration for custom HTTP modules within web.config. The tutorial tells you to add the following in your web.config:

<configuration>
  <system.web>
    <httpModules>
      <add name="RequestTimeIntervalModule" type="Samples.Aspnet.HttpModuleExamples.RequestTimeIntervalModule">
      </add>
    </httpModules>
  </system.web>
</configuration>


Which is how it is in previous versions of IIS. But IIS 7 looks for it here.

<configuration>
  <system.webServer>
    <modules>
      <add name="RequestTimeIntervalModule" type="Samples.Aspnet.HttpModuleExamples.RequestTimeIntervalModule">
      </add>
    </modules>
  </system.webServer>
</configuration>

Note that it is possible to put both sections in your web.config in case you want to run your web application in both versions of IIS. Also, the project migration tool within VS 2008 does not automatically add the corresponding entries in system.webServer even if you have  httpModules defined in your web.config, so this is something you have to add manually when you're upgrading IIS to version 7.