Ejemplo n.º 1
0
        /// <summary>
        /// Deletes an entry from the directory.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="distinguishedName">The distinguished name of the entry
        /// </param><param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="listeners">The event listeners to be notified.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="distinguishedName"/> is null, empty or white space.</exception>
        /// <exception cref="DirectoryOperationException">Thrown if the operation fails.</exception>
        /// <exception cref="LdapException">Thrown if the operation fails.</exception>
        public static void Delete(this LdapConnection connection, string distinguishedName, ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IDeleteEventListener> listeners = null)
        {
            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }
                if (distinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentNullException("distinguishedName");
                }

                var request = new DeleteRequest(distinguishedName);
                if (controls != null)
                {
                    request.Controls.AddRange(controls);
                }

                if (listeners != null)
                {
                    var args = new ListenerPreArgs <string, DeleteRequest>(distinguishedName, request, connection);
                    foreach (var eventListener in listeners.OfType <IPreDeleteEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }

                if (log != null && log.TraceEnabled)
                {
                    log.Trace(request.ToLogString());
                }

                var response = connection.SendRequest(request) as DeleteResponse;
                response.AssertSuccess();

                if (listeners != null)
                {
                    var args = new ListenerPostArgs <string, DeleteRequest, DeleteResponse>(distinguishedName, request, response, connection);
                    foreach (var eventListener in listeners.OfType <IPostDeleteEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("An error occurred while trying to delete '{0}'.", distinguishedName);
                if (log != null)
                {
                    log.Error(ex, message);
                }
                throw;
            }
        }
        /// <summary>
        /// Deletes an entry from the directory.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="distinguishedName">The distinguished name of the entry
        /// </param><param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="listeners">The event listeners to be notified.</param>
        /// <param name="resultProcessing">How the async results are processed</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="distinguishedName"/> is null, empty or white space.</exception>
        /// <exception cref="DirectoryOperationException">Thrown if the operation fails.</exception>
        /// <exception cref="LdapException">Thrown if the operation fails.</exception>
        public static async Task DeleteAsync(this LdapConnection connection, string distinguishedName, ILinqToLdapLogger log = null,
                                             DirectoryControl[] controls = null, IEnumerable <IDeleteEventListener> listeners = null, PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing)
        {
            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }
                if (distinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentNullException("distinguishedName");
                }

                var request = new DeleteRequest(distinguishedName);
                if (controls != null)
                {
                    request.Controls.AddRange(controls);
                }

                if (listeners != null)
                {
                    var args = new ListenerPreArgs <string, DeleteRequest>(distinguishedName, request, connection);
                    foreach (var eventListener in listeners.OfType <IPreDeleteEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }

                if (log != null && log.TraceEnabled)
                {
                    log.Trace(request.ToLogString());
                }

                DeleteResponse response = null;
#if NET45
                await Task.Factory.FromAsync(
                    (callback, state) =>
                {
                    return(connection.BeginSendRequest(request, resultProcessing, callback, state));
                },
                    (asyncresult) =>
                {
                    response = (DeleteResponse)connection.EndSendRequest(asyncresult);
                    response.AssertSuccess();
                },
                    null
                    ).ConfigureAwait(false);
#else
                response = await Task.Run(() => connection.SendRequest(request) as DeleteResponse).ConfigureAwait(false);

                response.AssertSuccess();
#endif

                if (listeners != null)
                {
                    var args = new ListenerPostArgs <string, DeleteRequest, DeleteResponse>(distinguishedName, request, response, connection);
                    foreach (var eventListener in listeners.OfType <IPostDeleteEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("An error occurred while trying to delete '{0}'.", distinguishedName);
                if (log != null)
                {
                    log.Error(ex, message);
                }
                throw;
            }
        }