c#-4.0 - Azure 缓存对象的类型转换始终为 null

标签 c#-4.0 azure collections entity-framework-5 azure-caching

这很奇怪,因为无法转换 DataCacheItem列出集合。

我正在从数据库中提取值并将其存储到缓存中,但如果我要求缓存使用 TypeCast 返回数据,那么它会拒绝这样做。

Cannot cast 'isValueInCache' (which has an actual type of     
'Microsoft.ApplicationServer.Caching.DataCacheItem') to      
'System.Collections.Generic.List<MMD.Data.HumanGender>'


//Setting Value in Cache Object
var isValueInCache = BaseCache.Instance().RetrieveCacheValue("allGenders");

//TypeCasting of Data
var isSeariled = isValueInCache == null ? 
Newtonsoft.Json.JsonConvert.DeserializeObject(proxy.GetAllGenders(), 
typeof(List<HumanGender>)) as List<HumanGender>
: isValueInCache as List<HumanGender>;

我还没有找到为什么它无法转换 ObjectList<T> 。我似乎只能在这里工作,将对象转换到 JSON格式,按键拉动并创建 List Object

Update 1:

这种情况不仅涉及列表,还涉及从缓存检索的任何实体对象。

Update 2:

也不使用 DTO。

仍在寻找解决办法。

最佳答案

您不应该使用数据缓存项的 value 属性吗?

目前不在 Visual Studio 后面,所以我在没有编译器的情况下编写此内容。

var isValueInCache = BaseCache.Instance().RetrieveCacheValue("allGenders") as DataCacheItem;

//TypeCasting of Data
var isSeariled = isValueInCache == null ? 
Newtonsoft.Json.JsonConvert.DeserializeObject(proxy.GetAllGenders(), 
typeof(List<HumanGender>)) as List<HumanGender>
: isValueInCache.Value as List<HumanGender>;

编辑:

我认为 BaseCache 的 RetrieveCacheValue 方法返回了错误的类型。

关于c#-4.0 - Azure 缓存对象的类型转换始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832153/

相关文章:

Java Commons Collections removeAll

c# - 如何根据 C# 中的条件组合两个具有不同属性的列表

c# - 在 C# 中读取文本文件

azure - 无法从 Azure API 检索 token

java - 嵌套的 Java 集合泛型

c# - TDD - 为迭代/使用集合的方法编写测试

linq-to-sql - linq to sql 查询的迭代非常慢

c#-4.0 - Nuget Moq 4.0.10827 和 InternalsVisibleTo(再次)

azure - 如何解决 azure 发布的 Blazor 应用程序上的 XMLHttpRequest 错误

angular - 如何在 Azure AD 上注册单租户 SPA?