asp.net - 奇怪的错误 : [ArgumentOutOfRangeException: 'count' must be non-negative

标签 asp.net nhibernate orm dependency-injection spring.net

我有一个在 ASP.NET 3.5、NHibernate 2.2 和 Sprint .NET 中运行的站点,用于依赖注入(inject)。在我们的测试服务器上发生了一个相当奇怪的错误,而且几乎每次都有多个用户在线。问题发生后,每个用户和他们提出的每个请求都会显示此错误 - 直到您执行 IISRESET。然后一切都好了。

这是一个异常(exception):

'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

最佳答案

这确实是一个非常奇怪的错误。当您查看 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) 之一引发。来电。那个特别的string当提供的值为负时,构造函数调用抛出。因为INDENT是一个常量,nestingCount在这种情况下必须有一个负值。 nestingCount是一个线程静态变量。线程静态变量始终使用其默认值(在本例中为 0)初始化,并且不受其他线程的影响。此外,nestingCount从未在此方法之外使用。

因为nestingCount是线程静态的并且仅用于该方法,很难想象一个场景是 nestingCount可以得到负面的。也许在异步(ThreadAbort)异常的情况下,但即使这样我也很难想象。其他选择是线程静态变量由其他人使用反射更改。

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

解决方案:

我只能想到一件事,那就是以不记录调试信息的方式重新配置 log4net。当禁止调试信息时,string(char, int)构造函数可能永远不会再次被调用,这将隐藏问题。不是很漂亮,但可能有效。这可能有效,因为 AbstractObjectFactory使用 log 的日志初始化如下的变量:
this.log = LogManager.GetLogger(this.GetType());

您可以通过在 log4net 中全局禁用调试信息的写入来做到这一点,或者 – 当您认为这太过分时 – 通过配置 log4net 来禁用类型 Spring.Objects.Factory.Support.DefaultListableObjectFactory 的调试信息(实际上是导致异常的实例)。

祝你好运。

关于asp.net - 奇怪的错误 : [ArgumentOutOfRangeException: 'count' must be non-negative,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4246747/

相关文章:

具有负载平衡功能的 ASP.NET SQL Server session

c# - nhibernate nuget - 找不到架构

c# - 使用 System.Data.OracleClient 配置 Fluent NHibernate

java - 是否可以在 hibernate 状态下将表视为只读?

php - 两个不同子类扩展同一个实体实例时的 Doctrine 继承策略

c# - 通过 HTTP 处理程序连接到 SQL Server Express 数据库

asp.net - 在事件目录中创建用户

c# - global.asax 中的 Application_OnStart 是否适用于缓存页面?

c# - 如何使用 asp.net mvc3 和 nhibernate 提高 Web 应用程序性能

java - JPA QL 用于选择多对多关系的非拥有方?