读取AppFabric内容会引发程序集异常

时间:2013-06-19 14:39:04

标签: c# appfabric appfabric-cache

我在AppFabric中使用了两个命名缓存:

  1. CacheA只是[string,string]对,并没有以任何特殊方式使用
  2. CacheB是[string,object]对(其中object是可序列化类型),并用作ASP.NET MVC4站点的Session存储
  3. 我有一个单独的VS Project,它试图枚举缓存的全部内容并输出每个项目:

    foreach (string regionName in cache.GetSystemRegions()) {
        foreach (var cacheItem in cache.GetObjectsInRegion(regionName)) {
            Trace.WriteLine(String.Format("{0} \t\t {1}", cacheItem.Key, cacheItem.Value));
        }
    }
    

    这适用于CacheA,但使用CacheB运行会引发异常(当cache.GetObjectsInRegion()返回非空的内容时):

    Assembly 'Microsoft.Web.DistributedCache, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not found. ---> System.Runtime.Serialization.SerializationException: Assembly 'Microsoft.Web.DistributedCache, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not found.
       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, Object context, IEnumerable`1 knownTypes)
       at Microsoft.ApplicationServer.Caching.Utility.Deserialize(Byte[][] buffers, Boolean checkTypeToLoad, Object context)
       at Microsoft.ApplicationServer.Caching.Utility.Deserialize(Byte[][] buffers, Boolean checkTypeToLoad)
       at Microsoft.ApplicationServer.Caching.CacheEnumerator.MoveNext()
    

    我看到unable to read azure cached data from a different application并添加了对我的其他项目的引用,该项目包含序列化之前这些对象的类(我实际上只想查看值,因为它们被序列化并存储在缓存中),但是一个人没有改变什么。

1 个答案:

答案 0 :(得分:3)

会话项目使用Microsoft.Web.DistributedCache.SerializableSessionStateStoreData存储在AppFabric Cache中。

这是反映的代码

[DataContract]
internal sealed class SerializableSessionStateStoreData
{
    [DataMember(Name = "Keys")]
    private string[] _keys;
    [DataMember(Name = "Values")]
    private object[] _values;
    [DataMember(Name = "Timeout")]
    private int _timeout;
    [DataMember(Name = "SessionStateActions")]
    private SessionStateActions _sessionStateActions;
    public SerializableSessionStateStoreData(SessionStateStoreData storeData, SessionStateActions sessionStateActions)
    {
        ...
    }
    public Tuple<SessionStateStoreData, SessionStateActions> Restore()
    {
        ...
    }
}

当您尝试在Web上下文之外获取会话项时,AppFabric缓存客户端无法反序列化数据项,因为它是以未知类型(在Microsoft.Web.DistributedCache.dll中定义)序列化的。

添加对 Microsoft.Web.DistributedCache.dll

的引用

这也需要System.Web.dll,因为它需要System.Web.SessionState.SessionStateStoreData