2010-11-22 24 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的]變量我在代碼中使用引起的。我用方法參數替換它,現在它工作正常。

相關問題