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

相关问题