Get的Appfabric DataCache反序列化问题

时间:2011-07-27 10:31:28

标签: appfabric deserialization data-caching

Appfabric DataCache遇到了一个奇怪的问题。有时,1000或者1000,我们从缓存Get方法cache.Get(key);public object Get(string key))中获得异常。

是个例外
  

类型的对象   ' System.Collections.Generic.List`1 [MyNamespace.PersonName]'不可能是   转换为' MyNamespace.StatusType'。

为什么要尝试进行此转换? StatusType类型与PersonName类型无关,它不以任何方式存在于对象图中。

这是DataCache的内部,如上所述,它将运行几天没有问题,然后有时它只会开始抛出这样的异常。对象(和对象图)都很简单。它在99%的时间里都有效。

堆栈跟踪:

at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.RtFieldInfo.InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, Boolean doVisibilityCheck, Boolean doCheckConsistency)
   at System.Runtime.Serialization.FormatterServices.SerializationSetValue(MemberInfo fi, Object target, Object value)
   at System.Runtime.Serialization.FormatterServices.PopulateObjectMembers(Object obj, MemberInfo[] members, Object[] data)
   at Castle.DynamicProxy.Serialization.ProxyObjectReference.DeserializeProxyState()
   at Castle.DynamicProxy.Serialization.ProxyObjectReference..ctor(SerializationInfo info, StreamingContext context)
   at ReadProxyObjectReferenceFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
   at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
   at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserializeInSharedTypeMode(XmlReaderDelegator xmlReader, Int32 declaredTypeID, Type declaredType, String name, String ns)
   at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
   at ReadPersonFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
   at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
   at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserializeInSharedTypeMode(XmlReaderDelegator xmlReader, Int32 declaredTypeID, Type declaredType, String name, String ns)
   at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, String name, String ns)
   at System.Runtime.Serialization.NetDataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
   at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
   at Microsoft.ApplicationServer.Caching.Utility.Deserialize(Byte[][] buffers, Boolean checkTypeToLoad)
   at Microsoft.ApplicationServer.Caching.RoutingClient.SendMsgAndWait(RequestBody reqMsg)
   at Microsoft.ApplicationServer.Caching.DataCache.SendReceive(RequestBody reqMsg)
   at Microsoft.ApplicationServer.Caching.DataCache.InternalGet(String key, DataCacheItemVersion& version, String region)
   at Microsoft.ApplicationServer.Caching.DataCache.Get(String key)
   at MyNamespace.CacheManagement.AppFabricCacheProvider.Get(String key)

1 个答案:

答案 0 :(得分:0)

最终跟踪此事。

如果更改了类型的结构,当相同类型的缓存版本在缓存中,然后尝试检索旧类型的缓存实例,则会出现此问题。 / p>

在多个开发人员处理相同类型的环境中,共享相同的全局缓存,您必然会遇到此问题。

在生产中永远不会发生类型接口是静态的。

示例

public class Foo // version 1.0
{
 public string Woof {get;set;}
 public FooBar Meow {get;set;}
}
// compile, run,
// add an instance of Foo to AppFabric Cache, cacheKey = X

// 5 mins later
public class Foo // version 1.1
{
 public int Id {get;set;} // change interface of type
 public string Woof {get;set;}
 public FooBar Meow {get;set;}
}

// compile, run, get an instance of Foo from the cache, cacheKey = X
// exception, with strange information.