我們是一個團隊,我們每個人都會遇到這種隨機錯誤。該錯誤在下面列出並顯示在行上: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
只是一個想法 - 你是否正確地處理你的PrincipalContexts? – 2013-04-04 09:40:56
發佈導致異常的代碼塊。 – Daro 2013-04-05 04:33:18
@Daro:我剛更新了導致異常的代碼。 – 2013-04-05 05:55:19