2014-12-02 37 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