1

我們是一個團隊,我們每個人都會遇到這種隨機錯誤。該錯誤在下面列出並顯示在行上:UserPrincipal.FindByIdentity(principalContext,windowsPrincipal.Identity.Name);UserPrincipal.FindByIdentity有時會因DirectoryServicesCOMException失敗:發生操作錯誤

它工作得很好幾天/周/月,然後我們其中一個得到這個錯誤。

在我們的測試服務器上,我們沒有像本地機器那樣頻繁部署更改,它在我們發生此錯誤之前可以運行數月。

如果我們將應用程序池從ApplicationPoolIdentity更改爲NetworkService,則可以使用。但是,切換回ApplicationPoolIdentity後,會出現相同的錯誤。

IISreset沒有幫助。

重新啓動計算機總能解決問題,所以ApplicationPoolIdentity沒有任何問題來驗證我們每天的身份。

這是代碼(略加修改),我們用:

var windowsPrincipal = principal as WindowsPrincipal; 
if (windowsPrincipal == null) 
    return null; 
try 
{ 
    var principalContext = new PrincipalContext(ContextType.Domain); 
    var userPrincipal = UserPrincipal.FindByIdentity(principalContext, windowsPrincipal.Identity.Name); 
    if (userPrincipal == null) return null; 
    return userPrincipal.Surname; 
} 

以下是錯誤消息:

An operations error occurred. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred. 

Source Error: 
var principalContext = new PrincipalContext(ContextType.Domain); 
var userPrincipal = UserPrincipal.FindByIdentity(principalContext, windowsPrincipal.Identity.Name); 

Stack Trace: 


[DirectoryServicesCOMException (0x80072020): An operations error occurred. 
] 
    System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +628309 
    System.DirectoryServices.DirectoryEntry.Bind() +44 
    System.DirectoryServices.DirectoryEntry.get_AdsObject() +42 
    System.DirectoryServices.PropertyValueCollection.PopulateList() +29 
    System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +63 
    System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +163 
    System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +521413 
    System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +51 
    System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +161 
    System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +42 
    System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +29 
    System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +81 
+0

只是一個想法 - 你是否正確地處理你的PrincipalContexts? – 2013-04-04 09:40:56

+0

發佈導致異常的代碼塊。 – Daro 2013-04-05 04:33:18

+0

@Daro:我剛更新了導致異常的代碼。 – 2013-04-05 05:55:19

回答

0

如果你不是在finaly塊處置它,你會最終耗盡資源...

Using (var principalContext = new PrincipalContext(ContextType.Domain)) 
{ 
var userPrincipal = UserPrincipal.FindByIdentity(principalContext, 
    windowsPrincipal.Identity.Name); 
if (userPrincipal == null) return null; 
    return userPrincipal.Surname; 
} 

應該幫助

+0

即使在使用語句之後,我們仍然面臨着這個問題。我真的認爲它會解決我們的問題,但不幸的是錯誤依然存在。 – 2013-05-06 09:04:16

+0

您可以觀察任務管理器以查看IIS進程中的句柄數是否持續攀升。 (進程/查看/選擇列... /句柄) – Daro 2013-05-08 15:28:07

+0

我試過了,有錯誤的iis進程的句柄數量大約是500.其他一些iis進程的句柄數量多於雙重句柄數量,並且它們運行良好(但它們不會針對UserPrincipal運行任何代碼)。 而我只是不能解釋爲什麼當我作爲ApplicationPoolIdentity運行時不起作用,但是當我切換到NetworkService時,它工作得很好。 – 2013-05-13 10:21:49

相關問題