什么是由ASP.net HttpRuntime.Cache使用默认的序列化 [英] What is the default serialization used by the ASP.net HttpRuntime.Cache

查看:194
本文介绍了什么是由ASP.net HttpRuntime.Cache使用默认的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道如果任何人知道什么明确的ASP.net HttpRuntime.Cache使用的默认的序列化是什么?它是二进制,XML,别的东西?

我问,因为我有我在哪里填充与相同的自定义类型的多个对象泛型列表的情况。自定义类型是POCO,没有什么特别的地方。其所有属性都与公众获得{;组; },它是公开的,它没有继承,它没有任何接口。事实上它比我们的缓存这没有问题,工作等诸多复杂的对象要少得多。我已尝试添加[Serializable]属性的自定义类,它有没有影响。

我的列表添加到一个独特的密钥缓存。作为填充之前被插入到高速缓存中的列表已被验证,如填充以及列表中的对象已被验证。但是,当名单被拉回从缓存中它是一个空列表(NOT NULL),它只是在它没有任何项目。这意味着该清单被添加到高速缓存,并可回收但由于某些原因缓存具有序列列表中的对象的问题。

我刚刚发现这个奇特怪异,因为我有一个更为复杂(包括继承,接口,并且还含有其他复杂对象的泛型列表属性)和这些列表的缓存工作没有自定义对象的另一个列表问题。

无论是工作和非工作清单正在使用C#类管理其消耗的缓存数据ASP.net用户控制之外。这两个缓存处理类调用完全相同的高速缓存管理器类单一实例它包装了HttpRuntime.Cache提供拉类型的方法,推动对象到缓存中。

任何人有任何想法可能导致这种情况发生。我可以唯一薄的是Document对象的'Blurb的'属性都可能包含HTML,但如果ASP.net使用二进制序列化缓存我不认为这会怎么做任何事情。

下面是类

 公共类文档
{
    公共字符串ContentTypeId {搞定;组; }
    公共字符串内容识别{搞定;组; }
    公共BOOL IsCustom {搞定;组; }
    公共语言DocLanguage {搞定;组; }
    公共字符串RegularTitle {搞定;组; }
    公共字符串InvertedTitle {搞定;组; }
    公共字符串的Blurb {搞定;组; }
}

下面是在语言属性使用的子类

 公共类语言
{
    公共字符串名称{;组; }
    公共字符串code {搞定;组; }
}


解决方案

关于此缓存异常正在发生的环境更详细一点。

我有一个ASP.net用户控制其重新presents选项卡式容器在页面上。此控件包含由用户选择的主题列表。每个主题的用户已经选择了这个控件创建包含与该主题相关的文档该主题一个新的标签。

控制通过利用ASP​​.net创建新的选项卡提供LoadControl方法。然后,它分配新建选项卡控件从列表中的主题。每个标签控件知道如何定位其分配话题的文档。

这就是缓存在执行此选项卡控制范围之内。用于缓存的文件列表中的缓存键是完全独立的网站+用户观看的话题主题+性别+年龄。这使得该文件列出了由所有用户进行高速缓存并在该网站中检索恰当的标准。

好时的文件列表传递给他们由普通的旧对象引用(即文件清单= _documents)通过缓存。但是从缓存中拉出时,列表是空的。

虽然每个标签控件是它自己相同的用户控件的实例,并在标签控件中使用的所有变量都是私有到控制和应具体到该控件。我打我的绳子的末端,尽管是没有办法的各个选项卡可以在书写每个人的私人名单,我决定必须有某种疯狂的引用错误发生的情况,如果这是我需要的情况下,停止使用在高速缓存中引用的对象,只有彻底发送缓存列表我试图店的新副本。

我然后用于名单和LT以下扩展方法>克隆每个列表,因为它们被传递到缓存中。这使得它,以便传递到缓存中的所有项目均与内存到内存自己独特的引用自己的空间全新的对象。

