Friday, 21 September 2012

HandleErrorAttribute, Elmah and the Yellow Screen of Death

If you apply the HandleErrorAttribute to an MVC application then it will redirect you to the Error page rather than serve up the Yellow Screen of Death.

But customErrors must be turned on for this to work.

<system.web>
  <customErrors mode="On">
  </customErrors>
</system.web>
Doing this will handle exceptions (e.g. the ExceptionContext's ExceptionHandled property will be set to true).

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
       public override void OnException(ExceptionContext context)
{

          
base.OnException(context);


           // context.ExceptionHandled will return true
if (!context.ExceptionHandled)
return;
          
}

Elmah will only pickup on UNHANDLED exceptions. Under normal circumstances it won't do anything with the UI, it will leave the Yellow Screen of Death intact if one is served. However it will pickup the error and email and log as appropriate.

To use Elmah in conjunction with the HandleErrorAttribute you have to add code to fire off Elmah even though the exception was handled.

A simple example is shown below (with a CustomHandleErrorAttribute)

public
class CustomHandleErrorAttribute : HandleErrorAttribute

{

private readonly ILog log;

public CustomHandleErrorAttribute(ILog log)

{

this.log = log;

}

public override void OnException(ExceptionContext context)

{

// Give the framework a chance to handle the exception

base.OnException(context);

// If the exception was not handled then allow the exception to bubble up - Elmah will catch it

if (!context.ExceptionHandled) return;

// If the exception was handled by the framework then log it for the record

var httpContext = context.HttpContext.ApplicationInstance.Context;

var signal = ErrorSignal.FromContext(httpContext);

signal.Raise(context.Exception, httpContext);

log.Error(context.Exception);

}

}
References:

http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html

Wednesday, 19 September 2012

403 Access is denied due to invalid credentials, MVC3, IIS7

I had a MVC3 application that compiled and ran fine locally, but when deployed to the UAT server running IIS7, the application failed and returned a 403 Access Denied error.

After a long debugging session I determined this was due to the fact that certain MVC DLLs were not present on the target machine. One way to get round this is to explicitly add them as references to the Web project, and select Copy Local to true.

The DLLs I had to add were

System.Web.WebPages
System.Web.WebPages.Razor

Other reasons why it may return such an unhelpful error are:

the application errors in the startup routines
the directory permissions are not correct
Windows authentication is not enabled for the application in IIS7

....and others

Visual Studio remote debugger is version sensitive

The Visual Studio remote debugger has to be connected to the same version as the main IDE.
You cannot connect the 2010 remote debugger to VS.NET 2012, and vice-versa!

Thursday, 13 September 2012

Watching DVDs on the train

To rip a DVD for watching on the train:

1. Use DVD Decrypter to extract the contents of the DVD onto the file system.
2. Then use Handbrake to convert it into a watchable format.