我无法理解这个错误的底部,因为它只发生在一个实例中,我找不到任何可能导致错误的代码。
我有一个3.5 Web服务,我从一个多线程的CAB客户端调用。我有一堆针对Web服务的单元测试(来自3.5和2.0代码),它工作正常。但是,在实际应用中,它在90%的时间内不起作用,而剩余的10%的时间,它决定工作。
代码:
Friend Function ExecuteSearch(ByVal query As String) As List(Of SomeObject)
Dim searchResults As List(of Object) = _searcher.UserSearch(query)
Return searchResults
End Function
// In Searcher
Public Function UserSearch(ByVal query As String) As List(Of SomeObject)
Return Translate(Search.GetResults(query))
End Function
// In Search
Public Function GetResults(ByVal query As String) As List(Of SomeObject)
Dim service As New FinderService.FinderService()
Dim results As New List(Of String)
Dim serviceResults As IEnumerable(Of String) = service.Search(query) // <-- ERRORS OUT HERE
results.AddRange(serviceResults)
Return results
End Function
// In the service
Public Function Search(ByVal query As String) As IEnumerable(Of String)
Initialize() // Initializes the _accounts variable
Dim results As New List(of String)
For Each account As User In _accounts
If a bunch of conditions Then
results.Add(account.Name)
End IF
End For
Return results
End Function
断点击中这些代码(按此顺序)。出错的行在“GetResults”方法中。
任何帮助都将不胜感激。
答案 0 :(得分:7)
显然_accounts在循环期间被修改。你可以通过
来缓解它For Each account As User In _accounts.ToList()
因此创建并枚举了当前_accounts的副本,而不是可能更改的实际集合