我有一个ASP.Net应用程序,经过长时间的运行后会开始抛出NUllReferenceExceptions。有问题的代码在每个单独的会话中尽早使用,我们正在尝试建立某种引用者信息。问题是,我无法理解什么可以抛出这个异常。
有问题的方法(堆栈跟踪中最顶层)是:
Private Function ResolveReferrer(ByVal wrRequest As HttpRequest) As Referral
'1) If we don't find a domain, try and get a match on any query strings
If wrRequest.QueryString.Count > 0 Then
For Each item As Referral In Me
For Each sKey As String In wrRequest.QueryString.Keys
If Not sKey Is Nothing AndAlso item.Names.Contains(sKey.ToLower) Then
Return item
End If
Next sKey
Next item
End If
Dim strSubDomain As String = Utility.RequestSubDomain(wrRequest.Url)
'2) If we don't find one on the domain, see if we can find the domain in query string
If Not wrRequest.QueryString.Item("domain") Is Nothing Then
strSubDomain = wrRequest.QueryString.Item("domain")
strSubDomain = HttpUtility.UrlDecode(strSubDomain)
' OK found a "domain" query string, so make up a referrer object to return
' ... just use the domain we've found for all the parameters
Dim oRef As New Referral(strSubDomain, strSubDomain, strSubDomain)
Return oref
End If
'3) If no query string of "domain", then see if the referring field is presented by the browser
If Not wrRequest.UrlReferrer Is Nothing Then
Dim sURL As String = wrRequest.UrlReferrer.ToString
strSubDomain = Utility.RequestSubDomain(wrRequest.UrlReferrer)
Dim oRef As New Referral(sURL, sURL, strSubDomain)
Return oRef
End If
'4) See if we can find the domain defined in the web.config
For Each item As Referral In Me
' See if we can find a referrer from the domain name
If String.Compare(strSubDomain, item.FromDomain, False) = 0 Then
Return item
End If
Next item
'5) If we still can't find one, make one up with a value of "Unknown"
Return New Referral("Unknown", "Unknown", "Unknown", "Unknown")
End Function
这是继承自ArrayList的一部分的类。我已经检查了,添加到这个ArrayList的唯一东西是Referral类的实例(它有多个构造函数,都很简单)。
我们所知道的是,我们可以拥有一个没有引用者信息的请求,并且它会导致异常被抛出。同时,如果带有引荐来源的请求进入,它可以正常工作。在任何情况下都不会在查询字符串中传递任何内容(因此我认为您可以跳到'3评论。
所以,我的问题是,此方法中的哪些内容会导致抛出NullReferenceException?如果您需要添加其他代码段或类定义,只需喊出来。
Utility.RequestSubDomain具有合理的复杂性,因此我怀疑它是否被内联并从堆栈跟踪中删除。最重要的是:
Public Shared Function RequestSubDomain(ByVal uri As System.Uri) As String
If uri Is Nothing Then
Return ""
End If
任何帮助或寻找更多信息的建议都将受到赞赏。显然 <(如同很多问题一样)只发生在生产中,所以我不想打开调试。
答案 0 :(得分:2)
我认真地看了一眼,看起来最有可能出现的两件事是:
wrRequest
可以为null。ArrayList
中可能包含空值,从而导致枚举数返回空值。如果是我,我会首先关注这一部分。是否有可能Me.GetEnumerator
将返回一个枚举器,其中一个项具有空值?
For Each item As Referral In Me
item.Names ' Can item be null here causing the exception on the getter of Names?
Next item
答案 1 :(得分:0)
事实证明,arraylist中有一个NULL - 事实证明,在某些情况下,多个线程正在处理同一个对象(继承自arraylist)并调用Add()。所以null出现了,因为内部索引被不同的线程增加了两次。