簡體   English   中英

為什么我的目錄搜索需要這么長時間?

[英]Why is my directory search taking so long?

我一直在對我構建的REST API中的性能進行故障排除,除其他外,它根據提供的搜索詞從Active Directory返回用戶列表。 基於我為測試目的而構建的一些日志記錄,我可以看到獲取設置(例如LDAP搜索信息)和檢索所有搜索結果的整個過程只需不到一秒鍾:

30/08/2017 3:37:58 PM | Getting search results.
30/08/2017 3:37:58 PM | Retrieving default settings
30/08/2017 3:37:58 PM | Default settings retrieved. Creating directoryEntry
30/08/2017 3:37:58 PM | Search retrieved.
30/08/2017 3:37:58 PM | Iterating through search results.
30/08/2017 3:38:16 PM | Search results iteration complete.

但是,正如您可以看到迭代這些搜索結果並填充我的用戶列表需要18秒。 這是我的代碼:

SearchResultCollection resultList = new DirectorySearcher(CreateDirectoryEntry())
{
    Filter = ("(&(objectClass=user) (cn=*" + SearchTerm + "*))"),
    PropertiesToLoad =
    {
        "givenName",
        "sn",
        "sAMAccountName",
        "mail"
    }
}.FindAll();

foreach (SearchResult result in resultList)
{
    ADUser thisUser = new ADUser();

    try
    {
        thisUser.Firstname = result.Properties["givenName"][0].ToString();
    }
    catch
    {
        thisUser.Firstname = "Firstname not found";
    }
    try
    {
        thisUser.Lastname = result.Properties["sn"][0].ToString();
    }
    catch
    {
        thisUser.Lastname = "Lastname not found";
    }
    try
    {
        thisUser.EmailAddress = result.Properties["mail"][0].ToString();
    }
    catch
    {
        thisUser.EmailAddress = "Email address not found";
    }

    UserList.Add(thisUser);
}

它非常香草,沒有做任何花哨的事情。 知道為什么會這么慢,或者有什么建議我可以做些什么來加快速度呢?

UPDATE

基於評論和答案,我已從代碼中刪除了空檢查。 所以現在它看起來像這樣:

foreach (SearchResult result in resultList)
{
    ADUser thisUser = new ADUser();

    thisUser.Firstname = result.Properties["givenName"][0].ToString();
    thisUser.Lastname = result.Properties["sn"][0].ToString();
    thisUser.EmailAddress = result.Properties["mail"][0].ToString();

    UserList.Add(thisUser);
}

這並沒有改善性能。 我可以看到這個循環仍然需要大約18秒,即使只返回一個結果。 (它還證明了我AD中的糟糕數據意味着我需要這個空檢查!)

設置的屬性時,依靠例外thisUser根據您的目錄時,它可能不是所有的特殊的用戶不用自己givenNamesn ,和/或mail屬性填充。 如果您有一長串搜索結果,則所有這些例外都會累加起來。 考慮檢查是否存在所需的屬性而不是使用try / catch塊:

thisUser.Firstname = result.Properties.Contains("givenName")
    ? result.Properties["givenName"][0].ToString()
    : "Firstname not found";
thisUser.Lastname = result.Properties.Contains("sn")
    ? result.Properties["sn"][0].ToString()
    : "Lastname not found";
thisUser.EmailAddress = result.Properties.Contains("mail")
    ? result.Properties["mail"][0].ToString()
    : "Email address not found";

您可以使用C#6中的 條件運算符null-coalescing運算符來將其簡化為以下內容:

thisUser.Firstname = result.Properties["givenName"]?[0]?.ToString() ?? "Firstname not found";
thisUser.Lastname = result.Properties["sn"]?[0]?.ToString() ?? "Lastname not found";
thisUser.EmailAddress = result.Properties["mail"]?[0]?.ToString() ?? "Email address not found";

你可以試試這樣的東西:

foreach (SearchResult result in resultList)
            {
                ADUser thisUser = new ADUser();
                if(result.Properties["givenName"][0] != null)
                    thisUser.Firstname = result.Properties["givenName"][0].ToString();
                else
                    thisUser.Firstname = "Firstname not found";

                if(thisUser.Lastname = result.Properties["sn"][0] != null)
                    thisUser.Lastname = result.Properties["sn"][0].ToString();
                else
                    thisUser.Lastname = "Lastname not found";

                if(result.Properties["mail"][0] != null)
                    thisUser.EmailAddress = result.Properties["mail"][0].ToString();
                else
                    thisUser.EmailAddress = "Email address not found";

                UserList.Add(thisUser);
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM