Event Listeners

There are 3 interfaces that allow you to subscribe to Add, Update, and Delete events. Each interface has a Notify method that takes a generic ListenerArgs parameter. The ListenerArgs object has a reference to the entry being modified (can be either a mapped object or DirectoryAttributes), the LdapConnection that will send the request, and the DirectoryRequest (AddRequest, ModifyRequest, or DeleteRequest). The DirectoryRequest will not have any of the attribute values added since you may want to perform validation beforehand.

public class EventListener : IPreAddEventListener,
                             IPreDeleteEventListener,
                             IPreUpdateEventListener
{
     public void Notify(ListenerArgs<object, AddRequest> args)
     {
          object entry = args.Entry;

          LdapConnection connection = args.Connection;

          AddRequest request = args.Request;

          //additional code
     }

     public void Notify(ListenerArgs<object, DeleteRequest> args)
     {
          string entry = args.Entry;

          LdapConnection connection = args.Connection;

          DeleteRequest request = args.Request;

          //additional code
     }

     public void Notify(ListenerArgs<object, ModifyRequest> args)
     {
          object entry = args.Entry;

          LdapConnection connection = args.Connection;

          ModifyRequest request = args.Request;

          //additional code
     }
}

Using your listener(s) can be done by either registering them with an LdapConfiguration or by passing them as an argument in one of the LdapConnection extension methods.

var config = new LdapConfiguration();
config.RegisterListener(new EventListener());

var connection = new LdapConnection("myserver");
connection.Add(new DirectoryAttributes("dn"), listeners: new [] {new EventListener()});

Last edited Aug 11, 2015 at 3:54 AM by MadHatter22, version 5