2010-11-22 25 views
4

我有它运行在ASP.NET 3.5,NHibernate的2.2和Sprint .NET依赖注入的站点。在我们的测试服务器上发生了一个相当奇怪的错误,并且几乎每次都有多个用户在线。问题发生后,每个用户和每个请求都会显示此错误 - 直到您执行IISRESET。然后它再次都可以。奇怪的错误:[ArgumentOutOfRangeException:“计数”必须为非负

这里的例外:

'count' must be non-negative. 
Parameter name: count 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: 'count' must be non-negative. 
Parameter name: count 

Source Error: 
[No relevant source lines] 

Source File: c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET  Files\root\4bf9aa39\6dcf5fc6\App_Web_z9ifuy6t.6.cs Line: 0 

Stack Trace: 
[ArgumentOutOfRangeException: 'count' must be non-negative. 
Parameter name: count] 
System.String.CtorCharCount(Char c, Int32 count) +10082288 
Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) +3612 
Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) +75 
Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) +365 
Spring.Context.Support.AbstractApplicationContext.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) +136 
Spring.Context.Support.AbstractApplicationContext.GetObjectsOfType(Type type) +66 


[ActivationException: Activation error occured while trying to get instance of type InfoTextService, key ""] 
    Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in  c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:57 
Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance() in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:90 
OurProjectsNamespace.Infrastructure.ObjectLocator.LocateService() +86 

回答

3

这的确是一个非常奇怪的错误。当你看到的源AbstractObjectFactory.GetObjectInternal,你会看到如下结构:

[ThreadStatic] 
private int nestingCount; 

protected object GetObjectInternal(...) 
{ 
    const int INDENT = 3; 
    bool hasErrors = false; 
    try 
    { 
     nestingCount++; 

     if (log.IsDebugEnabled) 
     { 
      log.Debug("msg" + 
       new String(' ', nestingCount * INDENT)); 
     } 

     // More code: Calls self recursively. 
    } 
    catch 
    { 
     nestingCount--; 
     hasErrors = true; 

     if (log.IsErrorEnabled) 
     { 
      log.Error("msg" + 
       new String(' ', nestingCount * INDENT)); 
     }   
    } 
    finally 
    { 
     if (!hasErrors) 
     { 
      nestingCount--; 
      if (log.IsDebugEnabled) 
      { 
       log.Debug("msg" + 
        new String(' ', nestingCount * INDENT)); 
      }   
     } 
    } 
} 

你看到必须由三个new String(' ', nestingCount * INDENT)调用一个抛出的异常。当提供的值为负时,该特定的构造函数调用会抛出。因为INDENT是常量,所以在这种情况下,nestingCount必须具有负值。 nestingCount是一个线程静态变量。线程静态变量总是以它们的默认值(在这种情况下为0)进行初始化,并且不会受到其他线程的影响。此外,nestingCount永远不会在此方法之外使用。

因为nestingCount是线程静态的,只能在该方法中使用,所以很难想象nestingCount可能会变为负值。也许在异步(ThreadAbort)异常的情况下,但即使如此,我也很难想象。其他选项是线程静态变量由其他人使用反射更改。

但最大的问题是:如何解决这个问题?

解决方案:

这里只有一件事我能想到的,那就是在调试信息没有登录的方式重新配置log4net的。当不允许调试信息时,构造函数可能永远不会再被调用,这将隐藏问题。不是很漂亮,但可能有效。这可能会实现,因为使用log变量如下初始化AbstractObjectFactory日志:

this.log = LogManager.GetLogger(this.GetType()); 

您可以禁止全局的调试信息在log4net的写作做,或者是 - 当你觉得这是overkill-配置log4net以禁用Spring.Objects.Factory.Support.DefaultListableObjectFactory类型的调试信息(实际上会导致异常的实例)。

祝你好运。

+0

为Spring.Objects.Factory.Support.DefaultListableObjectFactory禁用调试是一种可行的方法。我会试试看看这个错误会再次发生。错误绝对是奇怪的,但很有趣。希望基本原因将仅限于这个错误......谢谢史蒂文!如果有新消息出现,我会尽快更新帖子。 – Mattias 2010-11-23 10:34:33

0

我看到这个错误发生在一个数据库列映射到多个属性。一个典型的例子是外键列映射到一个属性和集合。配置文件中的第二对或第三对眼睛有助于发现这些。与此

一个转折是,它发生于所有用户。你有一个存储在应用程序状态的持久对象吗?

+0

由于机密性,我无法发布所有代码,但找到的对象是一个带有ISession和ExceptionHandler作为构造函数参数的内部对象。当该对象被初始化时会出现错误。 – Mattias 2010-11-23 10:16:17

0

在我的情况下,这个错误发生在性能测试的一段时间之后。它开始很好,一段时间后这个错误弹出。

原来它是由一个完全无关[ThreadLocal的]变量我在代码中使用引起的。我用方法参数替换它,现在它工作正常。

相关问题