固定它。缓存现在的作品。现在正在恰好返回的清单,因为它们被添加到高速缓存。我不知道为什么会有所作为,如果任何人有任何想法,我很愿意听到他们的声音。

  ///<总结>
    ///克隆指定列表克隆。
    ///< /总结>
    ///< typeparam NAME =T>< / typeparam>
    ///< PARAM NAME =listToClone方式>克隆清单< /参数>
    ///<&回报GT;< /回报>
    公共静态的IList< T>克隆< T>(这个IList的< T> listToClone)其中T:ICloneable
    {
        返回listToClone.Select(项目= GT;(T)的item.Clone())。了ToList();
    }

Just wondering if anyone knows definitively what the default serialization used by the ASP.net HttpRuntime.Cache is? Is it Binary, XML, something else?

I ask because I have a situation where I am populating a generic List with multiple objects of the same custom type. The custom type is a POCO, there is nothing special about it. All of its properties are public with { get; set; }, it is public, it has no inheritance, it has no interfaces. In fact it is much less complicated than many other objects which we are caching which work without issue. I have tried adding the [Serializable] attribute to the custom class and it has no effect.

I add the list to the cache with a unique key. The list has been verified as populated before it is inserted into the cache, the objects in the list have been verified as populated as well. But when the list is pulled back out of the cache it is an empty list (NOT NULL), it simply has no items in it. This means that the list is being added to the cache and is retrievable but for some reason the cache is having issues serializing the objects in the list.

I just find this freaky weird since I have another list of custom objects which are much more complicated (consisting of Inheritance, Interfaces, and also containing Properties which are generic lists of other complex objects) and the caching of those lists work without issue.

Both the working and non-working list are being managed in C# classes outside of the ASP.net user controls which consume the cached data. Both of these cache handling classes call the exact same Cache Manager class singleton instance which wraps the HttpRuntime.Cache to provide typed methods for pull and pushing objects into the cache.

Anyone have any ideas what could cause this to happen. The only thing I can thin of is that the 'Blurb' property of the Document object can potentially contain HTML, but if ASP.net uses binary serialization for the cache I don't see how this would do anything.

Here is the class

public class Document
{
    public string ContentTypeId { get; set; }
    public string ContentId { get; set; }
    public bool IsCustom { get; set; }
    public Language DocLanguage { get; set; }
    public string RegularTitle { get; set; }
    public string InvertedTitle { get; set; }
    public string Blurb { get; set; }
}  

Here is the subclass used in the Language Property

public class Language
{
    public string Name { get; set; }
    public string Code { get; set; }
}

解决方案

A little more detail regarding the environment in which this caching anomaly is taking place.

I have an ASP.net User Control which represents a Tabbed Container on the page. This control contains a list of topics selected by the user. For each topic the user has selected this control creates a new tab for that topic containing documents related to that topic.

The control creates the new tab by utilizing the ASP.net provided LoadControl method. It then assigns the newly created Tab Control a topic from its list. Each tab control knows how to locate documents for its assigned topic.

It is within this tab control where the caching was implemented. The cache key used to cache the lists of documents are completely unique to the Site+Topic+Gender+Age of the user viewing the topic. This allows the document lists to be cached and retrieved across the site by all users fitting that criteria.

Well when the lists of documents were passed to the cache they were passed by regular old object references (i.e. List documents = _documents). but when pulled from the cache the list were empty.

Although each tab control is its own instance of the same user control and all variables used within the tab control are private to that control and should be specific to that control. I hit the end of my rope and despite the fact that there is no way the individual tabs could be over-writing each others private lists I decided there had to be some sort of crazy reference bug going on and if that was the case I needed to stop using the referenced objects in the cache and only send the cache completely new copies of the lists I was trying to store.

I then used the following extension method for the List<> and cloned each list as they were passed to the cache. This made it so that all items passed to the cache were brand new objects with their own space in memory with their own unique references to that memory.

FIXED IT. THE CACHING NOW WORKS. The lists are now being returned exactly as they were added to the cache. I have no clue why this would make a difference, if anyone has any ideas I would love to hear them.

    /// <summary>
    /// Clones the specified list to clone.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="listToClone">The list to clone.</param>
    /// <returns></returns>
    public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }

这篇关于什么是由ASP.net HttpRuntime.Cache使用默认的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