/// <summary>
        /// Updates the entry in the directory.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="entry">The entry to update.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <param name="listeners">The event listeners to be notified.</param>
        /// <param name="resultProcessing">How the async results are processed</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="entry"/> is null.
        /// </exception>
        /// <exception cref="DirectoryOperationException">Thrown if the operation fails</exception>
        /// <exception cref="LdapException">Thrown if the operation fails</exception>
        public static async Task UpdateAsync(this LdapConnection connection, IDirectoryAttributes entry,
                                             ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IUpdateEventListener> listeners = null,
                                             PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing)
        {
            string distinguishedName = null;

            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }
                if (entry == null)
                {
                    throw new ArgumentNullException("entry");
                }

                distinguishedName = entry.DistinguishedName;
                if (distinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentException("entry.DistinguishedName is invalid.");
                }

                var changes = entry.GetChangedAttributes();

                if (changes.Any())
                {
                    var request = new ModifyRequest(distinguishedName, changes.ToArray());
                    if (controls != null)
                    {
                        request.Controls.AddRange(controls);
                    }

                    if (listeners != null)
                    {
                        var args = new ListenerPreArgs <object, ModifyRequest>(entry, request, connection);
                        foreach (var eventListener in listeners.OfType <IPreUpdateEventListener>())
                        {
                            eventListener.Notify(args);
                        }
                    }

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

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

                    response.AssertSuccess();
#endif

                    if (listeners != null)
                    {
                        var args = new ListenerPostArgs <object, ModifyRequest, ModifyResponse>(entry, request, response, connection);
                        foreach (var eventListener in listeners.OfType <IPostUpdateEventListener>())
                        {
                            eventListener.Notify(args);
                        }
                    }
                }
                else
                {
                    if (log != null && log.TraceEnabled)
                    {
                        log.Trace(string.Format("No changes found for {0}.", distinguishedName));
                    }
                }
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Error(ex, string.Format("An error occurred while trying to update '{0}'.", distinguishedName));
                }

                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the entry in the directory.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="entry">The entry to update.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <param name="listeners">The event listeners to be notified.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="entry"/> is null.
        /// </exception>
        /// <exception cref="DirectoryOperationException">Thrown if the operation fails</exception>
        /// <exception cref="LdapException">Thrown if the operation fails</exception>
        public static void Update(this LdapConnection connection, IDirectoryAttributes entry,
                                  ILinqToLdapLogger log = null, DirectoryControl[] controls = null, IEnumerable <IUpdateEventListener> listeners = null)
        {
            string distinguishedName = null;

            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }
                if (entry == null)
                {
                    throw new ArgumentNullException("entry");
                }

                distinguishedName = entry.DistinguishedName;
                if (distinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentException("entry.DistinguishedName is invalid.");
                }

                var changes = entry.GetChangedAttributes();

                if (changes.Any())
                {
                    var request = new ModifyRequest(distinguishedName, changes.ToArray());
                    if (controls != null)
                    {
                        request.Controls.AddRange(controls);
                    }

                    if (listeners != null)
                    {
                        var args = new ListenerPreArgs <object, ModifyRequest>(entry, request, connection);
                        foreach (var eventListener in listeners.OfType <IPreUpdateEventListener>())
                        {
                            eventListener.Notify(args);
                        }
                    }

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

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

                    if (listeners != null)
                    {
                        var args = new ListenerPostArgs <object, ModifyRequest, ModifyResponse>(entry, request, response, connection);
                        foreach (var eventListener in listeners.OfType <IPostUpdateEventListener>())
                        {
                            eventListener.Notify(args);
                        }
                    }
                }
                else
                {
                    if (log != null && log.TraceEnabled)
                    {
                        log.Trace(string.Format("No changes found for {0}.", distinguishedName));
                    }
                }
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Error(ex, string.Format("An error occurred while trying to update '{0}'.", distinguishedName));
                }

                throw;
            }
        }