我们是一个团队,我们每个人都会遇到这种随机错误。该错误在下面列出并显示在行上: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