使用 Redis 缓存会话状态提供程序时出现运行时错误
Runtime Error using Redis Cache Session State Provider
所以我有一个 MVC 应用程序,我在其中尝试将 Azure 的 Redis 缓存用于我的会话状态提供程序。一切都编码和配置好了,当我发布它时,索引页面加载正常。唯一要点击的按钮是 'Next',它应该添加一个带有值的会话状态变量,然后移动到适当的页面。但是,当我单击 'Next' 时,出现运行时错误。如果我简单地注释掉 Web.config 中的 sessionState 块并像那样发布它,我就可以继续进入 'next' 页面。所以我想知道我使用提供程序有什么问题,为什么它不起作用?
Web.Config块:
<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>
当我点击 'Next' 按钮时 POST 函数:
<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
请注意,我没有使用任何 cookie,也没有尝试将 Redis 缓存用于其他任何用途。 Azure 中启用了非 SSL 端口(是的,不好,我知道 - 这会改变)。
我希望这足以继续提供帮助 - 如果没有,请告诉我。谢谢!
好的,好吧,我想在发布这个之后我会找到答案!
所以在我能找到的一篇关于 Redis 会话状态提供程序的文章的底部有这个非常小的注释: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."
我试图放入会话变量的类型是自定义类型。我必须将 "Serializable" 属性添加到我的 class!
一旦我将其发布为可序列化,然后瞧!
所以我有一个 MVC 应用程序,我在其中尝试将 Azure 的 Redis 缓存用于我的会话状态提供程序。一切都编码和配置好了,当我发布它时,索引页面加载正常。唯一要点击的按钮是 'Next',它应该添加一个带有值的会话状态变量,然后移动到适当的页面。但是,当我单击 'Next' 时,出现运行时错误。如果我简单地注释掉 Web.config 中的 sessionState 块并像那样发布它,我就可以继续进入 'next' 页面。所以我想知道我使用提供程序有什么问题,为什么它不起作用?
Web.Config块:
<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>
当我点击 'Next' 按钮时 POST 函数:
<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
请注意,我没有使用任何 cookie,也没有尝试将 Redis 缓存用于其他任何用途。 Azure 中启用了非 SSL 端口(是的,不好,我知道 - 这会改变)。
我希望这足以继续提供帮助 - 如果没有,请告诉我。谢谢!
好的,好吧,我想在发布这个之后我会找到答案!
所以在我能找到的一篇关于 Redis 会话状态提供程序的文章的底部有这个非常小的注释: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."
我试图放入会话变量的类型是自定义类型。我必须将 "Serializable" 属性添加到我的 class! 一旦我将其发布为可序列化,然后瞧!