简体   繁体   English

如何区分DirectoryEntry无效路径或无法连接到.NET中的LDAP?

[英]How do I distinguish between a DirectoryEntry Invalid path, or not being able to connect to LDAP in .NET?

Let's say I have this LDAP path and I want to search for an object in AD using System.DirectoryServices.DirectoryEntry: 假设我有此LDAP路径,并且想使用System.DirectoryServices.DirectoryEntry在AD中搜索对象:

 var path = "LDAP://test.com/CN=bob,OU=Users,DC=test,DC=com"

This path was retrieved from AD 1 minute ago. 此路径是在1分钟前从AD检索到的。 What if the object was deleted that second or the connection to Active Directory dropped. 如果第二天删除了该对象或者与Active Directory的连接断开了怎么办。 In my application, i stored this path, and I want to change one of the attributes of that object. 在我的应用程序中,我存储了此路径,并且我想更改该对象的属性之一。 So, I retrieve the path and do whatever I want. 因此,我检索路径并做我想做的任何事情。 In both cases, deleted object or no connection, I get the same Exception 在这两种情况下,删除对象或没有连接,我都会得到相同的异常

"System.Runtime.InteropServices.COMException"

I want to throw an exception stating the problem, how do I distinguish between both cases? 我想抛出一个异常来说明问题,如何区分这两种情况? I want to do this because, in case it was a connection problem, i want to try again after a specific amount of time. 我想这样做是因为,如果是连接问题,我想在特定时间后重试。 However if the object is deleted, I want my Application user to know this. 但是,如果对象被删除,我希望我的应用程序用户知道这一点。

DirectoryEntry Constructor does not do any kind of validation. DirectoryEntry构造函数不执行任何类型的验证。 In Addition, DirectoryEntry.Exsist(path) returns false, but does not give additional information. 另外,DirectoryEntry.Exsist(path)返回false,但不提供其他信息。 Any Suggestions? 有什么建议么?

There's nothing wrong with catching another exception, then throwing another: 捕获另一个异常,然后抛出另一个异常没有任何问题:

try {
   my_dangerous_call();
} catch(COMException e) {
   throw new MyCustomException();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM