【问题标题】:WCF Caching List ErrorWCF 缓存列表错误
【发布时间】:2012-06-15 11:02:45
【问题描述】:

我正在尝试缓存从 WCF 中的 SQL Server 检索到的动态列表,到目前为止,检索过程运行良好,并且我已经对其进行了测试,并且完全没有问题,问题是当我尝试时为了缓存这个检索到的列表,发生了一个错误,我无法找出它背后的原因。

这是我的方法:

public List<ErrorEntities> GetALL()
{
            List<ErrorEntities> res = null;
            if (HttpContext.Current.Cache["GetALL"] == null)
            {
                SqlCommand com = Global.GetCommand("select * from [BlockingList]");
                com.Connection.Open();
                SqlDataReader reader = com.ExecuteReader();
                res = Fill(reader);

                HttpContext.Current.Cache.Add("GetALL", res, null, DateTime.Now.Add(new TimeSpan(0, 0, 15)), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

                com.Connection.Close();
                return res;
            }
            else
            {
                res = HttpRuntime.Cache["GetALL"] as List<ErrorEntities>;
                return res;
            }
}

我尝试通过添加以下代码行来启用 web.config 文件的缓存,但问题也没有解决:

<scriptResourceHandler enableCompression="true"    enableCaching="true" />

这是我在编译解决方案时遇到的错误:

由于内部错误,服务器无法处理请求。有关错误的更多信息,请打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或从配置行为)在服务器上 命令将异常信息发送回客户端,或打开 根据 Microsoft .NET Framework 3.0 SDK 文档和 检查服务器跟踪日志。

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more

有关错误及其在代码中的位置的信息。

Exception Details: System.ServiceModel.FaultException: The server was unable to process the request due to an internal error.  For more

有关错误的信息,请打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或从配置行为)在服务器上 命令将异常信息发送回客户端,或打开 根据 Microsoft .NET Framework 3.0 SDK 文档和 检查服务器跟踪日志。

Source Error: 


Line 113:        
Line 114:        public ServiceReference1.ErrorEntities[] GetALL() {
Line 115:            return base.Channel.GetALL();
Line 116:        }
Line 117:

【问题讨论】:

    标签: c# asp.net wcf list


    【解决方案1】:

    我假设有问题的代码是在 WCF 方法中调用的。

    WCF 在其自己的上下文中工作,不应在此处使用HttpContext.Current

    Microsoft 在 .NET 4.0 中添加了没有 System.Web 依赖项的新缓存。

    MemoryCache 在 WCF 中应该可以正常工作。

    尝试使用此代码获取缓存对象:

    ObjectCache cache = MemoryCache.Default;
    

    http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

    我还建议将usingtry-finally 块用于一次性资源(代码示例中的Connection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-13
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多