首页 文章

ArrayTypeMismatch Session 中的异常

提问于
浏览
1

我有一个用 ASP.NET MVC 编写的网站。有一些自定义授权逻辑,并添加了以下属性。

public sealed class CustomAuthorizeAttribute : AuthorizeAttribute

在 AuthorizeCore 方法的覆盖中,我尝试访问 Session 和:

  • HttpContext.Current.Session!= null

  • HttpContext.Current.Session.IsNewSession == false

  • 但是 HttpContext.Current.Session.Keys 会抛出一个 ArrayTypeMismatch 异常。另外 HttpContext.Current.Session [3]抛出相同的异常。结果我无法从会话中获取值。

更多信息:模式:SQL 服务器

<sessionState allowCustomSqlDatabase="true" cookieless="UseCookies" mode="SQLServer" sqlConnectionString="Server=.;Database=ASPState;integrated security=true;" timeout="120" sqlCommandTimeout="240" />

堆栈跟踪:

at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseArrayMember(ParseRecord pr)at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseMember(ParseRecord pr)at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)at System.Runtime.Serialization.Formatters.Binary._BinaryParser.ReadMemberReference() at System.Runtime.Serialization.Formatters.Binary._BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler,_BinaryParser serParser,Boolean fCheck,Boolean isCrossAppDomain,IMethodCallMessage methodCallMessage)at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream,HeaderHandler handler,Boolean fCheck,Boolean isCrossAppDomain,IMethodCallMessage methodCallMessage)at System.Web.Util.AltSerialization.ReadValueFromStream(BinaryReader 读者)在 System.Web.SessionState.SessionStateItemCollection.ReadValueFromStreamWithAssert() at System.Web.SessionState.SessionStateItemCollection.DeserializeItem(String name,Boolean check)at System.Web.SessionState.SessionStateItemCollection.get_Item(String name)at System.Web.SessionState.HttpSessionStateContainer.get_Item(String name)at System.Web.SessionState.HttpSessionState.get_Item(String name)at {。 6} .SessionHelper.get_UserSessionContext() in d:{。 7 {\ 16 41 at {。 8:.CustomAuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext)在 d:{。在 System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext controllerContext 上的 System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext,IList`1 过滤器,ActionDescriptor actionDescriptor)System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass25.b_1e(AsyncCallback asyncCallback,Object asyncState)

任何人都可以解释为什么会出现这种错误,我该如何解决它以及解决这个问题的可能方法?

1 回答

  • 2

    这听起来像 type-based 序列化程序(BinaryFormatterNetDataContractSerializer等)的 all-too-common 问题与构建之间的不兼容类型一起使用。在一般情况下,这个 kinda-sorta 足够工作,它通常允许你在构建之间使用相同的数据(因为你通常不会更改涉及的类型),但是:当动态类型生成时使用时会出现一个大问题广泛地存在于 Entity Framework 和 NHibernate 等工具中。本质上,因为它们在运行时是子类,所以几乎可以保证您永远无法在重建之间进行反序列化,甚至可能在集群中的不同节点之间进行反序列化。

    这里有两条指导:

    • 永远不会将您的标准域实体存储在会话(或缓存)中:拥有专用的 DTO(i.e.一种表示您要存储的数据的简单类型)并存储它。如果可能,使此类型不可变(特别是为了避免 same-object vs clone 的问题,这在提供者之间发生变化)

    • 如果可能的话,控制序列化,使其不依赖于类型定义:“基于合同”的序列化器,如 XmlSerializer,Json.NET 或 protobuf-net 都允许您使用 pain-free 处理会话(或缓存)层的 CLOB 或 BLOB 数据 serialization/deserialization 即使您在程序集之间移动类型并重命名它们

相关问题