I have a need to start a new thread for a long running task (writes to document store, triggering re-index etc).
I’m using Ninject in an ASP.Net MVC website. Essentially, I want to spawn a new thread from my Http Request, which instanciates it’s own objects, making sure that no database connections etc are used from the Http Request in the new thread. I’m not sure what to use to spawn the thread or how to configure Ninject to not re-use my database session from the web request context.
Below is a basic sequence diagram of the problem
I’ve read a bit about named scopes in Ninject but I’m not sure if in any way that’s related.

The code for my CommandProcessor which will spawn the new thread is:
public class CommandProcessor
{
private IKernel _serviceLocator;
public CommandProc(IKernel serviceLocator)
{
_serviceLocator = serviceLocator;
}
public void Process<TCommand>(TCommand command) where T : ICommand
{
var commandHandlers = _serviceLocator.Get<ICommandHandler<TCommand>>();
foreach (var commandHandler in commandHandlers)
{
// I want this call to be asynchronous
commandHandler.Handle(command);
}
}
}
Thanks in advance,
Hamish.
When you spawn your new thread, create a new activation context from Ninject to use in the thread: