Project Description

This project extends with some new features.

NuGet Package

Main Features

Support for different scopes
If used in web sites or in console applications, you might want to run your LINQ to LDAP in a thread-safe scope. Especially within the web, this can be a tough task if used with injections and all this kind of goodness.

For example, you cannot use a shared instance of the LINQ to LDAP DirectoryContext within a web application. Meaning if different requests access the same context, it will run into errors.

Therefore this project provides some classes which help you getting this done easily.

The following scopes are currently supported by DirectoryContextFactory
  • ThreadStatic
  • Web

Basic usage pattern for the DirectoryContextFactory:
    // instantiate the factory with the scoping you need.
    var factory = new DirectoryContextFactory(ScopeState.ThreadStatic);

    // create a new directory context instance.
    var ctx = factory.CreateContext();

    // bind it to the scope.

    // now we can use GetCurrentContext whenever needed.
    var current = factory.GetCurrentContext();

Unit of work implementation
The DirectoryContextUnitOfWork class implements a basic unit of work which uses the DirectoryContextFactory.


To setup the example, download the sources and follow the instruction in the readme.txt or read Examples and Integration tests setup:.

For the full code examples, download the sources and maybe read the Documentation.

MVC Example
The web site example uses Unity to instantiate a singleton instance of the DirectoryContextUnitOfWork within the global.asax.
On request start/end the begin and end unit of work calls are handled
    this.BeginRequest += (sender, args) =>
    this.EndRequest += (sender, e) =>
This means, we have a thread safe context per request with a shared connection pool instantiated only once!

Now we can use the context easily in our controllers by calling DirectoryContextUnitOfWork.CurrentContext.
    public ActionResult UserList()
        var result = this.unitOfWork.CurrentContext.Query<InetOrgPerson>(

        return View(result);
At that point, you don't have to care about the configuration or state anymore. You can simply access the context and query it... And at the end of the request the context will be disposed.

A basic unit of work console application example

    public static void Main(string[] args)
        // first create a factory with some configuration 
        // (there are the same overloads as DirectoryContext provides).
        var factory = new DirectoryContextFactory(
            scopeState: ScopeState.ThreadStatic, 
            configuration: DomainSetup.SetupConfiguration());

        // create a unit of work instance
        var unitOfWork = new DirectoryContextUnitOfWork(factory);

        // call begin to initialize a context and bind it to the current thread

        // do some work
        var result = unitOfWork.CurrentContext.Query<InetOrgPerson>(

        foreach (var user in result)

        // end the unit of work which will unbind and dispose the context

for more information see Documentation

Help Pages

Last edited Nov 29, 2013 at 7:59 AM by MichaConrad, version 9