DisconnectedContext was detected

M

mr_doles

I am trying to match users from one database with users in AD to get
an e-mail address. The program works but right before it exits I get
the following message.

DisconnectedContext was detected
Message: Context 0x1a04b0' is disconnected. Releasing the interfaces
from the current context (context 0x1a0340).This may cause corruption
or data loss. To avoid this problem, please ensure that all contexts/
apartments stay alive until the application is completely done with
the RuntimeCallableWrappers that represent COM components that
liveinside them.

I have read Microsoft's description of what this error means (http://
msdn2.microsoft.com/en-us/library/2c1czate.aspx) but I am not an
experienced programmer and have no clue what "transition into a
disconnected apartment" means.

It definitely has to do with the function as I have striped everything
out and just run this in main:
For x As Integer = 1 To 200
Email = GetUserInfo("Test User", "mail")
Console.WriteLine(x)
Next

Anything less then ~180 and I don't get the message. The Function was
copied (shame on me) from google. I added the Finally clause to see
if disposing would help. The directory path has been changed to
protect the innocent.


Public Function GetUserInfo(ByVal inSAM As String, ByVal inType As
String) As String

Dim sPath As String = "LDAP://xxx/DC=xxx,DC=com"
Dim SamAccount As String = Right(inSAM, Len(inSAM) -
InStr(inSAM, "\"))
Dim myDirectory As New DirectoryEntry(sPath)
Dim mySearcher As New DirectorySearcher(myDirectory)
Dim mySearchResultColl As SearchResultCollection
Dim mySearchResult As SearchResult
Dim myResultPropColl As ResultPropertyCollection
Dim myResultPropValueColl As ResultPropertyValueCollection

Try
'Build LDAP query
mySearcher.Filter = ("(&(objectClass=user)(name=" &
SamAccount & "))")
mySearchResultColl = mySearcher.FindAll()
'I expect only one user from search result
Select Case mySearchResultColl.Count
Case 0
Return "Not Found"
Exit Function
Case Is > 1
Return "Multiple"
Exit Function
End Select

'Get the search result from the collection
mySearchResult = mySearchResultColl.Item(0)

'Get the Properites, they contain the usefull info
myResultPropColl = mySearchResult.Properties

'displayname, mail
'Retrieve from the properties collection the display name
and email of the user
myResultPropValueColl = myResultPropColl.Item(inType)
Return CStr(myResultPropValueColl.Item(0))

Catch ex As System.Exception
Return "Null"
'do some error return here.

Finally
mySearcher.Dispose()
myDirectory.Dispose()
End Try

End Function

What am I doing wrong here?
 
M

mr_doles

Oh come on...there is not one person out there that has a suggestion
on what could be causing this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top