Andy
Andy

Reputation: 646

Runtime Error using Redis Cache Session State Provider

So I have an MVC application in which I am trying to use Azure's Redis Cache for my Session State Provider. With everything coded and configured and all, when I publish it, the index page loads fine. The only button to hit is 'Next', which is supposed to add a session state variable with a value, and then move on to the appropriate page. But when I click 'Next' I get a runtime error. If I simply comment out the sessionState block in Web.config and publish it like that, I can move on to the 'next' page just fine. So I'm wondering what is wrong with my use of the provider and why it's not working?

Web.Config block:

  <system.web>
    <compilation debug="false" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  <sessionState mode="Custom" customProvider="MySessionStateStore">
      <providers>
          <add 
            type="Microsoft.Web.Redis.RedisSessionStateProvider"
            name="MySessionStateStore" 
            host = "[Host name from Azure]"
            port = "[Port # from Azure]"
            accessKey = "[Key from Azure]"
            ssl = "false"
            throwOnError = "true"
            retryTimeoutInMilliseconds = "5000"
            databaseId = "0"
            applicationName = ""
            connectionTimeoutInMilliseconds = "5000"
            operationTimeoutInMilliseconds = "1000"
          />
      </providers>
    </sessionState>
  </system.web>

POST function when I hit the 'Next' button:

<HttpPost()>
<ValidateAntiForgeryToken()>
Async Function Index(ByVal obj As Type) As Task(Of ActionResult)
    If ModelState.IsValid Then
        Session("VarName") = obj
        Return RedirectToAction("nextPage", "[controller]")
    End If
    Return View()
End Function

Note I am not using any cookies, nor am I trying to use the Redis Cache for anything else. The non-SSL port IS Enabled in Azure (yes, bad, I know - that will change).

I hope that's enough to go on to be able to help - if not, let me know. Thank you!

Upvotes: 0

Views: 1833

Answers (1)

Andy
Andy

Reputation: 646

OK, well, figures that after posting this I'd find the answer!

So there is this very small note at the bottom of one of the articles I could find on Redis Session State Provider: https://msdn.microsoft.com/en-us/library/azure/dn690522.aspx

"Note that data stored in the cache must be serializable, unlike the data that can be stored in the default in-memory ASP.NET Session State Provider. When the Session State Provider for Redis is used, be sure that the data types that are being stored in session state are serializable."

The Type that I was trying to put into the session variable was a custom type. I had to add the "Serializable" attribute to my class! Once I published it as serializable, then voila!

Upvotes: 0

Related Questions