views:

155

answers:

1

I am using Global.asax to perform logging at the end of each request via the Application_EndRequest event. However, I am seeing some odd behavior of certain values stored in the HTTPContext.Current.Items collection.

Below is the debug output for a nullable Enum. You can see that there is a value, but HasValue resolved to False?!

{System.Nullable(Of AreaNameEnum)}
    HasValue: False
    hasValue: False
    Value: {System.InvalidOperationException}
    value: ADMIN {0}

I am guessing that it is too late in the request lifecycle to access the HTTPContext.Current - but it seems to sometimes work and sometimes not. Can anyone shed any more light on exactly what is going on?

Thanks

+1  A: 

Nullable is a structure that contains a boolean hasValue and a T value where T is a value type. In this case an enum. The enum has to have some value in this case the default 0, however the public Value throws an exception because hasValue is false.

What you are seeing is the internals of how Nullable does what it does. You cannot read anything into the internal value field having any value when hasValue is false. After all if value could contain null there wouldn't be any point in using Nullable here.

AnthonyWJones
Yes, that makes sense now I think about it. I guess the HTTPContext stuff was a red herring. Thanks.
James