1

Appfabric DataCache 有一个奇怪的问题。有时,也许千分之一,我们从缓存 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)
4

1 回答 1

0

最终追踪到了这个。

如果您更改类型的结构,当同一类型的缓存版本在缓存中时,可能会发生此问题,然后尝试检索旧类型的缓存实例,并使用新类型。

在多个开发人员处理相同类型、共享相同全局缓存的环境中,您一定会遇到这种情况。

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

例子

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.
于 2012-06-26T16:38:00.447 回答