2011-05-20 57 views
6

有沒有人知道在C#和VB.NET中的DirectorySearcher對象上的FindAll()方法的實現之間是否有區別?根據我的理解,它們都被「編譯」爲MSIL,並以相同的方式被CLR處理。針對我們的ADAM/LDAP系統,下面的C#代碼會拋出一個錯誤,而下面的VB.NET不會。C#和VB.NET LDAP搜索不同?

這裏是C#異常堆棧:

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 
    at System.DirectoryServices.DirectoryEntry.Bind() 
    at System.DirectoryServices.DirectoryEntry.get_AdsObject() 
    at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) 
    at System.DirectoryServices.DirectorySearcher.FindAll() 

這裏是C#錯誤:

System.Runtime.InteropServices.COMException was unhandled 
Message="The parameter is incorrect.\r\n" 
Source="System.DirectoryServices" 
ErrorCode=-2147024809 

C#代碼:

private void button1_Click(object sender, EventArgs e) 
{ 
    DirectoryEntry root = new DirectoryEntry("LDAP://directory.corp.com/OU=Person,OU=Lookups,O=Corp,C=US", null, null, AuthenticationTypes.Anonymous); 
    DirectorySearcher mySearcher = new DirectorySearcher(root); 

    mySearcher.Filter = "(uid=ssnlxxx)"; 
    mySearcher.PropertiesToLoad.Add("cn"); 
    mySearcher.PropertiesToLoad.Add("mail"); 

    SearchResultCollection searchResultCollection = null; 
    searchResultCollection = mySearcher.FindAll(); //this is where the error occurs 

    try 
    { 
     foreach (SearchResult resEnt in searchResultCollection) 
     { 
      Console.Write(resEnt.Properties["cn"][0].ToString()); 
      Console.Write(resEnt.Properties["mail"][0].ToString()); 
     } 
    } 
    catch (DirectoryServicesCOMException ex) 
    { 
     MessageBox.Show("Failed to connect LDAP domain, Check username or password to get user details."); 
    } 
} 

這是VB.NET代碼工作:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 

    Dim root As New  DirectoryEntry("LDAP://directory.corp.com/OU=People,OU=Lookups,O=corp,C=US", vbNull, vbNull, authenticationType:=DirectoryServices.AuthenticationTypes.Anonymous) 
    Dim searcher As New DirectorySearcher(root) 
    searcher.Filter = "(uid=ssnlxxx)" 
    searcher.PropertiesToLoad.Add("cn") 
    searcher.PropertiesToLoad.Add("mail") 

    Dim results As SearchResultCollection 

    Try 
     results = searcher.FindAll() 

     Dim result As SearchResult 
     For Each result In results 
      Console.WriteLine(result.Properties("cn")(0)) 
      Console.WriteLine(result.Properties("mail")(0)) 
     Next result 
    Catch ex As Exception 
     MessageBox.Show("There was an error") 
    End Try 
End Sub 
+0

什麼是異常消息? – Yuck 2011-05-20 14:59:29

+5

我不知道這會解決你的問題_(所以我評論,而不是'回答...'),但在你的c#代碼中你可能會改變'「LDAP://directory.corp.com/OU= Person,OU = Lookups,O = Corp,C = US「'到'@」LDAP://directory.corp.com/OU=Person,OU=Lookups,O=Corp,C=US「'。 @符號告訴C#編譯器忽略轉義字符,這是C#和VB.NET之間可能直接影響這個問題的少數差異之一。 – 2011-05-20 14:59:46

+0

什麼是VB.NET代碼中的'vbNull'?這與「Nothing」不同嗎? – 2011-05-20 15:07:33

回答

1

C#和VB.NET都不實現DirectorySearcher或.NET的任何其他部分。它們都是.NET Framework的一部分。

+0

謝謝。我意識到,最終這個功能是在框架而不是語言。類似於java和JVM。這並不能真正回答我的問題。 – user1195358 2011-05-20 18:37:48

+0

您問過「C#和VB.NET中的DirectorySearcher對象上的FindAll()方法的實現之間是否存在差異」。答案是「不,因爲該對象是.NET,而不是C#或VB.NET」。所以如果你的兩個代碼示例有區別,那是另外一回事。 – 2011-05-20 19:08:03

8

我要去猜測,在VB.NET代碼,你在vbNull傳遞(而不是Nothing)兩個參數的構造函數DirectoryEntry,並在C#代碼你傳遞nullvbNull大概來自不應使用的邪惡的Microsoft.VisualBasic裝配。

DirectoryEntry的構造函數檢查用戶名和密碼參數以查看它們是否爲空。如果vbNull != Nothing,構造函數不會將它們視爲null並且行爲會有所不同。

如果您使用Nothing或查看C#代碼是否使用String.Empty而不是null,請查看VB.NET代碼是否引發異常。

此外,在您的C#代碼中,FindAll的調用位於try塊之外。

+0

謝謝。這很有幫助。我會給你一些建議。 – user1195358 2011-05-20 18:40:02

+0

你解決了!將DirectoryEntry對象的構造函數中的空值更改爲String.Empty解決了問題。 COM層不能像空值。它必須需要一個空字符串。非常感謝! – user1195358 2011-05-20 20:58:18

+0

@ user477871歡迎使用堆棧溢出。我很高興你得到了你的答案,請花點時間和「接受」@格雷厄姆的回答表明他對你的幫助。花點時間熟悉常見問題http://stackoverflow.com/faq – 2011-05-20 21:34:27