This project is read-only.

Using Castle Windsor IoC with LinqToLDAP

Nov 19, 2014 at 8:29 AM
I downloaded the GIT example project that uses SimpleInjector as the Inversion of Control container and translated the code accress to Castle Windsor. My config code looks like this:
                .UsingFactoryMethod(() =>
                    var config = new LdapConfiguration()
                    .AddMapping(new UserImageMapping())
                    .AddMapping(new UserMapping());

                    return config;
and the Directory Context is registered as:
                .UsingFactoryMethod(() => new DirectoryContext(container.Resolve<ILdapConfiguration>())).LifestyleSingleton());
This issue I've come across is when using the DirectoryContext in a query such as:
 var photo =  context.Query<UserPhoto>().FirstOrDefault(u => u.EmployeeId == userQuery.Id);
The context object has a list of providers associated with it and the count of these providers keeps increasing every time a query is made. So much so that there's hundreds after a few minutes of use. I guess it's something to do with correct disposal of the context, but when I explicitly dispose of the context subsequent queries fail since the context is disposed and for some reason a new one isn't created.
Can anyone help?
Nov 21, 2014 at 11:01 AM
Edited Nov 21, 2014 at 11:03 AM
You have your registration reversed. LdapConfiguration should be registered as a singleton and DirectoryContext should be an instance per request.

Also, I'm going to remove the DirectoryQueryProvider tracking in DirectoryContext. That's left over from an older version of LINQ to LDAP when DirectoryQueryProvider had a hard reference to an LdapConnection. Now it uses a WeakReference.
Nov 21, 2014 at 11:04 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.