views:

175

answers:

3

I've been trying to work out how to cancel a long-running AD search in System.DirectoryServices.Protocols. Can anyone help?

I've looked at the supportControl/supportedCapabilities attributes on RootDSE and they don't contain the 1.3.6.1.1.8 OID so I think that means it doesn't support the LDAP CANCEL extended operation as defined here: http://tools.ietf.org/html/rfc3909

That leaves the original LDAP ABANDON command (see here for list). But there doesn't seem to be a matching DirectoryRequest Class.

Anyone have any ideas?

A: 

I think, but I'm not positive, there is no asynch query with a cancel. It has an asynch property but it's to allow a collection to be filled, nothing to do with cancelling. The best I can offer is to put your query in a background worker thread and put an asynch callback that will deal with the answer when it comes back. If the user decides to cancel, you can just cancel the background worker thread. You'll free your app up, even if you haven't freed the ldap server up until it finishes it's query. You can find info on background worker threads at http://www.c-sharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx Don't forget to call .Dispose() when cleaning up your active directory objects to prevent memory leaks.

gjutras
I'm not talking about DirectorySearcher in System.DirectoryServices: I think you are. I'm talking about running a search using System.DirectoryServices.Protocols. As you can see from the links it's possible to do an async search using S.DS.P because it doesn't use ADSI but uses native LDAP and native LDAP has async searching and a cancel facility. I just can't find it in the docs. I don't want to leave the search running because of the impact it might be having on the server.
serialhobbyist
A: 

If the query will produce many data also, you can abandon them through paging. Specify a PageResultRequestControl option in the query, giving a fairly low page size (IIUC, 1000 is the default page size). IIUC, you'll send new requests every time you got a page (passing cookies from one response into the next request). When you choose to cancel the query, send another request with zero expected results.

Martin v. Löwis
A: 

I think I've found my answer: whilst I was reading around your suggestion, Martin, I came across the Abort method on the LdapConnection class. I didn't expect to find it there: starting out from the LDAP documentation I'd expected to find it as just another LDAPMessage but the MS guys seem to have treated it as a special case. If anyone is familiar with a non-MS implementation of LDAP and can comment on whether the MS approach is typical, I'd appreciate it to improve my understanding.

serialhobbyist