Take the 2-minute tour ×

I have a MVC 3 solution configured with Ninject using a repository pattern. Some of my bindings include:


I also added a console application to my solution and I want to leverage the same repository and services. My Ninject configuration for the console application looks like:


My console code looks like:

static void Main(string[] args)
    IKernel kernel = new StandardKernel(new IoCMapper());

    var service = kernel.Get<IMyService>();
    var logger = kernel.Get<ILogging>();

    ... do some processing here

This works just fine but I want t be sure that I am configuring Ninject correctly for a console application. Is it correct to use InSingletonScope() for all my bindings in my console application? Should I be configuring it differently?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Do you want one and only one instance of each of your repository services for the whole application? If so, then use InSingletonScope.

Is your console application multithreaded? If this is the case and you want a new instance of your services for each thread then you will use InThreadScope.

If you want a new instance of the service(s) each time they are called for, set it to InTransientScope.

You also have the option of defining your own scope using InScope. Bob Cravens gives a good overview of each of these here http://blog.bobcravens.com/2010/03/ninject-life-cycle-management-or-scoping/

share|improve this answer
The console application has a simple for loop that iterates through a few urls, scrapes the content, processes it and passes it to the services to save it to the database. No async requests so I assume this will make it single threaded? Which approach fits this scenario best? –  Thomas Jul 8 '11 at 17:36
If you want to use the same database connection each time then you can use InSingletonScope or InRequestScope. For a synchronous operation that should be fine. If for any reason your requests to the database service becomes asynchronous (which is common in UoW scenarios) then you may start running into problems with the database connection being used by two different calls at the same time. This happened to me several months ago and I spend days trying to figure out what was going on –  Scott Lance Jul 8 '11 at 17:39
When you say "your requests to the database service becomes asynchronous" I am assuming I actually have to modify my code to "become asynchronous" or is this something that can occur "under the hood"? –  Thomas Jul 8 '11 at 19:00
Correct, if you change it later and forget about the scope your services are in you may start seeing random errors –  Scott Lance Jul 8 '11 at 19:29
add comment

Your Answer


