2012-06-12 67 views
1

我想緩存從WCF中的SQL Server檢索到的動態列表,檢索過程目前工作正常,並且我已經測試過,並且根本沒有任何問題,問題是當我試圖緩存這個檢索列表時,發生錯誤,我無法找出背後的原因。WCF緩存列表錯誤

這裏是我的方法:

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: 

回答

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代碼示例中)