ASP.NET缓存始终返回null

ASP.NET缓存始终返回null,asp.net,sqlcachedependency,Asp.net,Sqlcachedependency,我正在我的ASP.NET应用程序中使用SQLCacheDependency,并带有查询通知。 我成功地设置了我的数据库。但是,每当我试图将数据存储在缓存对象中时,它就是不保存值。它总是空。我没有收到任何错误或异常。 这是我的密码 Global.asax void Application_Start(object sender, EventArgs e) { // Code that runs on application startup System.Da

我正在我的
ASP.NET
应用程序中使用
SQLCacheDependency
,并带有
查询通知
。 我成功地设置了我的数据库。但是,每当我试图将数据存储在
缓存
对象中时,它就是不保存值。它总是
。我没有收到任何错误或异常。 这是我的密码

Global.asax

void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        System.Data.SqlClient.SqlDependency.
        Start(ConfigurationManager.ConnectionStrings["McdConn"].ToString());

    }

void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown
        System.Data.SqlClient.SqlDependency.
        Stop(ConfigurationManager.ConnectionStrings["McdConn"].ToString());
    }

public static class CacheManagement
{
    public static DataTable CreateCache(string cacheName, string tableName, string query)
    {
        DataTable dtResult = new DataTable();
        try
        {
            string connectionString = ConfigurationManager.ConnectionStrings["McdConn"].ToString();
            dtResult = HttpContext.Current.Cache[cacheName] as DataTable;

            if (dtResult == null)
            {
                dtResult = new DataTable();
                using (var cn = new SqlConnection(connectionString))
                {
                    cn.Open();
                    var cmd = new SqlCommand(query, cn);
                    cmd.Notification = null;
                    cmd.NotificationAutoEnlist = true;
                    SqlCacheDependencyAdmin.EnableNotifications(connectionString);
                    if (!SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connectionString).Contains(tableName))
                    {
                        SqlCacheDependencyAdmin.EnableTableForNotifications(connectionString,tableName);
                    }

                    var dependency = new SqlCacheDependency(cmd);
                    //SqlDataAdapter ad = new SqlDataAdapter(cmd);
                    //ad.Fill(dsResult);
                    SqlDataReader reader = cmd.ExecuteReader();
                    dtResult.Load(reader);
                    HttpContext.Current.Cache.Insert(cacheName, dtResult, dependency);

                }
            }
        }
        catch (Exception ex)
        {
            Exception_Log.ExceptionMethod("Web", "CacheManagement.cs", "CacheManagement", ex);
        }

        return dtResult = HttpContext.Current.Cache[cacheName] as DataTable;
    }

}
代码隐藏

var dtCachedCategories = HttpContext.Current.Cache["tbl_CategoryMaster_Cached"] as DataTable;
if (dtCachedCategories == null)
{
 dtCachedCategories = CacheManagement.CreateCache("tbl_CategoryMaster_Cached","dbo.tbl_CategoryMaster_Languages", "Select * from dbo.tbl_CategoryMaster_Languages");

}
上述参数始终返回
null


有谁能帮我指出可能缺少的内容吗?

好的,您可以做很多事情来调试代码并得出结论。您的缓存项似乎被删除得太频繁了

1.)使用
CacheItemPriority.NotRemovable
缓存.Insert()以确保ASP.NET不会删除 您的物品,无论何时感觉如此。使用说明的
Insert()
方法。检查这个 文章也是

2.)要找出删除缓存项的原因,请使用
CacheItemRemovedCallback
Cache.Insert()方法的委托选项。请检查此版本和此版本

3.)确保您的
dtresult
以及
reader
不为空。检查线路:

SqlDataReader=cmd.ExecuteReader()&
dtResult.Load(读卡器),以及您的日志

4.)检查应用程序池回收时间。这包含与应用程序池设置(IIS 7+)相关的所有内容

5.)此链接为IIS 6的应用程序池提供了解决方案:

另外,尝试使用这个方法,看看它是否有效

System.Web.HttpRuntime.Cache.Insert(cacheName, dtResult, dependency);

你能提供更多关于问题跟踪的信息吗?执行插入时dtResult是否为空?在没有查询通知的情况下尝试时,它是否会更改任何内容?使用原子类型值?@jbl从缓存中检索时,
dtResult
始终为空。我认为缓存从不保存
dtResult