Using Castle Windsor IoC with LinqToLDAP

Nov 19, 2014 at 7:29 AM
Hi
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:
container.Register(Component.For<ILdapConfiguration>()
                .UsingFactoryMethod(() =>
                {
                    var config = new LdapConfiguration()
                    .AddMapping(new UserImageMapping())
                    .AddMapping(new UserMapping());

                    config.ConfigurePooledFactory("my.ad.server");
                    return config;
                }));
and the Directory Context is registered as:
 container.Register(Component.For<IDirectoryContext>()
                .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?
Coordinator
Nov 21, 2014 at 10:01 AM
Edited Nov 21, 2014 at 10: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.
Coordinator
Nov 21, 2014 at 10:04 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.