2014-12-02 38 views
1

使用.NET 3.5 C#,我有一個WCF Web服務爲移動客戶端提供服務。客戶端經常使用該服務將數據發送到服務器並接收更新。該服務部署在IIS 7.5中,並且配置爲每天早晨0600回收。回收通常無暇的工作,客戶像往常一樣繼續使用該服務。但是,有一些事件導致應用程序進入有趣的狀態,並且我可以看到下面的日誌類型初始化錯誤。這幾乎是重疊循環的競爭,其中還沒有成功卸載DLL的過程中喜歡的事發生了:IIS回收事件後.NET類型初始化異常

System.NullReferenceException: Object reference not set to an instance of an object. 
    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 
    at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value) 
    at Docobo.Keswick.DbAccess.TableData.DataClasses.GetInfo(Type type) 

的數據類是使用IQToolkit查找數據庫表名內部靜態類:

internal static class DataClasses 
{ 
    private static readonly Dictionary<Type, DataClassInfo> classInfos = new Dictionary<Type, DataClassInfo>(); 

    public static DataClassInfo GetInfo(Type type) 
    { 
     DataClassInfo info; 
     if (!classInfos.TryGetValue(type, out info)) 
     { 
      // This is not thread-safe, but that's fine. 
      // If this class is generated more than once it doesn't matter. 
      info = new DataClassInfo(type); 
      classInfos[type] = info; 
     } 
     return info; 
    } 
} 

手動回收應用程序池解決了問題。 從堆棧跟蹤看來,靜態只讀字段classInfos可能是NULL,但我不知道這是怎麼回事?

+0

任何機會'類型'可以爲空? – rodrigogq 2014-12-02 14:19:41

+0

不,我不這麼認爲 – user3703978 2014-12-02 14:27:10

回答

4

發生異常的字典裏面,你可以從堆棧跟蹤看到:

System.Collections.Generic.Dictionary`2.Insert 

相當多了可能發生的唯一原因是,如果你同時訪問字典。嘗試將其包裝在lock聲明中。

我猜測它在回收過程中發生的原因如下。讀取字典可能是線程安全的,因此在啓動期間異常的機會較高。在回收期間,可能會暫停多個同時發生的客戶端請求,直到應用程序重新啓動。因此,在應用程序重新啓動後,會同時發生多次寫入嘗試。

+0

似乎是合理的。謝謝 – user3703978 2014-12-02 14:36:44

相關問題