1
votes

When hooking up an ASP.NET website to Azure Redis Cache (but this error also occurs when using a local redis instance) using the Microsoft ASP.NET Redis Session State provider, I get a Null Reference Exception. Why? Google tells me nothing. I have tried using the Russian Redis Session State provider, but that randomly corrupts the session state, so I can't use that either.

This is the stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.Web.Redis.StackExchangeClientConnection.Eval(String script, String[] keyArgs, Object[] valueArgs) +381
Microsoft.Web.Redis.RedisConnectionWrapper.TryUpdateIfLockIdMatch(Object lockId, ISessionStateItemCollection data, Int32 sessionTimeout) +108
Microsoft.Web.Redis.RedisSessionStateProvider.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +1280
System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +565
System.Web.SessionState.SessionStateModule.OnEndRequest(Object source, EventArgs eventArgs) +139
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

I have no idea where this problem occurs as the stack trace doesn't touch my code, but it occurs on any url hitting my site after I am logged in, yes using forms authentication.

My redis config looks like this:

<sessionState mode="Custom" customProvider="RedisSessionStateStoreProvider">
  <providers>
    <clear />
     <add name="RedisSessionStateStoreProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider, Microsoft.Web.RedisSessionStateProvider, Version=0.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
          host="*******.redis.cache.windows.net"
          throwOnError = "true" 
          connectionTimeoutInMilliseconds = "5000" 
          retryTimeoutInMilliseconds = "5000" 
          operationTimeoutInMilliseconds = "5000" 
          ssl="true"
          accessKey="*******************************"
          />
  </providers>

... where I have starred out the access key and service uri.

EDIT: I was happy, and thought I had found the solution when I found a post on MSDN Forums (booh!) that dealt with intermittent null reference exceptions where they warned that a nonexistent key being assigned with with a null value will break everything. I found an instance where I was doing this and fixed it,but that didn't actually do the trick and the same problem remains.

I am using Microsoft.Web.RedisSessionStateProvider 0.4.0.0-Pre-121 that allegedly fixes a similar bug

1
Sounds like a bug in the MS code; IIRC, I have looked at this one before (reported separately), and via the IL found a few possible null-ref sources in that code. You should report it to the MS team, though. - Marc Gravell
We (Azure Cache team) were not able to reproduce the issue at our end. Would it be possible for to send us a small repro app?. You can contact us on AzureCache AT microsoft.com for further communication. If you can contact us, we can provide you pdb file that you can use so we get actual line number with exception. - Saurabh
@Saurabh, I'm afraid I cannot furnish you with a proper repro, but it does seem like we are doing something that ASP SessionState doesn't agree with. Using the competing Redis provider, or the MS Universal Provider we avoid the NRE, but we get corruptions or Out of Memory exceptions at specific points in the app (where we aren't doing something horrendous to the session as far as we can tell), which is weird. As soon as scheduling permits I will endeavour to provide a repro web. - hrillo666
@hrillo666, if "the Russian Redis Session State provider" is from RedisAspNetProviders and you have any problems with it please fill a bug to github.com/alex-simonov/RedisAspNetProviders/issues - Alexander Simonov
@AlexanderSimonov, yes it was, but as you can see below, it behaved as any other SessionstateProvider did, so I have no issue with it. The framework around ASP.NET Session State providers made it rather difficult to find the exact error I had made. - hrillo666

1 Answers

3
votes

The problem is that I managed to use Null as a key(!!!). What I thought was a properly named constant (like all the other key names) was in fact a variable that was null.

The effects of this mistake differed considerably, so I will post them here if it helps anybody:

Inserting Null as a key caused both the ASP.NET Universal Providers session state provider and RedisAspNetProviders SessionStateProvider to work until the first deserialization after the fatal value with a null key is inserted, after which they blow up with a HttpException (which is correct, probably, although a slightly more helpful error message could be supplied) or an Out Of Memory exception, when I tried storing a List, which is unexpected.

The Microsoft Redis Session State provider blows up with an NRE directly as the idiotic key is inserted and again, us idiots could probably use a better message to save us time.

I tried to use Azure AppFabric as an alternative before I knew what the error was but it was not responding enough to be tested.