PrincipalContext throwing AccessViolationException on creation
I have a WCF self hosted service which I am trying to interact with AD, on a single user it works fine but when many users try hitting it at once I get an `AccessViolationException` error with the following stacktrace:

    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    
    at System.DirectoryServices.Protocols.Wldap32.ldap_init(String hostName, Int32 portNumber) 
    at System.DirectoryServices.Protocols.LdapConnection.Init() 
    at System.DirectoryServices.Protocols.LdapConnection..ctor(LdapDirectoryIdentifier identifier, NetworkCredential credential, AuthType authType) 
    at System.DirectoryServices.Protocols.LdapConnection..ctor(LdapDirectoryIdentifier identifier) 
    at System.DirectoryServices.Protocols.LdapConnection..ctor(String server) 
    at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties) 
    at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() 
    at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password) 
    at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String userName, String password) 
    at GetUserGroups(String domain, String UserToFind) in C:\dev\....vb:line 7

The error comes from the line:

    Using context As New PrincipalContext(ContextType.Domain, domain, My.Settings.ADUSERNAME, My.Settings.ADPASSWORD)

My full function:

    Friend Shared Function GetUserGroups(ByVal domain As String, ByVal UserToFind As String) As List(Of String)
        Dim retVal As New List(Of String)()

        Dim _domain As String = domain.Trim().ToLower() & "\"
        UserToFind = CleanUserName(UserToFind, domain)
        Try
            Using context As New PrincipalContext(ContextType.Domain, domain, My.Settings.ADUSERNAME, My.Settings.ADPASSWORD)

                Using _userPrincipal As UserPrincipal = UserPrincipal.FindByIdentity(context, UserToFind)

                    Dim groupSearch As PrincipalSearchResult(Of Principal) = _userPrincipal.GetAuthorizationGroups()
                    Dim iterGroup As IEnumerator(Of Principal) = groupSearch.GetEnumerator()

                    Using iterGroup
                        While iterGroup.MoveNext
                            Try
                                Dim _principal As Principal = iterGroup.Current
                                retVal.Add(_domain & _principal.SamAccountName.Trim().ToLower())
                            Catch ex As Exception
                            End Try
                        End While
                    End Using

                End Using
            End Using

        Catch ex As Exception

            Console.WriteLine("An error has occurred in {0}.{1} method", System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name, System.Reflection.MethodBase.GetCurrentMethod.Name)

        Finally

        End Try

        Return retVal

    End Function

PrincipalContext throwing AccessViolationException on creation
===

I have a WCF self hosted service which I am trying to interact with AD, on a single user it works fine but when many users try hitting it at once I get an `AccessViolationException` error with the following stacktrace:

    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    
    at System.DirectoryServices.Protocols.Wldap32.ldap_init(String hostName, Int32 portNumber) 
    at System.DirectoryServices.Protocols.LdapConnection.Init() 
    at System.DirectoryServices.Protocols.LdapConnection..ctor(LdapDirectoryIdentifier identifier, NetworkCredential credential, AuthType authType) 
    at System.DirectoryServices.Protocols.LdapConnection..ctor(LdapDirectoryIdentifier identifier) 
    at System.DirectoryServices.Protocols.LdapConnection..ctor(String server) 
    at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties) 
    at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval() 
    at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password) 
    at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String userName, String password) 
    at GetUserGroups(String domain, String UserToFind) in C:\dev\....vb:line 7

The error comes from the line:

    Using context As New PrincipalContext(ContextType.Domain, domain, My.Settings.ADUSERNAME, My.Settings.ADPASSWORD)

My full function:

    Friend Shared Function GetUserGroups(ByVal domain As String, ByVal UserToFind As String) As List(Of String)
        Dim retVal As New List(Of String)()

        Dim _domain As String = domain.Trim().ToLower() & "\"
        UserToFind = CleanUserName(UserToFind, domain)
        Try
            Using context As New PrincipalContext(ContextType.Domain, domain, My.Settings.ADUSERNAME, My.Settings.ADPASSWORD)

                Using _userPrincipal As UserPrincipal = UserPrincipal.FindByIdentity(context, UserToFind)

                    Dim groupSearch As PrincipalSearchResult(Of Principal) = _userPrincipal.GetAuthorizationGroups()
                    Dim iterGroup As IEnumerator(Of Principal) = groupSearch.GetEnumerator()

                    Using iterGroup
                        While iterGroup.MoveNext
                            Try
                                Dim _principal As Principal = iterGroup.Current
                                retVal.Add(_domain & _principal.SamAccountName.Trim().ToLower())
                            Catch ex As Exception
                            End Try
                        End While
                    End Using

                End Using
            End Using

        Catch ex As Exception

            Console.WriteLine("An error has occurred in {0}.{1} method", System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name, System.Reflection.MethodBase.GetCurrentMethod.Name)

        Finally

        End Try

        Return retVal

    End Function
