<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="/rss.xsl"?><rss version="2.0"><channel><title>Simple Injector</title><link>http://simpleinjector.codeplex.com/project/feeds/rss</link><description>The Simple Injector is an easy-to-use Inversion of Control library for .NET and Silverlight.</description><item><title>New Post: SimpleInjector Prism Bootstrapper</title><link>http://simpleinjector.codeplex.com/discussions/444128</link><description>&lt;div style="line-height: normal;"&gt;Hi all,&lt;br /&gt;
&lt;br /&gt;
So a colleague directed me towards SimpleInjector after viewing the stats &lt;a href="http://www.palmmedia.de/Blog/2011/8/30/ioc-container-benchmark-performance-comparison" rel="nofollow"&gt;here&lt;/a&gt;. He felt it had the right level of features for his MVC project, whilst being among the fastest around.&lt;br /&gt;
&lt;br /&gt;
I'm starting a new prism app (still feeling my way around the basics of prism/MVVM to be honest), but I was wondering if there is a SI bootstrapper around anywhere for Prism?&lt;br /&gt;
&lt;br /&gt;
If not, can someone point me in the direction of where I can see how the bootstrappers are constructed (i.e. what I'd need to do to roll my own).&lt;br /&gt;
&lt;br /&gt;
Thanks&lt;br /&gt;
Fergal&lt;br /&gt;
&lt;/div&gt;</description><author>ObsidianPhoenix</author><pubDate>Sat, 18 May 2013 10:55:12 GMT</pubDate><guid isPermaLink="false">New Post: SimpleInjector Prism Bootstrapper 20130518105512A</guid></item><item><title>Updated Wiki: How-to</title><link>https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;version=32</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;How To...&lt;/h1&gt;
&lt;ul&gt;&lt;li&gt;How to &lt;a href="#Register-Factory-Delegates"&gt;Register factory delegates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Instances-By-Key"&gt;Resolve instances by key&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Arrays-And-Lists"&gt;Resolve array and lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;Register multiple interfaces with the same implementation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Override-Existing-Registrations"&gt;Override existing registrations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Verifying-Configuration"&gt;Verify the container&amp;#39;s configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Multi-Threaded-Applications"&gt;Work with dependency injection in multi-threaded applications&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;Register factory delegates&lt;a name="Register-Factory-Delegates"&gt;&lt;/a&gt;&lt;a name="Register_Factory_Delegates"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;b&gt;Simple Injector&lt;/b&gt; allows you to register a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate for the creation of an instance. This is especially useful in scenarios where it is impossible for the container to create the instance. There are overloads of the &lt;i&gt;Register&lt;/i&gt; method available that accept an &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; argument:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IMyService&amp;gt;(() =&amp;gt; SomeSubSystem.CreateMyService());
&lt;/pre&gt;&lt;/div&gt;In situations where a service needs to create multiple instances of a certain dependencies, or needs to explicitly control the lifetime of such dependency, abstract factories can be used. Instead of injecting an &lt;i&gt;IMyService&lt;/i&gt;, you should inject an &lt;i&gt;IMyServiceFactory&lt;/i&gt; that allows services to create new instances of &lt;i&gt;IMyService&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IMyServiceFactory
{
    IMyService CreateNew();
}

&lt;span style="color:Green;"&gt;// Implementation&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ServiceFactory : IMyServiceFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IMyService CreateNew()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;IMyServiceFactory, ServiceFactory&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMyServiceFactory factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(IMyServiceFactory factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
        {
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }

        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service2 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
        {
            &lt;span style="color:Green;"&gt;// use service 2&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Instead of creating specific interfaces for your factories, you can also choose to inject &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates into your services:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;Func&amp;lt;IMyService&amp;gt;&amp;gt;(
    () =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl());

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMyService&amp;gt; factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Func&amp;lt;IMyService&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.Invoke())
        {
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;This saves you from having to define a new interface and implementation per factory.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: On the downside however, this communicates less clearly the intend of your code and as a result might make your code harder to grasp for other developers.&lt;/i&gt;&lt;/blockquote&gt;
When you choose &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates over specific factory interfaces, you can define the following extension method that makes it easier to register &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; factories:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;TService, TImplementation&amp;gt;(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container,
    Lifestyle lifestyle = &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    lifestyle = lifestyle ?? Lifestyle.Transient;

    &lt;span style="color:Green;"&gt;// Register the type explicitly to keep the configuration&lt;/span&gt;
    &lt;span style="color:Green;"&gt;// verifiable.&lt;/span&gt;
    container.Register&amp;lt;TService, TImplementation&amp;gt;(lifestyle);
    
    &lt;span style="color:Green;"&gt;// Register the Func&amp;lt;T&amp;gt; that resolves that instance.&lt;/span&gt;
    container.RegisterSingle&amp;lt;Func&amp;lt;TService&amp;gt;&amp;gt;(
        () =&amp;gt; container.GetInstance&amp;lt;TService&amp;gt;());
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;IMyService, RealService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous extension method allowed registration of a single factory, but won&amp;#39;t be maintainable when you want all registrations to be resolvable using &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates by default. &lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: We personally think that allowing to register Func&amp;lt;T&amp;gt; delegates by default is a design smell. The use of Func&amp;lt;T&amp;gt; delegates makes your design harder to follow and your system harder to maintain and test. If you have many constructors in your system that depend on a Func&amp;lt;T&amp;gt;, please take a good look at your dependency strategy. If in doubt, please ask us here on CodePlex or on Stackoverflow.&lt;/i&gt;&lt;/blockquote&gt;
The following extension method allows Simple Injector to resolve all types using a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate by default:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AllowResolvingFuncFactories(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = options.Container;

    container.ResolveUnregisteredType += (sender, e) =&amp;gt;
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (e.UnregisteredServiceType.IsGenericType &amp;amp;&amp;amp;
            e.UnregisteredServiceType.GetGenericTypeDefinition() ==
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;))
        {
            Type serviceType = e.UnregisteredServiceType
                .GetGenericArguments().First();

            InstanceProducer registration =
                container.GetRegistration(serviceType);

            &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (registration != &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
            {
                Type funcType =
                    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;).MakeGenericType(serviceType);

                &lt;span style="color:Blue;"&gt;var&lt;/span&gt; factoryDelegate = 
                    Expression.Lambda(funcType,
                        registration.BuildExpression())
                    .Compile();

                e.Register(Expression.Constant(factoryDelegate));
            }
        }
    };
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.Options.AllowResolvingFuncFactories();
&lt;/pre&gt;&lt;/div&gt;After calling the &lt;b&gt;AllowResolvingFuncFactories&lt;/b&gt; extension method, the container allows resolving &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates.&lt;br /&gt;&lt;br /&gt;&lt;a name="lazy"&gt;&lt;/a&gt;Just like &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates can be injected, &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; instances can be injected into services. &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; is useful in situations where the creation of a service is time consuming, but is not always used. Thus &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; allows you to postpone the creation of that service until the moment that it is really needed:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterLazy&amp;lt;T&amp;gt;(&lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    Func&amp;lt;T&amp;gt; factory = () =&amp;gt; container.GetInstance&amp;lt;T&amp;gt;();

    container.Register&amp;lt;Lazy&amp;lt;T&amp;gt;&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Lazy&amp;lt;T&amp;gt;(factory));
}

&lt;span style="color:Green;"&gt;// Registration    &lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; myService;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Lazy&amp;lt;IMyService&amp;gt; myService)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService = myService;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (someCondition)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService.Value.Operate();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Note that instead of polluting the API of your application with &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; dependencies, it is usually cleaner to hide the &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; behind a proxy:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Proxy definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyLazyServiceProxy : IMyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; wrapped;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyLazyServiceProxy(Lazy&amp;lt;IMyService&amp;gt; wrapped)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped = wrapped;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Operate()
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped.Value.Operate();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();
container.Register&amp;lt;IMyService, MyLazyServiceProxy&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This way the application can simply depend on &lt;i&gt;IMyService&lt;/i&gt; instead of &lt;i&gt;Lazy&amp;lt;IMyService&amp;gt;&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: The same warning applies to the use of Lazy&amp;lt;T&amp;gt; as it does for the use of Func&amp;lt;T&amp;gt; delegates. For more information about creating an application and container configuration that can be successfully verified, please read the &lt;a href="#Verifying-Configuration"&gt;How To Verify the container&amp;#8217;s configuration&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve instances by key&lt;a name="Resolve-Instances-By-Key"&gt;&lt;/a&gt;&lt;a name="Resolve_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h2&gt;Resolving instances by a key is a feature that is deliberately left out of the &lt;b&gt;Simple Injector&lt;/b&gt;, because we feel it steers developers to a design were the application tends to have a dependency on the DI container itself. For resolving a keyed instance, you will often need to call into the container itself, which leads to the &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator anti-pattern&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This doesn’t mean however, that resolving instances by a key is never useful. Resolving instances by a key however, is much more a job for a specific factory than for a container itself. This makes the design much cleaner, saves you from having to take a dependency on the DI framework, and enables many scenarios that the DI container writer just didn’t consider.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The need for keyed registration can be an indication of ambiguity in the application design. Take a good look if each keyed registration shouldn&amp;#39;t have its own unique interface, or perhaps each registration should implement each own version of a generic interface.&lt;/i&gt;&lt;/blockquote&gt;
Take a look at the following scenario, where we want to retrieve instances of type &lt;i&gt;IRequestHandler&lt;/i&gt; by a string key. There are of course several ways to achieve this, but here is a simple but effective way, by defining an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IRequestHandlerFactory
{
    IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name);
}

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; factory =
    container.GetInstance&amp;lt;IRequestHandlerFactory&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = factory.CreateNew(&lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;);
handler.Handle(requestContext);
&lt;/pre&gt;&lt;/div&gt;By inheriting from the BCL’s &lt;i&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/i&gt;, creating an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt; implementation, is almost a one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Func&amp;lt;IRequestHandler&amp;gt;&amp;gt;,
    IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]();
    }
}
&lt;/pre&gt;&lt;/div&gt;With this class, we can register &lt;i&gt;Func&amp;lt;IRequestHandler&amp;gt;&lt;/i&gt; factory methods by a key. With this in place the registration of keyed instances is a breeze:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
 
container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;DefaultRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;OrdersRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;CustomersRequestHandler&amp;gt;() },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this design will work with almost all DI containers, making this not only a design that is easy to follow, but making it very easy to migrate to another DI implementation.&lt;/i&gt;&lt;/blockquote&gt;
If you don’t like a design that uses &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates this way, it can easily be changed to be a &lt;b&gt;Dictionary&amp;lt;string, Type&amp;gt;&lt;/b&gt; instead. The &lt;i&gt;RequestHandlerFactory&lt;/i&gt; can be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Type&amp;gt;, IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; RequestHandlerFactory(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container.GetInstance(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]);
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (IRequestHandler)handler;
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration will then look as follows:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory(container)
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DefaultRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(OrdersRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomersRequestHandler) },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Please remember the previous note about ambiguity in the application design. In the given example the design would probably be better af by using a generic &lt;b&gt;IRequestHandler&amp;lt;TRequest&amp;gt;&lt;/b&gt; interface. This would allow the implementations to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Batch_Registration"&gt;batch registered using a single line of code&lt;/a&gt;, saves you from using keys, and results in a configuration the is &lt;a href="#Verifying-Configuration"&gt;verifiable by the container&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
A last option to do keyed registrations is to manually create registrations and store them in a dictionary. The following example shows previous &lt;i&gt;RequestHandlerFactory&lt;/i&gt; with this approach:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory : IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, InstanceProducer&amp;gt; producers =
        &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, InstanceProducer&amp;gt;(
            StringComparer.OrdinalIgnoreCase);

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; RequestHandlerFactory(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    IRequestHandler IRequestHandlerFactory.CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.producers[name].GetInstance();
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (IRequestHandler)handler;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Register&amp;lt;TImplementation&amp;gt;(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name,
        Lifestyle lifestyle = &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
        &lt;span style="color:Blue;"&gt;where&lt;/span&gt; TImplementation : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;, IRequestHandler
    {
        lifestyle = lifestyle ?? Lifestyle.Transient;

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration = lifestyle
            .CreateRegistration&amp;lt;IRequestHandler, TImplementation&amp;gt;(container);

        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; producer =
            &lt;span style="color:Blue;"&gt;new&lt;/span&gt; InstanceProducer(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IRequestHandler), registration);

        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.producers.Add(name, producer);
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration will then look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; factory = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory(container);

factory.Register&amp;lt;DefaultRequestHandler&amp;gt;(&lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;);
factory.Register&amp;lt;OrdersRequestHandler&amp;gt;(&lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;);
factory.Register&amp;lt;CustomersRequestHandler&amp;gt;(&lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;);

container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(factory);
&lt;/pre&gt;&lt;/div&gt;The advantage of this method is that it completely integrates with the container. For instance, decorators can be applied to individual returned instances and types can be registered multiple times if needed.&lt;br /&gt;
&lt;h2&gt;Resolve array and lists&lt;a name="Resolve-Arrays-And-Lists"&gt;&lt;/a&gt;&lt;a name="Resolve_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h2&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; allows registration of collections of elements using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Container_RegisterAll.htm"&gt;RegisterAll&lt;/a&gt; method overloads. Collections can be resolved by using one of the &lt;b&gt;GetAllInstances&amp;lt;T&amp;gt;()&lt;/b&gt; method, by calling &lt;b&gt;GetInstance&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;()&lt;/b&gt;, or by defining an &lt;i&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/i&gt; parameter in the constructor of a type that is created using automatic constructor injection.&lt;br /&gt;&lt;br /&gt;Injection of other collection types, such as &lt;i&gt;arrays of T&lt;/i&gt; or &lt;i&gt;IList&amp;lt;T&amp;gt;&lt;/i&gt; into constructors is not supported out of the box. By hooking onto the unregistered type resolution event however, this functionality can be added. Look &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions&amp;referringTitle=How-to"&gt;here&lt;/a&gt; for an example extension method that allows this behavior for &lt;i&gt;T[]&lt;/i&gt; types.&lt;br /&gt;&lt;br /&gt;Please take a look at your design if you think you need to work with a collection of items. Often you can succeed by creating a composite type that can be injected. Take the following interface for instance:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}
&lt;/pre&gt;&lt;/div&gt;Instead of injecting a collection of dependencies, the consumer might not really be interested in the collection, but simply wishes to operate on all elements. In that scenario you can configure your container to inject a composite of that particular type. That composite might look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeLogger : ILogger
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger[] loggers;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeLogger(&lt;span style="color:Blue;"&gt;params&lt;/span&gt; ILogger[] loggers)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers = loggers ?? &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ILogger[0];
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; logger &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers)
        {
            logger.Log(message);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;A composite allows you to remove this boilerplate iteration logic from the application, which makes the application cleaner and when changes have to be made to the way the collection of loggers is processed, only the composite has to be changed.
&lt;h2&gt;Register multiple interfaces with the same implementation&lt;a name="Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;&lt;/a&gt;&lt;a name="Register_Multiple_Interfaces_With_The_Same_Implementation"&gt;&lt;/a&gt;&lt;/h2&gt;
To adhere to the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Interface%20Segregation%20Principle&amp;referringTitle=How-to"&gt;Interface Segregation Principle&lt;/a&gt;, it is important to keep interfaces narrow. Although in most situations implementations implement a single interface, it can sometimes be beneficial to have multiple interfaces on a single implementation. Here is an example of how to register this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Impl implements IInterface1, IInterface2 and IInterface3.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration =
    Lifestyle.Singleton.CreateRegistration&amp;lt;Impl, Impl&amp;gt;(container);

container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface1), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface2), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface3), registration);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; a = container.GetInstance&amp;lt;IInterface1&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; b = container.GetInstance&amp;lt;IInterface2&amp;gt;();

&lt;span style="color:Green;"&gt;// Since Impl is a singleton, both requests return the same instance.&lt;/span&gt;
Assert.AreEqual(a, b);
&lt;/pre&gt;&lt;/div&gt;The first line creates a &lt;i&gt;Registration&lt;/i&gt; instance for the &lt;i&gt;Impl&lt;/i&gt;, in this case with a singleton lifestyle. The other lines add this registration to the container, once for each interface. This maps multiple service types to the exact same registration.
&lt;h2&gt;Override existing registrations&lt;a name="Override-Existing-Registrations"&gt;&lt;/a&gt;&lt;a name="Override_Existing_Registrations"&gt;&lt;/a&gt;&lt;/h2&gt;The default behavior of the &lt;b&gt;Simple Injector&lt;/b&gt; is to fail when a service is registered for a second time. Most of the time the developer didn&amp;#39;t intend to override a previous registration and allowing this would lead to a configuration that would pass the container&amp;#39;s verification, but doesn&amp;#39;t behave as expected.&lt;br /&gt;&lt;br /&gt;There are certain scenarios however where overriding is useful. An example of such is a bootstrapper project for a Business layer that is reused in multiple applications (in both a web application, web service, and Windows service for instance). Not having a business layer specific bootstrapper project would mean the complete DI configuration would be duplicated in the startup path of each application, which would lead to code duplication. In that situation the applications would roughly have the same configuration, with a few adjustments.&lt;br /&gt;&lt;br /&gt;Best is to start of by configuring all possible dependencies in the BL bootstrapper and leave out the service registrations where the implementation differs for each application. In other words, the BL bootstrapper would result in an incomplete configuration. After that, each application can finish the configuration by registering the missing dependencies. This way you still don&amp;#39;t need to override the existing configuration.&lt;br /&gt;&lt;br /&gt;In certain scenarios it can be beneficial to allow an application override an existing configuration. The container can be configured to allow overriding as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Register IUserService.&lt;/span&gt;
container.Register&amp;lt;IUserService, FakeUserService&amp;gt;();

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous example created a &lt;i&gt;Container&lt;/i&gt; instance that allows overriding. It is also possible to enable overriding half way the registration process:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Create a container with overriding disabled&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Green;"&gt;// Pass container to the business layer.&lt;/span&gt;
BusinessLayer.Bootstrapper.Bootstrap(container);

&lt;span style="color:Green;"&gt;// Enable overriding&lt;/span&gt;
container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;h2&gt;Verify the container’s configuration&lt;a name="Verifying-Configuration"&gt;&lt;/a&gt;&lt;a name="Verifying_Configuration"&gt;&lt;/a&gt;&lt;/h2&gt;Dependency Injection promotes the concept of programming against abstractions. This makes your code much easier to test, easier to change and more maintainable. However, since the code itself isn&amp;#39;t responsible of maintaining the dependencies between implementations, the compiler will not be able to verify whether the dependency graph is correct.&lt;br /&gt;&lt;br /&gt;When starting to use a Dependency Injection container, many developers see their application fail when it is deployed in staging or sometimes even production, because of container misconfigurations. This makes developers often conclude that dependency injection is bad, since the dependency graph cannot be verified. This conclusion however, is incorrect. Although it is impossible for the compiler to verify the dependency graph, verifying the dependency graph is still possible and advisable.&lt;br /&gt;&lt;br /&gt;Simple Injector contains a &lt;b&gt;Verify()&lt;/b&gt; method, that will simply iterate over all registrations and resolve an instance for each registration. Calling this method directly after configuring the container, allows the application to fail during start-up, when the configuration is invalid.&lt;br /&gt;&lt;br /&gt;Calling the &lt;b&gt;Verify()&lt;/b&gt; method however, is just part of the story. It is very easy to create a configuration that passes any verification, but still fails at runtime. Here are some tips to help building a verifiable configuration:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Stay away from &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Implicit_Property_Injection"&gt;implicit property injection&lt;/a&gt;, where the container is allowed to skip injecting the property if a corresponding or correctly registered dependency can&amp;#39;t be found. This will disallow your application to fail fast and will result in &lt;b&gt;NullReferenceException&lt;/b&gt;s later on. Only use implicit property injection when the property is truly optional, omitting the dependency still keeps the configuration valid, and the application still runs correctly without that dependency. Truly optional dependencies should be very rare though, since most of the time you should prefer injecting empty implementations (a.k.a. the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) instead of allowing dependencies to be a null reference. &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=How-to&amp;ANCHOR#Configuring_Property_Injection"&gt;Explicit property injection&lt;/a&gt; on the other hand is fine. With explicit property injection you force the container to inject a property and it will fail when it can&amp;#39;t succeed. However, you should prefer constructor injection whenever possible. Note that the need for property injection is often an indication of problems in the design. If you revert to property injection because you otherwise have too many constructor arguments, you&amp;#39;re probably violating the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Register all root objects explicitly if possible. For instance, register all ASP.NET MVC Controller instances explicitly in the container (Controller instances are requested directly and are therefore called &amp;#39;root objects&amp;#39;). This way the container can check the complete dependency graph starting from the root object when you call &lt;b&gt;Verify()&lt;/b&gt;. Prefer registering all root objects in an automated fashion, for instance by using reflection to find all root types. The &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Web.Mvc"&gt;Simple Injector ASP.NET MVC Integration NuGet Package&lt;/a&gt; for instance, contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/M_SimpleInjector_SimpleInjectorMvcExtensions_RegisterMvcControllers.htm"&gt;RegisterMvcControllers&lt;/a&gt; extension method that will do this for you and the &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Wcf"&gt;WCF Integration NuGet Package&lt;/a&gt; contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary.v2/?topic=html/M_SimpleInjector_SimpleInjectorWcfExtensions_RegisterWcfServices.htm"&gt;RegisterWcfServices&lt;/a&gt; extension method for this purpose.&lt;/li&gt;
&lt;li&gt;If registering root objects is not possible or feasible, test the creation of each root object manually during start-up. With ASP.NET Web Form Page classes for instance, you will probably call the container from within their constructor (since Page classes must unfortunately have a default constructor). The key here again is finding them all in once using reflection. By finding all Page classes using reflection and instantiating them, you&amp;#39;ll find out (during app start-up or through automated testing) whether there is a problem with your DI configuration or not. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20Forms%20Integration&amp;referringTitle=How-to"&gt;Web Forms Integration&lt;/a&gt; guide contains an example of how to verify page classes.&lt;/li&gt;
&lt;li&gt;There are scenarios where some dependencies cannot yet be created during application start-up. To ensure that the application can be started normally and the rest of the DI configuration can still be verified, abstract those dependencies behind a proxy or abstract factory. Try to keep those unverifiable dependencies to a minimum and keep good track of them, because you will probably have to test them manually.&lt;/li&gt;
&lt;li&gt;But even when all registrations can be resolved succesfully by the container, that still doesn&amp;#39;t mean your configuration is correct. It is very easy to accidentally misconfigure the container in a way that only shows up late in the development process. &lt;b&gt;Simple Injector&lt;/b&gt; contains &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt; to help you spot common configuration mistakes. Use these diagnostic services and use them often.&lt;/li&gt;&lt;/ol&gt;
&lt;h2&gt;Work with dependency injection in multi-threaded applications&lt;a name="Multi-Threaded-Applications"&gt;&lt;/a&gt;&lt;a name="Multi_Threaded_Applications"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note:&lt;/b&gt; Simple Injector is designed for use in highly-concurrent applications. Its lock-free design allows it to scale linearly with the number of threads and processors in your system.&lt;/i&gt;&lt;/blockquote&gt;
Many applications and application frameworks are inherently multi-threaded. Working in multi-threaded applications forces developers to take special care. It is easy for a less experienced developer to introduce a race condition in the code. Even although some frameworks such as ASP.NET make it easy to write thread-safe code, introducing a simple static field could break thread-safety.&lt;br /&gt;&lt;br /&gt;This same holds when working with DI containers in multi-threaded applications. The developer that configures the container, should be aware of the risks of shared state. &lt;b&gt;Not knowing which configured services are thread-safe is a sin.&lt;/b&gt; Registering a service that is not thread-safe as singleton, will eventually lead to concurrency bugs, that usually only appear in production code. Those bugs are often hard to reproduce and hard to find, making them costly to fix. And even when you correctly configured a service with the correct lifestyle, when another component that depends on it accidentally as a longer lifetime, the service might be kept alive much longer and might even be accessible from other threads.&lt;br /&gt;&lt;br /&gt;Dependency injection however, can actually help in writing multi-threaded applications. Dependency injection forces you to wire all dependencies together in a single place in the application, the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/"&gt;Composition Root&lt;/a&gt;. This means that there is a single place in the application that knows about how services behave, whether they are thread-safe, and how they should be wired. Without this centralization, this knowledge would be scattered throughout the code base, making it very hard to change the behavior of a service.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Take a close look at the &amp;#39;Potential Lifestyle Mismatches&amp;#39; warnings in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt;. Lifestyle mismatches are a source of concurrency bugs.&lt;/i&gt;&lt;/blockquote&gt;
In a multi-threaded application, each thread should get its own object graph. This means that you should typically call &lt;b&gt;container.GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; once at the beginning of the thread to get the root object for processing that thread (or request). The container will build an object graph with all root object&amp;#39;s dependencies. Some of those dependencies will be singletons; shared between all threads. Other dependencies might be transient; a new instance is created per dependency. Other dependencies might be thread-specific, request-specific, or with some other lifestyle. The application code itself is unaware of the way the dependencies are registered and that&amp;#39;s the way it is supposed to be.&lt;br /&gt;&lt;br /&gt;For web applications, you typically call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; at the beginning of the web request. In an ASP.NET MVC application for instance, one Controller instance will be requested from the container (by the Controller Factory) per web request. When using one of the integration packages, such as the &lt;a href="https://nuget.org/packages/SimpleInjector.MVC3"&gt;Simple Injector MVC Integration Quick Start NuGet package&lt;/a&gt; for instance, you don&amp;#39;t have to call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; yourself, the package will ensure this is done for you. Still, &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; is typically called once per request.&lt;br /&gt;&lt;br /&gt;The advice of building a new object graph (calling &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt;) at the beginning of a thread, also holds when manually starting a new thread. Although you can pass on data to other threads, you should not pass on container controlled dependencies to other threads. On each new thread, you should ask the container again for the dependencies. When you start passing dependencies from one thread to the other, those parts of the code have to know whether it is safe to pass those dependencies on. For instance, are those dependencies thread-safe? This might be trivial to analyze in some situations, but prevents you to change those dependencies with other implementations, since now you have to remember that there is a place in your code where this is happening and you need to know which dependencies are passed on. You are decentralizing this knowledge again, making it harder to reason about the correctness of your DI configuration and making it easier to misconfigure the container in a way that causes those concurrency problems.&lt;br /&gt;&lt;br /&gt;Running code on a new thread can be done by adding a little bit of infrastructural code. Take for instance the following example where we want to send e-mail messages asynchronously. Instead of letting the caller implement this logic, it is better to hide the logic for asynchronicity behind an abstraction; a proxy. This ensures that this logic is centralized to a single place, and by placing this proxy inside the composition root, we prevent the application code to take a dependency on the container itself (which should be prevented).&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Synchronous implementation of IMailSender&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMailFormatter formatter;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender(IMailFormatter formatter)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.formatter = formatter;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// format mail&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// send mail&lt;/span&gt;
    }
}

&lt;span style="color:Green;"&gt;// Proxy for executing IMailSender asynchronously.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncMailSenderProxy : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMailSender&amp;gt; mailSenderFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncMailSenderProxy(ILogger logger,
        Func&amp;lt;IMailSender&amp;gt; mailSenderFactory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory = mailSenderFactory;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Run on a new thread&lt;/span&gt;
        Task.Factory.StartNew(() =&amp;gt;
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.SendMailAsync(to, message);
        });        
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SendMailAsync(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Here we run on a different thread and the&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// services should be requested on this thread.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; mailSender = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory();

        &lt;span style="color:Blue;"&gt;try&lt;/span&gt;
        {
            mailSender.SendMail(to, message);
        }
        &lt;span style="color:Blue;"&gt;catch&lt;/span&gt; (Exception ex)
        {
            &lt;span style="color:Green;"&gt;// logging is important, since we run on a&lt;/span&gt;
            &lt;span style="color:Green;"&gt;// different thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(ex);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;In the Composition Root, instead of registering the &lt;i&gt;MailSender&lt;/i&gt;, we register the &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;ILogger, FileLogger&amp;gt;(Lifestyle.Singleton);
container.Register&amp;lt;IMailSender, RealMailSender&amp;gt;();
container.RegisterSingleDecorator(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IMailSender),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncMailSenderProxy));
&lt;/pre&gt;&lt;/div&gt;In this case the container will ensure that when an &lt;i&gt;IMailSender&lt;/i&gt; is requested, a single &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; is returned with a &lt;i&gt;Func&amp;lt;IMailSender&amp;gt;&lt;/i&gt; delegate that will create a new &lt;i&gt;RealMailSender&lt;/i&gt; when requested. The &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; and &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterSingleDecorator.htm"&gt;RegisterSingleDecorator&lt;/a&gt; overloads natively understand how to handle &lt;i&gt;Func&amp;lt;Decoratee&amp;gt;&lt;/i&gt; dependencies. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Generic_Decorators"&gt;Decorators&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to"&gt;Advanced-scenarios&lt;/a&gt; wiki page explains more about registering decorators.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Fri, 17 May 2013 10:49:55 GMT</pubDate><guid isPermaLink="false">Updated Wiki: How-to 20130517104955A</guid></item><item><title>New Post: Register an Interface wich implementation's constructor has arguments</title><link>http://simpleinjector.codeplex.com/discussions/443987</link><description>&lt;div style="line-height: normal;"&gt;View Models are created by your presentation layer (most often your controller, presenter, page, or whatever your pattern you're using) and are filled with data from the model and possibly other sources. Since this View Model class (this data package) is created using (almost) all run-time values, in shouldn't delegate the creation to your DI container. Further more, in general you're not interested to abstract the View Model from your View or Controller, because it is simply a data package.&lt;br /&gt;
&lt;br /&gt;
If you still try this, you will find out very soon that this gets really awkward, and this holds not only when working with Simple Injector, but for all DI frameworks.&lt;br /&gt;
&lt;br /&gt;
But when we ignore the fact that you're dealing with a view model, you can register types with a string parameter as follows (excuse my C#):&lt;br /&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;ISomeService&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SomeServiceImpl(&lt;span style="color:#A31515;"&gt;&amp;quot;somePath&amp;quot;&lt;/span&gt;));
&lt;/pre&gt;&lt;/div&gt;And when you need to supply a runtime value, you can register &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to#Register-Factory-Delegates" rel="nofollow"&gt;a factory&lt;/a&gt;  or register a Func&amp;lt;T&amp;gt; delegate as follows:&lt;br /&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.RegisterSingle&amp;lt;Func&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, ISomeService&amp;gt;&amp;gt;(
    path =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SomeServiceImpl(path));
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Fri, 17 May 2013 08:37:58 GMT</pubDate><guid isPermaLink="false">New Post: Register an Interface wich implementation's constructor has arguments 20130517083758A</guid></item><item><title>New Post: Register an Interface wich implementation's constructor has arguments</title><link>http://simpleinjector.codeplex.com/discussions/443987</link><description>&lt;div style="line-height: normal;"&gt;Well I read the documentation but I couldn't find how to do this.&lt;br /&gt;
I've for example a class called &amp;quot;DocumentViewModel&amp;quot; wich constructor is something like:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;Public Sub New(ByVal filepath As String)
    _filepath = New FilePath(filepath)
    //more code...
End Sub&lt;/code&gt;&lt;/pre&gt;

How should I register this? And how should I implement this?&lt;br /&gt;
&lt;br /&gt;
Thanks in advance&lt;br /&gt;
&lt;/div&gt;</description><author>The_Chaoz</author><pubDate>Fri, 17 May 2013 01:51:38 GMT</pubDate><guid isPermaLink="false">New Post: Register an Interface wich implementation's constructor has arguments 20130517015138A</guid></item><item><title>New Post: Can Simple Injector support Web API and MVC Controllers simultaneously?</title><link>https://simpleinjector.codeplex.com/discussions/443293</link><description>&lt;div style="line-height: normal;"&gt;Thanks! - Adding the additional SetResolver call did the trick! Here is my final code:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;  public static class SimpleInjectorInitializer
    {
        /// &amp;lt;summary&amp;gt;Initialize the container and register it as MVC3 Dependency Resolver.&amp;lt;/summary&amp;gt;
        public static void Initialize()
        {
            var container = new Container();

            // register Web API controllers (important!)
            var controllerTypes =
                from type in Assembly.GetExecutingAssembly().GetExportedTypes()
                where typeof(IHttpController).IsAssignableFrom(type) // (typeof(IHttpController).IsAssignableFrom(type) || (typeof(IController).IsAssignableFrom(type) &amp;amp;&amp;amp; type.Name != &amp;quot;HelpController&amp;quot;))
                where !type.IsAbstract
                where !type.IsGenericTypeDefinition
                where type.Name.EndsWith(&amp;quot;Controller&amp;quot;, StringComparison.Ordinal)
                select type;

            foreach (var controllerType in controllerTypes)
            {
                container.Register(controllerType);
            }

            // Register your types, for instance:
            container.Register&amp;lt;IRepository, Repository&amp;gt;();

            // register MVC controllers (This is an extension method from the integration package).
            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

            // register MVC attributes (This is an extension method from the integration package as well).
            container.RegisterMvcAttributeFilterProvider();

            // Verify the container configuration
            container.Verify();

            System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver =
                new SimpleInjectorWebApiDependencyResolver(container);

            System.Web.Mvc.DependencyResolver.SetResolver(
                new SimpleInjector.Integration.Web.Mvc.SimpleInjectorDependencyResolver(container));

        }
    }&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;</description><author>Sergeir</author><pubDate>Mon, 13 May 2013 20:17:07 GMT</pubDate><guid isPermaLink="false">New Post: Can Simple Injector support Web API and MVC Controllers simultaneously? 20130513081707P</guid></item><item><title>New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm</title><link>https://simpleinjector.codeplex.com/discussions/443150</link><description>&lt;div style="line-height: normal;"&gt;An assembly is not the same as an NuGet package. In fact, the MVC Quick Start Package itself, does not contain an assembly. It contains a reference to three other NuGet packages (two Simple Injector packages and the WebActivator package) and one content file. &lt;br /&gt;
&lt;br /&gt;
Again, none of the dlls (assemblies) provided by Simple Injector reference the WebActivator. To integrate Simple Injector with MVC you don't need the MVC Quick Start package. The Quick Start is really just built to bootstrap everything together. If you don't want to use WebActivator, just deinstall the MVC Quick Start package and use the &lt;strong&gt;SimpleInjector.Integration.Web.Mvc&lt;/strong&gt; NuGet package directly. The assembly in that package depends on SimpleInjector.dll and SimpleInjector.Integration.Web.dll and those are located in the NuGet packages with the same name.&lt;br /&gt;
&lt;br /&gt;
SimpleInjector.Integration.Web.dll however, does not have a dependency on WebActivator. It only takes (besides SimpleInjector.dll) a dependency Microsoft.Web.Infrastructure.dll, which is part of the .NET framework.&lt;br /&gt;
&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Mon, 13 May 2013 18:21:13 GMT</pubDate><guid isPermaLink="false">New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm 20130513062113P</guid></item><item><title>New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm</title><link>http://simpleinjector.codeplex.com/discussions/443150</link><description>&lt;div style="line-height: normal;"&gt;The statement &amp;quot;none of the Simple Injector assemblies in fact have a dependency on WebActivator&amp;quot; seems to be incorrect.  I just tried to uninstall WebActivator after uninstalling the Quick Start NuGet package and get the following:&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Unable to uninstall 'WebActivator 1.4.4' because 'SimpleInjector.Integration.Web 2.2.3' depend(s) on it.&amp;quot;&lt;br /&gt;
&lt;/div&gt;</description><author>omegaluz</author><pubDate>Mon, 13 May 2013 15:54:28 GMT</pubDate><guid isPermaLink="false">New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm 20130513035428P</guid></item><item><title>New Post: Scenario questions about SI</title><link>http://simpleinjector.codeplex.com/discussions/443157</link><description>&lt;div style="line-height: normal;"&gt;That was all very helpful, thanks!&lt;br /&gt;
&lt;/div&gt;</description><author>androticus</author><pubDate>Sun, 12 May 2013 16:05:13 GMT</pubDate><guid isPermaLink="false">New Post: Scenario questions about SI 20130512040513P</guid></item><item><title>Updated Wiki: Home</title><link>https://simpleinjector.codeplex.com/wikipage?version=99</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;Welcome to the Simple Injector project&lt;/h1&gt;The Simple Injector is an easy-to-use Inversion of Control library for .NET and Silverlight.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http&amp;#58;&amp;#47;&amp;#47;nuget.org&amp;#47;packages&amp;#63;q&amp;#61;simpleinjector&amp;#38;sortOrder&amp;#61;package-download-count"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=305103" alt="Download&amp;#32;us&amp;#32;from&amp;#32;Nuget" title="Download&amp;#32;us&amp;#32;from&amp;#32;Nuget" /&gt;&lt;/a&gt;     &lt;a href="http&amp;#58;&amp;#47;&amp;#47;twitter.com&amp;#47;simpleinjector"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=305102" alt="Follow&amp;#32;us&amp;#32;on&amp;#32;twitter" title="Follow&amp;#32;us&amp;#32;on&amp;#32;twitter" /&gt;&lt;/a&gt;
&lt;h2&gt;Overview&lt;a name="Overview"&gt;&lt;/a&gt;&lt;/h2&gt;The goal of Simple Injector is to provide .NET application developers with an easy, flexible, and fast dependency injection framework, that uses best practices to steers developers towards the pit of success.&lt;br /&gt;&lt;br /&gt;Many of the existing IoC frameworks have a big complicated legacy API or are are new, immature, and lack the features do be used in large developement projects. &lt;b&gt;Simple Injector&lt;/b&gt; solves this problem by supplying a simple implementation with a very limited set of carefully chosen features in its core library. For example, file- and attribute-based configuration methods (that result in brittle and maintenance heavy applications) have been abandoned, favoring simple code-based configuration instead. This is enough for most applications, requiring only that configuration be performed at the start of the program. Although the API is limited, the core library does contain many features and allows almost any &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=Home"&gt;advanced scenario&lt;/a&gt;.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;You will need &lt;b&gt;.NET 4.0&lt;/b&gt; or up to use the &lt;b&gt;Simple Injector 2&lt;/b&gt; library. (For .NET 3.5 projects you can use the &lt;a href="https://simpleinjector.codeplex.com/releases/view/98963"&gt;Simple Injector v1.x&lt;/a&gt; releases).&lt;/li&gt;
&lt;li&gt;Simple Injector is carefully designed to run in &lt;i&gt;&lt;b&gt;partial trust&lt;/b&gt;&lt;/i&gt;.&lt;/li&gt;
&lt;li&gt;Simple Injector runs in &lt;i&gt;&lt;b&gt;Silverlight 4&lt;/b&gt;&lt;/i&gt; and up (Silverlight for Windows Phone is &lt;i&gt;&lt;b&gt;&lt;a href="https://simpleinjector.codeplex.com/discussions/285015"&gt;not&lt;/a&gt;&lt;/b&gt;&lt;/i&gt; supported).&lt;/li&gt;
&lt;li&gt;Simple Injector 2 runs on &lt;b&gt;Mono&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;It is fast; &lt;a href="http://simpleinjector.codeplex.com/discussions/326621"&gt;blazingly fast&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;New Diagnostic Services in Simple Injector 2&lt;/h2&gt;Simple Injector version 2 adds the ability to warn about common misconfigurations while debugging the application. Many Simple Injector users have already used this feature to diagnose their configuration and have successfully fixed problems that would be very hard to find otherwise. Here&amp;#39;s a quick example of how this might look in the debugger:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=635982" alt="Diagnostics&amp;#32;debugger&amp;#32;view&amp;#32;context&amp;#32;menu" title="Diagnostics&amp;#32;debugger&amp;#32;view&amp;#32;context&amp;#32;menu" /&gt;&lt;br /&gt;&lt;br /&gt;See the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=Home"&gt;Debug Diagnostic Services&lt;/a&gt; page for more information.&lt;br /&gt;
&lt;h2&gt;Getting started&lt;a name="Getting_started"&gt;&lt;/a&gt;&lt;/h2&gt;If you&amp;#39;re using &lt;a href="http://www.nuget.org/"&gt;NuGet&lt;/a&gt;, there are NuGet packages available. Take a look &lt;a href="http://nuget.org/packages?q=simpleinjector&amp;amp;sortOrder=package-download-count"&gt;here&lt;/a&gt;. Otherwise, follow the steps below, to start using the &lt;b&gt;Simple Injector&lt;/b&gt;:
&lt;ol&gt;&lt;li&gt;Go to the &lt;a href="http://simpleinjector.codeplex.com/releases/"&gt;Downloads&lt;/a&gt; tab and download the latest &lt;i&gt;runtime library&lt;/i&gt;;&lt;/li&gt;
&lt;li&gt;Unpack the downloaded .zip file;&lt;/li&gt;
&lt;li&gt;Add the &lt;i&gt;SimpleInjector.dll&lt;/i&gt; to your start-up project by right-clicking on a project in the Visual Studio solution explorer and selecting &amp;#39;Add Reference...&amp;#39;.&lt;/li&gt;
&lt;li&gt;Add the &lt;i&gt;using SimpleInjector;&lt;/i&gt; using directive on the top of the code file where you wish to configure the application.&lt;/li&gt;
&lt;li&gt;Look at the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Home"&gt;Usage&lt;/a&gt; section in the documentation on how to configure and use the &lt;b&gt;Simple Injector&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;Look at the &lt;a href="#More_information"&gt;More information&lt;/a&gt; section if you want to learn more or have any questions.&lt;/li&gt;&lt;/ol&gt;

&lt;h2&gt;A Quick Example&lt;a name="Example"&gt;&lt;/a&gt;&lt;/h2&gt;The general idea behind the &lt;b&gt;Simple Injector&lt;/b&gt; (or any DI framework for that matter) is that you design your application around loosely coupled components using the &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;dependency injection pattern&lt;/a&gt;. Take for instance the following &lt;i&gt;UserController&lt;/i&gt; class in the context of an ASP.NET MVC application:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: &lt;b&gt;Simple Injector&lt;/b&gt; works for many different technologies, not only for MVC. Please look at the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Home"&gt;Integration Guide&lt;/a&gt; for help using the &lt;b&gt;Simple Injector&lt;/b&gt; with your technology of choice.&lt;/i&gt;&lt;/blockquote&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; UserController : Controller {
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IUserRepository repository;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;

    &lt;span style="color:Green;"&gt;// Use constructor injection for the dependencies&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; UserService(IUserRepository rep, ILogger logger) {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.repository = rep;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
    }

    &lt;span style="color:Green;"&gt;// implement UserController methods here:&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ActionResult Index() {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(&lt;span style="color:#A31515;"&gt;&amp;quot;Index called&amp;quot;&lt;/span&gt;);
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; View(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;.rep.GetAll());
    }	
}
&lt;/pre&gt;&lt;/div&gt;The &lt;i&gt;UserController &lt;/i&gt; class depends on the &lt;i&gt;IUserRepository&lt;/i&gt; and &lt;i&gt;ILogger&lt;/i&gt; interfaces. By not depending on concrete implementations, we can test &lt;i&gt;UserController &lt;/i&gt; in isolation. Ease of testing is however, just the start of what Dependency Injection will accomplish. It allows us to design highly flexible systems that can completely be composed at a single place (often the startup path) in the application.&lt;br /&gt;&lt;br /&gt;Using the &lt;b&gt;Simple Injector&lt;/b&gt;, the configuration of the application using the &lt;i&gt;UserController&lt;/i&gt; class, could look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color:Blue;"&gt;object&lt;/span&gt; sender, EventArgs e) {
    &lt;span style="color:Green;"&gt;// 1. Create a new Simple Injector container&lt;/span&gt;
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

    &lt;span style="color:Green;"&gt;// 2. Configure the container (register)&lt;/span&gt;
    container.Register&amp;lt;IUserRepository, SqlUserRepository&amp;gt;();

    container.RegisterSingle&amp;lt;ILogger&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; CompositeLogger(
        container.GetInstance&amp;lt;DatabaseLogger&amp;gt;(),
        container.GetInstance&amp;lt;MailLogger&amp;gt;()
    ));

    &lt;span style="color:Green;"&gt;// 3. Optionally verify the container&amp;#39;s configuration.&lt;/span&gt;
    container.Verify();

    &lt;span style="color:Green;"&gt;// 4. Register the container as MVC3 IDependencyResolver.&lt;/span&gt;
    DependencyResolver.SetResolver(&lt;span style="color:Blue;"&gt;new&lt;/span&gt; SimpleInjectorDependencyResolver(container));
}
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: If you start with a MVC application, use the &lt;a href="http://nuget.org/packages/SimpleInjector.MVC3"&gt;NuGet Simple Injector MVC Integration Quick Start package&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;
The given configuration registers implementations for the &lt;i&gt;IUserRepository&lt;/i&gt; and &lt;i&gt;ILogger&lt;/i&gt; interfaces. The code snippet shows a few interesting things. First of all, you can map concrete instances (such as &lt;i&gt;SqlUserRepository&lt;/i&gt;) to an interface or base type. In the given example, every time you ask the container for an &lt;i&gt;IUserRepository&lt;/i&gt;, it will create a new &lt;i&gt;SqlUserRepository&lt;/i&gt; on your behalf (in DI terminology: an object with a &lt;i&gt;Transient&lt;/i&gt; lifestyle).&lt;br /&gt;&lt;br /&gt;The registration of the &lt;i&gt;ILogger&lt;/i&gt; is a bit more complex though. It registers a delegate that knows how to create a new &lt;i&gt;ILogger&lt;/i&gt; implementation, in this case &lt;i&gt;CompositeLogger&lt;/i&gt; (which is an implementation of &lt;i&gt;ILogger&lt;/i&gt;). The delegate itself calls back into the container and this allows the container to create the concrete &lt;i&gt;DatabaseLogger&lt;/i&gt; and &lt;i&gt;MailLogger&lt;/i&gt; and inject them into the &lt;i&gt;CompositeLogger&lt;/i&gt;. While the type of registration that we’ve seen with the &lt;i&gt;IUserRepository&lt;/i&gt; is much more common, the use of delegates allows many interesting scenarios.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: We did not register the &lt;/i&gt;UserController&lt;i&gt;, because the &lt;/i&gt;UserController&lt;i&gt; is a concrete type, &lt;b&gt;Simple Injector&lt;/b&gt; can implicitly create it (as long as its dependencies can be resolved).&lt;/i&gt;&lt;/blockquote&gt;
This is in fact all it takes to start using the &lt;b&gt;Simple Injector&lt;/b&gt;. Design your classes around the dependency injection principle (which is actually the hard part) and configure them in the top of your application. Some frameworks (such as ASP.NET MVC) will do the rest for you. Other frameworks (like ASP.NET Web Forms) will need a little bit more work to get this done. See the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Home"&gt;Integration Guide&lt;/a&gt; for examples of your framework of choice.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;Please go to the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Home"&gt;Using the Simple Injector&lt;/a&gt; section in the documentation to see more examples.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;More information&lt;a name="More_information"&gt;&lt;/a&gt;&lt;/h2&gt;For more information about the &lt;b&gt;Simple Injector&lt;/b&gt; please visit the following links: 
&lt;ul&gt;&lt;li&gt;&lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=Home"&gt;Using the Simple Injector&lt;/a&gt; will guide you through the &lt;b&gt;Simple Injector&lt;/b&gt; basics.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement&amp;referringTitle=Home"&gt;Simple Injector and object lifetime management&lt;/a&gt; page explains how to configure lifestyles such as &lt;i&gt;transient&lt;/i&gt;, &lt;i&gt;singleton&lt;/i&gt;, and many others.&lt;/li&gt;
&lt;li&gt;See the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/"&gt;Reference library&lt;/a&gt; for the complete API documentation.&lt;/li&gt;
&lt;li&gt;See the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Integration%20Guide&amp;referringTitle=Home"&gt;Integration Guide&lt;/a&gt; for more information about how to integrate the &lt;b&gt;Simple Injector&lt;/b&gt; into your specific application framework.&lt;/li&gt;
&lt;li&gt;For more information about &lt;i&gt;dependency injection&lt;/i&gt; in general, please visit &lt;a href="http://stackoverflow.com/tags/dependency-injection/info"&gt;this page on Stackoverflow&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;If you have any questions about how to use the &lt;b&gt;Simple Injector&lt;/b&gt; or about &lt;i&gt;dependency injection&lt;/i&gt; in general, the experts at &lt;a href="http://stackoverflow.com/questions/ask?tags=simple-injector ioc-container dependency-injection .net c%23&amp;amp;title=Simple%20Injector:"&gt;Stackoverflow.com&lt;/a&gt; are waiting for you.&lt;/li&gt;
&lt;li&gt;For all other &lt;b&gt;Simple Injector&lt;/b&gt; related question and discussions, such as bug reports and feature requests, the &lt;a href="http://simpleinjector.codeplex.com/discussions"&gt;Simple Injector discussion forum&lt;/a&gt; will be the place to start.&lt;/li&gt;&lt;/ul&gt;

&lt;h3&gt;Happy injecting!&lt;/h3&gt;
&lt;h2&gt;This project is supported by&lt;/h2&gt;&lt;a href="http&amp;#58;&amp;#47;&amp;#47;www.ncover.com"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=99602" alt="NCover" title="NCover" /&gt;&lt;/a&gt; &lt;a href="http&amp;#58;&amp;#47;&amp;#47;www.ndepend.com"&gt;&lt;img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=simpleinjector&amp;DownloadId=604581" alt="NDepend" title="NDepend" /&gt;&lt;/a&gt;
&lt;h2&gt;Simple Injector Twitter Feed&lt;a name="Twitter_Feed"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;sub&gt;&lt;div class="rss"&gt;&lt;div class="accentbar"&gt;&lt;span class="left"&gt;&amp;nbsp;&lt;/span&gt;Twitter / simpleinjector News Feed&lt;span class="right"&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/250305928434442241"&gt;simpleinjector: Hi @rossipedia. Please post a question on the http://t.co/9Q0ED2Yk discussion forum or stackoverflow http://t.co/L1eTCQoL with more info.&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Monday, September 24, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/249961931920793600"&gt;simpleinjector: RT @NuGetLatest: SimpleInjector.Integration.Wcf 1.5.0.12267 http://t.co/N4vkPAbs&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Sunday, September 23, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/249942026206998529"&gt;simpleinjector: #simpleinjector #WCF Integration Guide: http://t.co/5qSVQu1Z and #NuGet package: https://t.co/ceDMj7YI&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Sunday, September 23, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/249574764052611072"&gt;simpleinjector: #simpleinjector Integration Guide for http://t.co/s4u4dx9N Web API http://t.co/tGxmP95H&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Saturday, September 22, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="entry"&gt;&lt;div class="title"&gt;&lt;a href="http://twitter.com/simpleinjector/statuses/239480728390340608"&gt;simpleinjector: Just published a new patch release of  #SimpleInjector Extensions to #NuGet http://t.co/VTw9P23A Fixes a few bugs with RegisterOpenGeneric&lt;/a&gt;&lt;/div&gt;&lt;div class="moreinfo"&gt;&lt;span class="date"&gt;Saturday, August 25, 2012&lt;/span&gt; &amp;nbsp;|&amp;nbsp; &lt;span class="source"&gt;From &lt;a target="_blank" href="https://twitter.com/statuses/user_timeline/simpleinjector.rss"&gt;Twitter / simpleinjector&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="accentbar"&gt;&lt;span class="left"&gt;&amp;nbsp;&lt;/span&gt;Twitter / simpleinjector News Feed&lt;span class="right"&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/sub&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Sun, 12 May 2013 10:53:30 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20130512105330A</guid></item><item><title>New Post: Scenario questions about SI</title><link>https://simpleinjector.codeplex.com/discussions/443157</link><description>&lt;div style="line-height: normal;"&gt;Simple Injector does not allow resolving Lifetime Scoped instances outside the context of a scope. This design is deliberate. One of the earlier versions of the framework in fact behaved differently and followed the design of Autofac and other containers to return a singleton instance in the case there was no scope.&lt;br /&gt;
&lt;br /&gt;
This design however proved to be bad, since returning a singleton is almost always a bad idea, since components are often registered with Lifetime Scope because they are not thread-safe and should not be reused. A great example of this is the Unit of Work pattern that Entity Framework implements with its DbContext and ObjectContext classes. We saw users spinning off background threads and forget to start a new scope, which caused their Unit of Work class to be shared over multiple threads, which lead to errors in production.&lt;br /&gt;
&lt;br /&gt;
Because of this we decided to disallow this by default. Perhaps your case is different and it is okay to return a default instance. If you really want to do this, you can enable this by using a Hybrid lifestyle, as follows:&lt;br /&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; scopedLifestyleWithSingletonFallback = Lifestyle.CreateHybrid(
    () =&amp;gt; container.GetCurrentLifetimeScope() != &lt;span style="color:Blue;"&gt;null&lt;/span&gt;,
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; LifetimeScopeLifestyle(),
    Lifestyle.Singleton); &lt;span style="color:Green;"&gt;// the fallback lifestyle&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;But please take a look carefully at your design if this really is what you need and if it is possible for a programmer to make a mistake in such way that things accidentally are returned as singleton, while it should have been scoped. Since you are running a Windows Service that spins of new threads to do its work, it seems very unlikely that any code should even run outside the context of a lifetime scope.&lt;br /&gt;
&lt;br /&gt;
When you spin of a background thread and have any components registered as Lifetime Scope, you will have to wrap the execution with a lifetime scope. You will have to start the scope before requesting the root object, for instance:&lt;br /&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
Task.Factory.StartNew(() =&amp;gt;
{
    &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (container.BeginLifetimeScope())
    {
         &lt;span style="color:Green;"&gt;// resolve root object&lt;/span&gt;
         &lt;span style="color:Blue;"&gt;var&lt;/span&gt; service = container.GetInstance&amp;lt;ISomeService&amp;gt;();
         service.Do();
    }
}
&lt;/pre&gt;&lt;/div&gt;This code should be part of your application's &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/" rel="nofollow"&gt;Composition Root&lt;/a&gt; and not of any application code. This prevents the application to know anything about it. Of course the code cannot run without outside the context of a lifetime scope, but this is quite obvious. The fact that the consumer should now as little as possible doesn't mean it should be able to work in any case. Not starting a scope is a misconfiguration and you'd rather see the application fail than silently continue in error. Neither would you want the application to continue when one of your registrations is missing. You should always fail fast.&lt;br /&gt;
&lt;blockquote&gt;
I want the middleware to use the logging object that is for that specific service.&lt;br /&gt;
&lt;/blockquote&gt;
This seems more like &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios#Context-Based-Injection" rel="nofollow"&gt;Context Based Injection&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Lifetime Scoping in Simple Injector differs from the concept of Child Containers that frameworks such as Unity use. A child container allows you to do special registrations in that child container. Simple Injector does use this approach, since it is impossible to both allow creating child containers with new registrations and having good performance at the same time. If I understand correctly you want to register a new instance at the beginning of the thread and reuse that same instance for the duration of that scope. There are several ways to do this. Here is an idea:&lt;br /&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterLifetimeScope&amp;lt;LoggerContext&amp;gt;();

container.RegisterLifetimeScope&amp;lt;ILogger&amp;gt;(
    () =&amp;gt; container.GetInstance&amp;lt;LoggerContext&amp;gt;().Logger ?? 
        NullLogger.Instance);

&lt;span style="color:Green;"&gt;// This class is unknown to the application, only known in &lt;/span&gt;
&lt;span style="color:Green;"&gt;// the composition root.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; LoggerContext
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; ILogger Logger { &lt;span style="color:Blue;"&gt;get&lt;/span&gt;; &lt;span style="color:Blue;"&gt;set&lt;/span&gt;; }
}

&lt;span style="color:Green;"&gt;// Part of the Compostion root that starts a use case:&lt;/span&gt;
Task.Factory.StartNew(() =&amp;gt;
{
    &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (container.BeginLifetimeScope())
    {
        &lt;span style="color:Green;"&gt;// Set the logger to be used during this scope.&lt;/span&gt;
        container.GetInstance&amp;lt;LoggerContext&amp;gt;().Logger =
            &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Logger(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(ISomeService), &lt;span style="color:#A31515;"&gt;&amp;quot;special stuff&amp;quot;&lt;/span&gt;);
    
         &lt;span style="color:Green;"&gt;// resolve root object&lt;/span&gt;
         &lt;span style="color:Blue;"&gt;var&lt;/span&gt; service = container.GetInstance&amp;lt;ISomeService&amp;gt;();
         service.Do();
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Sat, 11 May 2013 10:18:25 GMT</pubDate><guid isPermaLink="false">New Post: Scenario questions about SI 20130511101825A</guid></item><item><title>New Post: Can Simple Injector support Web API and MVC Controllers simultaneously?</title><link>http://simpleinjector.codeplex.com/discussions/443293</link><description>&lt;div style="line-height: normal;"&gt;What is exactly the problem you are having?&lt;br /&gt;
&lt;br /&gt;
Although &lt;a href="http://stackoverflow.com/questions/15494920/what-is-the-difference-between-dependencyresolver-setresolver-and-httpconfigurat/15495934#15495934" rel="nofollow"&gt;not advised&lt;/a&gt;, integrating MVC and Web API in the same project is possible. But since both frameworks have their own API for dependency resolution, you will have to make sure that you register both the MVC DependencyResolver and the Web API DependencyResolver, like this:&lt;br /&gt;
&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver =
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SimpleInjectorWebApiDependencyResolver(container);
        
System.Web.Mvc.DependencyResolver.SetResolver(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; SimpleInjector.Integration.Web.Mvc.SimpleInjectorDependencyResolver(container));
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Sat, 11 May 2013 09:35:43 GMT</pubDate><guid isPermaLink="false">New Post: Can Simple Injector support Web API and MVC Controllers simultaneously? 20130511093543A</guid></item><item><title>New Post: Scenario questions about SI</title><link>http://simpleinjector.codeplex.com/discussions/443157</link><description>&lt;div style="line-height: normal;"&gt;Here is code for the Scoped Instance Scenario (I will provide the other scenario in another message). I think it is easiest with the concrete use- case, which is for a logging interface. We have a lot of code that is in windows services, where the code provides several different services running on different threads using different loggers. When I make a call to the middleware in a scoped instance from one of those threads, I want the middleware to use the logging object that is for that specific service. The example below is for some middleware service ISomeService that takes a ILogger which I want to supply. I want to set that instance of the Logger in the scope so that all DI resolutions within that scope will use that logger instance.&lt;br /&gt;
&lt;br /&gt;
//ctor for SomeService : ISomeService&lt;br /&gt;
&lt;br /&gt;
public SomeService(ILogger logger) {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// sample scenario:&lt;br /&gt;
Container container = new Container();&lt;br /&gt;
&lt;br /&gt;
// global registrations not shown&lt;br /&gt;
container.Register&amp;lt;ISomeService, SomeService&amp;gt;(new LifestyleScoped());&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
using (container.BeginLifetimeScope())&lt;br /&gt;
{&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;container.RegisterSingle&amp;lt;ILogger&amp;gt;(anExistingLoggerInstance); // THIS IS WHAT I WANT TO DO
&lt;/code&gt;&lt;/pre&gt;

...&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;//
ISomeService svc = container.GetInstance&amp;lt;ISomeService &amp;gt;(); // SHOULD GET CONSTRUCTED WITH THE INSTANCE I SET
&lt;/code&gt;&lt;/pre&gt;

}&lt;br /&gt;
&lt;/div&gt;</description><author>androticus</author><pubDate>Fri, 10 May 2013 21:34:45 GMT</pubDate><guid isPermaLink="false">New Post: Scenario questions about SI 20130510093445P</guid></item><item><title>New Post: Can Simple Injector support Web API and MVC Controllers simultaneously?</title><link>http://simpleinjector.codeplex.com/discussions/443293</link><description>&lt;div style="line-height: normal;"&gt;I have an MVC 4 ASP.NET Web API project in which I want to be able to inject a repository into both a MVC controller and an Web API controller. Obviously the project itself supports both types of controllers. I have found documentation on the Simple Injector site which shows how to implement either but not both at once.&lt;br /&gt;
&lt;br /&gt;
I have tried several methods, both of which can be seen in the code below, to no avail:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt; public static class SimpleInjectorInitializer
    {
        /// &amp;lt;summary&amp;gt;Initialize the container and register it as MVC3 Dependency Resolver.&amp;lt;/summary&amp;gt;
        public static void Initialize()
        {
            var container = new Container();

            // register Web API controllers (important!)
            var controllerTypes =
                from type in Assembly.GetExecutingAssembly().GetExportedTypes()
                where typeof(IHttpController).IsAssignableFrom(type) 
                where !type.IsAbstract
                where !type.IsGenericTypeDefinition
                where type.Name.EndsWith(&amp;quot;Controller&amp;quot;, StringComparison.Ordinal)
                select type;

            foreach (var controllerType in controllerTypes)
            {
                container.Register(controllerType);
            }

            // Register your types, for instance:
            container.Register&amp;lt;IRepository, Repository&amp;gt;();

            // register MVC controllers (This is an extension method from the integration package).
            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

            // register MVC controllers - second method (This is an extension method from the integration package).
            //var mvcControllerTypes =
            //    from type in SimpleInjectorMvcExtensions.GetControllerTypesToRegister(container, Assembly.GetExecutingAssembly())
            //    where type.Name != &amp;quot;HelpController&amp;quot;
            //    select type;

            //foreach (Type controllerType in mvcControllerTypes)
            //{
            //    container.Register(controllerType);
            //}

            // register MVC attributes (This is an extension method from the integration package as well).
            container.RegisterMvcAttributeFilterProvider();

            // Verify the container configuration
            container.Verify();

            // Register the dependency resolver.
            GlobalConfiguration.Configuration.DependencyResolver =
                new SimpleInjectorWebApiDependencyResolver(container);
        }
    }&lt;/code&gt;&lt;/pre&gt;

Here is the SimpleInjectorWebApiDependencyResolver calss taken from the Web API guide:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public sealed class SimpleInjectorWebApiDependencyResolver
    : IDependencyResolver
{
    private readonly Container container;

    public SimpleInjectorWebApiDependencyResolver(
        Container container)
    {
        this.container = container;
    }

    [DebuggerStepThrough]
    public IDependencyScope BeginScope()
    {
        return this;
    }

    [DebuggerStepThrough]
    public object GetService(Type serviceType)
    {
        return ((IServiceProvider)this.container)
            .GetService(serviceType);
    }

    [DebuggerStepThrough]
    public IEnumerable&amp;lt;object&amp;gt; GetServices(Type serviceType)
    {
        return this.container.GetAllInstances(serviceType);
    }

    [DebuggerStepThrough]
    public void Dispose()
    {
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;a href="http://simpleinjector.codeplex.com/wikipage?title=Web%20API%20Integration" rel="nofollow"&gt;http://simpleinjector.codeplex.com/wikipage?title=Web%20API%20Integration&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://simpleinjector.codeplex.com/wikipage?title=MVC%20Integration" rel="nofollow"&gt;http://simpleinjector.codeplex.com/wikipage?title=MVC%20Integration&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>Sergeir</author><pubDate>Fri, 10 May 2013 20:32:56 GMT</pubDate><guid isPermaLink="false">New Post: Can Simple Injector support Web API and MVC Controllers simultaneously? 20130510083256P</guid></item><item><title>Updated Wiki: How-to</title><link>https://simpleinjector.codeplex.com/wikipage?title=How-to&amp;version=31</link><description>&lt;div class="wikidoc"&gt;&lt;h1&gt;How To...&lt;/h1&gt;
&lt;ul&gt;&lt;li&gt;How to &lt;a href="#Register-Factory-Delegates"&gt;Register factory delegates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Instances-By-Key"&gt;Resolve instances by key&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Resolve-Arrays-And-Lists"&gt;Resolve array and lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;Register multiple interfaces with the same implementation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Override-Existing-Registrations"&gt;Override existing registrations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Verifying-Configuration"&gt;Verify the container&amp;#39;s configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;a href="#Multi-Threaded-Applications"&gt;Work with dependency injection in multi-threaded applications&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;Register factory delegates&lt;a name="Register-Factory-Delegates"&gt;&lt;/a&gt;&lt;a name="Register_Factory_Delegates"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;b&gt;Simple Injector&lt;/b&gt; allows you to register a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate for the creation of an instance. This is especially useful in scenarios where it is impossible for the container to create the instance. There are overloads of the &lt;i&gt;Register&lt;/i&gt; method available that accept an &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; argument:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;IMyService&amp;gt;(() =&amp;gt; SomeSubSystem.CreateMyService());
&lt;/pre&gt;&lt;/div&gt;In situations where a service needs to create multiple instances of a certain dependencies, or needs to explicitly control the lifetime of such dependency, abstract factories can be used. Instead of injecting an &lt;i&gt;IMyService&lt;/i&gt;, you should inject an &lt;i&gt;IMyServiceFactory&lt;/i&gt; that allows services to create new instances of &lt;i&gt;IMyService&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IMyServiceFactory
{
    IMyService CreateNew();
}

&lt;span style="color:Green;"&gt;// Implementation&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; ServiceFactory : IMyServiceFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IMyService CreateNew()
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;IMyServiceFactory, ServiceFactory&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMyServiceFactory factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(IMyServiceFactory factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
    	{
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }

        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service2 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.CreateNew())
    	{
            &lt;span style="color:Green;"&gt;// use service 2&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Instead of creating specific interfaces for your factories, you can also choose to inject &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates into your services:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterSingle&amp;lt;Func&amp;lt;IMyService&amp;gt;&amp;gt;(
    () =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; MyServiceImpl());

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMyService&amp;gt; factory;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Func&amp;lt;IMyService&amp;gt; factory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory = factory;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; service1 = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.factory.Invoke())
    	{
            &lt;span style="color:Green;"&gt;// use service 1&lt;/span&gt;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;This saves you from having to define a new interface and implementation per factory.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: On the downside however, this communicates less clearly the intend of your code and as a result might make your code harder to grasp for other developers.&lt;/i&gt;&lt;/blockquote&gt;
When you choose &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates over specific factory interfaces, you can define the following extension method that makes it easier to register &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; factories:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;TService, TImplementation&amp;gt;(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container,
    Lifestyle lifestyle = &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    lifestyle = lifestyle ?? Lifestyle.Transient;

    &lt;span style="color:Green;"&gt;// Register the type explicitly to keep the configuration&lt;/span&gt;
    &lt;span style="color:Green;"&gt;// verifiable.&lt;/span&gt;
    container.Register&amp;lt;TService, TImplementation&amp;gt;(lifestyle);
	
    &lt;span style="color:Green;"&gt;// Register the Func&amp;lt;T&amp;gt; that resolves that instance.&lt;/span&gt;
    container.RegisterSingle&amp;lt;Func&amp;lt;TService&amp;gt;&amp;gt;(
        () =&amp;gt; container.GetInstance&amp;lt;TService&amp;gt;());
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterFuncFactory&amp;lt;IMyService, RealService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous extension method allowed registration of a single factory, but won&amp;#39;t be maintainable when you want all registrations to be resolvable using &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates by default. &lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: We personally think that allowing to register Func&amp;lt;T&amp;gt; delegates by default is a design smell. The use of Func&amp;lt;T&amp;gt; delegates makes your design harder to follow and your system harder to maintain and test. If you have many constructors in your system that depend on a Func&amp;lt;T&amp;gt;, please take a good look at your dependency strategy. If in doubt, please ask us here on CodePlex or on Stackoverflow.&lt;/i&gt;&lt;/blockquote&gt;
The following extension method allows Simple Injector to resolve all types using a &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegate by default:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;static&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; AllowResolvingFuncFactories(
    &lt;span style="color:Blue;"&gt;this&lt;/span&gt; ContainerOptions options)
{
    &lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = options.Container;

    container.ResolveUnregisteredType += (sender, e) =&amp;gt;
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (e.UnregisteredServiceType.IsGenericType &amp;amp;&amp;amp;
            e.UnregisteredServiceType.GetGenericTypeDefinition() ==
                &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;))
        {
            Type serviceType = e.UnregisteredServiceType
                .GetGenericArguments().First();

            InstanceProducer registration =
                container.GetRegistration(serviceType);

            &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (registration != &lt;span style="color:Blue;"&gt;null&lt;/span&gt;)
            {
                Type funcType =
                    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(Func&amp;lt;&amp;gt;).MakeGenericType(serviceType);

                &lt;span style="color:Blue;"&gt;var&lt;/span&gt; factoryDelegate = 
                    Expression.Lambda(funcType,
                        registration.BuildExpression())
                    .Compile();

                e.Register(Expression.Constant(factoryDelegate));
            }
        }
    };
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.Options.AllowResolvingFuncFactories();
&lt;/pre&gt;&lt;/div&gt;After calling the &lt;b&gt;AllowResolvingFuncFactories&lt;/b&gt; extension method, the container allows resolving &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates.&lt;br /&gt;&lt;br /&gt;&lt;a name="lazy"&gt;&lt;/a&gt;Just like &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates can be injected, &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; instances can be injected into services. &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; is useful in situations where the creation of a service is time consuming, but is not always used. Thus &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; allows you to postpone the creation of that service until the moment that it is really needed:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Extension method&lt;/span&gt;
container.RegisterLazy&amp;lt;T&amp;gt;(&lt;span style="color:Blue;"&gt;this&lt;/span&gt; Container container)
    &lt;span style="color:Blue;"&gt;where&lt;/span&gt; T : &lt;span style="color:Blue;"&gt;class&lt;/span&gt;
{
    Func&amp;lt;T&amp;gt; factory = () =&amp;gt; container.GetInstance&amp;lt;T&amp;gt;();

    container.Register&amp;lt;Lazy&amp;lt;T&amp;gt;&amp;gt;(() =&amp;gt; &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Lazy&amp;lt;T&amp;gt;(factory));
}

&lt;span style="color:Green;"&gt;// Registration    &lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; myService;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyService(Lazy&amp;lt;IMyService&amp;gt; myService)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService = myService;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SomeOperation()
    {
        &lt;span style="color:Blue;"&gt;if&lt;/span&gt; (someCondition)
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.myService.Value.Operate();
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;Note that instead of polluting the API of your application with &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; dependencies, it is usually cleaner to hide the &lt;i&gt;Lazy&amp;lt;T&amp;gt;&lt;/i&gt; behind a proxy:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Proxy definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; MyLazyServiceProxy : IMyService
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Lazy&amp;lt;IMyService&amp;gt; wrapped;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; MyLazyServiceProxy(Lazy&amp;lt;IMyService&amp;gt; wrapped)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped = wrapped;
    }
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Operate()
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.wrapped.Value.Operate();
    }
}

&lt;span style="color:Green;"&gt;// Registration&lt;/span&gt;
container.RegisterLazy&amp;lt;IMyService&amp;gt;();
container.Register&amp;lt;IMyService, MyLazyServiceProxy&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;This way the application can simply depend on &lt;i&gt;IMyService&lt;/i&gt; instead of &lt;i&gt;Lazy&amp;lt;IMyService&amp;gt;&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Warning&lt;/b&gt;: The same warning applies to the use of Lazy&amp;lt;T&amp;gt; as it does for the use of Func&amp;lt;T&amp;gt; delegates. For more information about creating an application and container configuration that can be successfully verified, please read the &lt;a href="#Verifying-Configuration"&gt;How To Verify the container&amp;#8217;s configuration&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve instances by key&lt;a name="Resolve-Instances-By-Key"&gt;&lt;/a&gt;&lt;a name="Resolve_Instances_By_Key"&gt;&lt;/a&gt;&lt;/h2&gt;Resolving instances by a key is a feature that is deliberately left out of the &lt;b&gt;Simple Injector&lt;/b&gt;, because we feel it steers developers to a design were the application tends to have a dependency on the DI container itself. For resolving a keyed instance, you will often need to call into the container itself, which leads to the &lt;a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx"&gt;Service Locator anti-pattern&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This doesn’t mean however, that resolving instances by a key is never useful. Resolving instances by a key however, is much more a job for a specific factory than for a container itself. This makes the design much cleaner, saves you from having to take a dependency on the DI framework, and enables many scenarios that the DI container writer just didn’t consider.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: The need for keyed registration can be an indication of ambiguity in the application design. Take a good look if each keyed registration shouldn&amp;#39;t have its own unique interface, or perhaps each registration should implement each own version of a generic interface.&lt;/i&gt;&lt;/blockquote&gt;
Take a look at the following scenario, where we want to retrieve instances of type &lt;i&gt;IRequestHandler&lt;/i&gt; by a string key. There are of course several ways to achieve this, but here is a simple but effective way, by defining an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt;:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Definition&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; IRequestHandlerFactory
{
    IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name);
}

&lt;span style="color:Green;"&gt;// Usage&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; factory =
    container.GetInstance&amp;lt;IRequestHandlerFactory&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = factory.CreateNew(&lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;);
handler.Handle(requestContext);
&lt;/pre&gt;&lt;/div&gt;By inheriting from the BCL’s &lt;i&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/i&gt;, creating an &lt;i&gt;IRequestHandlerFactory&lt;/i&gt; implementation, is almost a one-liner:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Func&amp;lt;IRequestHandler&amp;gt;&amp;gt;,
    IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]();
    }
}
&lt;/pre&gt;&lt;/div&gt;With this class, we can register &lt;i&gt;Func&amp;lt;IRequestHandler&amp;gt;&lt;/i&gt; factory methods by a key. With this in place the registration of keyed instances is a breeze:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();
 
container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;DefaultRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;OrdersRequestHandler&amp;gt;() },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, () =&amp;gt; container.GetInstance&amp;lt;CustomersRequestHandler&amp;gt;() },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: this design will work with almost all DI containers, making this not only a design that is easy to follow, but making it very easy to migrate to another DI implementation.&lt;/i&gt;&lt;/blockquote&gt;
If you don’t like a design that uses &lt;i&gt;Func&amp;lt;T&amp;gt;&lt;/i&gt; delegates this way, it can easily be changed to be a &lt;b&gt;Dictionary&amp;lt;string, Type&amp;gt;&lt;/b&gt; instead. The &lt;i&gt;RequestHandlerFactory&lt;/i&gt; can be implemented as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RequestHandlerFactory
    : Dictionary&amp;lt;&lt;span style="color:Blue;"&gt;string&lt;/span&gt;, Type&amp;gt;, IRequestHandlerFactory
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Container container;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; RequestHandlerFactory(Container container)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container = container;
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; IRequestHandler CreateNew(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; name)
    {
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; handler = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.container.GetInstance(&lt;span style="color:Blue;"&gt;this&lt;/span&gt;[name]);
        &lt;span style="color:Blue;"&gt;return&lt;/span&gt; (IRequestHandler)handler;
    }
}
&lt;/pre&gt;&lt;/div&gt;The registration will then look as follows:&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.RegisterSingle&amp;lt;IRequestHandlerFactory&amp;gt;(
    &lt;span style="color:Blue;"&gt;new&lt;/span&gt; RequestHandlerFactory(container)
{
    { &lt;span style="color:#A31515;"&gt;&amp;quot;default&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(DefaultRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;orders&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(OrdersRequestHandler) },
    { &lt;span style="color:#A31515;"&gt;&amp;quot;customers&amp;quot;&lt;/span&gt;, &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(CustomersRequestHandler) },
});
&lt;/pre&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;: Please remember the previous note about ambiguity in the application design. In the given example the design would probably be better af by using a generic &lt;b&gt;IRequestHandler&amp;lt;TRequest&amp;gt;&lt;/b&gt; interface. This would allow the implementations to be &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Batch_Registration"&gt;batch registered using a single line of code&lt;/a&gt;, saves you from using keys, and results in a configuration the is &lt;a href="#Verifying-Configuration"&gt;verifiable by the container&lt;/a&gt;.&lt;/i&gt;&lt;/blockquote&gt;&lt;h2&gt;Resolve array and lists&lt;a name="Resolve-Arrays-And-Lists"&gt;&lt;/a&gt;&lt;a name="Resolve_Arrays_And_Lists"&gt;&lt;/a&gt;&lt;/h2&gt;The &lt;b&gt;Simple Injector&lt;/b&gt; allows registration of collections of elements using the &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Container_RegisterAll.htm"&gt;RegisterAll&lt;/a&gt; method overloads. Collections can be resolved by using one of the &lt;b&gt;GetAllInstances&amp;lt;T&amp;gt;()&lt;/b&gt; method, by calling &lt;b&gt;GetInstance&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;()&lt;/b&gt;, or by defining an &lt;i&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/i&gt; parameter in the constructor of a type that is created using automatic constructor injection.&lt;br /&gt;&lt;br /&gt;Injection of other collection types, such as &lt;i&gt;arrays of T&lt;/i&gt; or &lt;i&gt;IList&amp;lt;T&amp;gt;&lt;/i&gt; into constructors is not supported out of the box. By hooking onto the unregistered type resolution event however, this functionality can be added. Look &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions&amp;referringTitle=How-to"&gt;here&lt;/a&gt; for an example extension method that allows this behavior for &lt;i&gt;T[]&lt;/i&gt; types.&lt;br /&gt;&lt;br /&gt;Please take a look at your design if you think you need to work with a collection of items. Often you can succeed by creating a composite type that can be injected. Take the following interface for instance:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;interface&lt;/span&gt; ILogger
{
    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message);
}
&lt;/pre&gt;&lt;/div&gt;Instead of injecting a collection of dependencies, the consumer might not really be interested in the collection, but simply wishes to operate on all elements. In that scenario you can configure your container to inject a composite of that particular type. That composite might look as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; CompositeLogger : ILogger
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger[] loggers;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; CompositeLogger(&lt;span style="color:Blue;"&gt;params&lt;/span&gt; ILogger[] loggers)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers = loggers ?? &lt;span style="color:Blue;"&gt;new&lt;/span&gt; ILogger[0];
    }

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; Log(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:Blue;"&gt;var&lt;/span&gt; logger &lt;span style="color:Blue;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.loggers)
        {
            logger.Log(message);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;A composite allows you to remove this boilerplate iteration logic from the application, which makes the application cleaner and when changes have to be made to the way the collection of loggers is processed, only the composite has to be changed.
&lt;h2&gt;Register multiple interfaces with the same implementation&lt;a name="Register-Multiple-Interfaces-With-The-Same-Implementation"&gt;&lt;/a&gt;&lt;a name="Register_Multiple_Interfaces_With_The_Same_Implementation"&gt;&lt;/a&gt;&lt;/h2&gt;
To adhere to the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Interface%20Segregation%20Principle&amp;referringTitle=How-to"&gt;Interface Segregation Principle&lt;/a&gt;, it is important to keep interfaces narrow. Although in most situations implementations implement a single interface, it can sometimes be beneficial to have multiple interfaces on a single implementation. Here is an example of how to register this:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Impl implements IInterface1, IInterface2 and IInterface3.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; registration =
    Lifestyle.Singleton.CreateRegistration&amp;lt;Impl, Impl&amp;gt;(container);

container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface1), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface2), registration);
container.AddRegistration(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IInterface3), registration);

&lt;span style="color:Blue;"&gt;var&lt;/span&gt; a = container.GetInstance&amp;lt;IInterface1&amp;gt;();
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; b = container.GetInstance&amp;lt;IInterface2&amp;gt;();

&lt;span style="color:Green;"&gt;// Since Impl is a singleton, both requests return the same instance.&lt;/span&gt;
Assert.AreEqual(a, b);
&lt;/pre&gt;&lt;/div&gt;The first line creates a &lt;i&gt;Registration&lt;/i&gt; instance for the &lt;i&gt;Impl&lt;/i&gt;, in this case with a singleton lifestyle. The other lines add this registration to the container, once for each interface. This maps multiple service types to the exact same registration.
&lt;h2&gt;Override existing registrations&lt;a name="Override-Existing-Registrations"&gt;&lt;/a&gt;&lt;a name="Override_Existing_Registrations"&gt;&lt;/a&gt;&lt;/h2&gt;The default behavior of the &lt;b&gt;Simple Injector&lt;/b&gt; is to fail when a service is registered for a second time. Most of the time the developer didn&amp;#39;t intend to override a previous registration and allowing this would lead to a configuration that would pass the container&amp;#39;s verification, but doesn&amp;#39;t behave as expected.&lt;br /&gt;&lt;br /&gt;There are certain scenarios however where overriding is useful. An example of such is a bootstrapper project for a Business layer that is reused in multiple applications (in both a web application, web service, and Windows service for instance). Not having a business layer specific bootstrapper project would mean the complete DI configuration would be duplicated in the startup path of each application, which would lead to code duplication. In that situation the applications would roughly have the same configuration, with a few adjustments.&lt;br /&gt;&lt;br /&gt;Best is to start of by configuring all possible dependencies in the BL bootstrapper and leave out the service registrations where the implementation differs for each application. In other words, the BL bootstrapper would result in an incomplete configuration. After that, each application can finish the configuration by registering the missing dependencies. This way you still don&amp;#39;t need to override the existing configuration.&lt;br /&gt;&lt;br /&gt;In certain scenarios it can be beneficial to allow an application override an existing configuration. The container can be configured to allow overriding as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Register IUserService.&lt;/span&gt;
container.Register&amp;lt;IUserService, FakeUserService&amp;gt;();

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;The previous example created a &lt;i&gt;Container&lt;/i&gt; instance that allows overriding. It is also possible to enable overriding half way the registration process:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Create a container with overriding disabled&lt;/span&gt;
&lt;span style="color:Blue;"&gt;var&lt;/span&gt; container = &lt;span style="color:Blue;"&gt;new&lt;/span&gt; Container();

&lt;span style="color:Green;"&gt;// Pass container to the business layer.&lt;/span&gt;
BusinessLayer.Bootstrapper.Bootstrap(container);

&lt;span style="color:Green;"&gt;// Enable overriding&lt;/span&gt;
container.Options.AllowOverridingRegistrations = &lt;span style="color:Blue;"&gt;true&lt;/span&gt;;

&lt;span style="color:Green;"&gt;// Replaces the previous registration&lt;/span&gt;
container.Register&amp;lt;IUserService, RealUserService&amp;gt;();
&lt;/pre&gt;&lt;/div&gt;&lt;h2&gt;Verify the container’s configuration&lt;a name="Verifying-Configuration"&gt;&lt;/a&gt;&lt;a name="Verifying_Configuration"&gt;&lt;/a&gt;&lt;/h2&gt;Dependency Injection promotes the concept of programming against abstractions. This makes your code much easier to test, easier to change and more maintainable. However, since the code itself isn&amp;#39;t responsible of maintaining the dependencies between implementations, the compiler will not be able to verify whether the dependency graph is correct.&lt;br /&gt;&lt;br /&gt;When starting to use a Dependency Injection container, many developers see their application fail when it is deployed in staging or sometimes even production, because of container misconfigurations. This makes developers often conclude that dependency injection is bad, since the dependency graph cannot be verified. This conclusion however, is incorrect. Although it is impossible for the compiler to verify the dependency graph, verifying the dependency graph is still possible and advisable.&lt;br /&gt;&lt;br /&gt;Simple Injector contains a &lt;b&gt;Verify()&lt;/b&gt; method, that will simply iterate over all registrations and resolve an instance for each registration. Calling this method directly after configuring the container, allows the application to fail during start-up, when the configuration is invalid.&lt;br /&gt;&lt;br /&gt;Calling the &lt;b&gt;Verify()&lt;/b&gt; method however, is just part of the story. It is very easy to create a configuration that passes any verification, but still fails at runtime. Here are some tips to help building a verifiable configuration:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Stay away from &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Implicit_Property_Injection"&gt;implicit property injection&lt;/a&gt;, where the container is allowed to skip injecting the property if a corresponding or correctly registered dependency can&amp;#39;t be found. This will disallow your application to fail fast and will result in &lt;b&gt;NullReferenceException&lt;/b&gt;s later on. Only use implicit property injection when the property is truly optional, omitting the dependency still keeps the configuration valid, and the application still runs correctly without that dependency. Truly optional dependencies should be very rare though, since most of the time you should prefer injecting empty implementations (a.k.a. the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object pattern&lt;/a&gt;) instead of allowing dependencies to be a null reference. &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Using%20the%20Simple%20Injector&amp;referringTitle=How-to&amp;ANCHOR#Configuring_Property_Injection"&gt;Explicit property injection&lt;/a&gt; on the other hand is fine. With explicit property injection you force the container to inject a property and it will fail when it can&amp;#39;t succeed. However, you should prefer constructor injection whenever possible. Note that the need for property injection is often an indication of problems in the design. If you revert to property injection because you otherwise have too many constructor arguments, you&amp;#39;re probably violating the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;Single Responsibility Principle&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Register all root objects explicitly if possible. For instance, register all ASP.NET MVC Controller instances explicitly in the container (Controller instances are requested directly and are therefore called &amp;#39;root objects&amp;#39;). This way the container can check the complete dependency graph starting from the root object when you call &lt;b&gt;Verify()&lt;/b&gt;. Prefer registering all root objects in an automated fashion, for instance by using reflection to find all root types. The &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Web.Mvc"&gt;Simple Injector ASP.NET MVC Integration NuGet Package&lt;/a&gt; for instance, contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/M_SimpleInjector_SimpleInjectorMvcExtensions_RegisterMvcControllers.htm"&gt;RegisterMvcControllers&lt;/a&gt; extension method that will do this for you and the &lt;a href="http://nuget.org/packages/SimpleInjector.Integration.Wcf"&gt;WCF Integration NuGet Package&lt;/a&gt; contains a &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary.v2/?topic=html/M_SimpleInjector_SimpleInjectorWcfExtensions_RegisterWcfServices.htm"&gt;RegisterWcfServices&lt;/a&gt; extension method for this purpose.&lt;/li&gt;
&lt;li&gt;If registering root objects is not possible or feasible, test the creation of each root object manually during start-up. With ASP.NET Web Form Page classes for instance, you will probably call the container from within their constructor (since Page classes must unfortunately have a default constructor). The key here again is finding them all in once using reflection. By finding all Page classes using reflection and instantiating them, you&amp;#39;ll find out (during app start-up or through automated testing) whether there is a problem with your DI configuration or not. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Web%20Forms%20Integration&amp;referringTitle=How-to"&gt;Web Forms Integration&lt;/a&gt; guide contains an example of how to verify page classes.&lt;/li&gt;
&lt;li&gt;There are scenarios where some dependencies cannot yet be created during application start-up. To ensure that the application can be started normally and the rest of the DI configuration can still be verified, abstract those dependencies behind a proxy or abstract factory. Try to keep those unverifiable dependencies to a minimum and keep good track of them, because you will probably have to test them manually.&lt;/li&gt;
&lt;li&gt;But even when all registrations can be resolved succesfully by the container, that still doesn&amp;#39;t mean your configuration is correct. It is very easy to accidentally misconfigure the container in a way that only shows up late in the development process. &lt;b&gt;Simple Injector&lt;/b&gt; contains &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt; to help you spot common configuration mistakes. Use these diagnostic services and use them often.&lt;/li&gt;&lt;/ol&gt;
&lt;h2&gt;Work with dependency injection in multi-threaded applications&lt;a name="Multi-Threaded-Applications"&gt;&lt;/a&gt;&lt;a name="Multi_Threaded_Applications"&gt;&lt;/a&gt;&lt;/h2&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Note:&lt;/b&gt; Simple Injector is designed for use in highly-concurrent applications. Its lock-free design allows it to scale linearly with the number of threads and processors in your system.&lt;/i&gt;&lt;/blockquote&gt;
Many applications and application frameworks are inherently multi-threaded. Working in multi-threaded applications forces developers to take special care. It is easy for a less experienced developer to introduce a race condition in the code. Even although some frameworks such as ASP.NET make it easy to write thread-safe code, introducing a simple static field could break thread-safety.&lt;br /&gt;&lt;br /&gt;This same holds when working with DI containers in multi-threaded applications. The developer that configures the container, should be aware of the risks of shared state. &lt;b&gt;Not knowing which configured services are thread-safe is a sin.&lt;/b&gt; Registering a service that is not thread-safe as singleton, will eventually lead to concurrency bugs, that usually only appear in production code. Those bugs are often hard to reproduce and hard to find, making them costly to fix. And even when you correctly configured a service with the correct lifestyle, when another component that depends on it accidentally as a longer lifetime, the service might be kept alive much longer and might even be accessible from other threads.&lt;br /&gt;&lt;br /&gt;Dependency injection however, can actually help in writing multi-threaded applications. Dependency injection forces you to wire all dependencies together in a single place in the application, the &lt;a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot/"&gt;Composition Root&lt;/a&gt;. This means that there is a single place in the application that knows about how services behave, whether they are thread-safe, and how they should be wired. Without this centralization, this knowledge would be scattered throughout the code base, making it very hard to change the behavior of a service.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;&lt;b&gt;Tip&lt;/b&gt;: Take a close look at the &amp;#39;Potential Lifestyle Mismatches&amp;#39; warnings in the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Diagnostics&amp;referringTitle=How-to"&gt;Debugger Diagnostics&lt;/a&gt;. Lifestyle mismatches are a source of concurrency bugs.&lt;/i&gt;&lt;/blockquote&gt;
In a multi-threaded application, each thread should get its own object graph. This means that you should typically call &lt;b&gt;container.GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; once at the beginning of the thread to get the root object for processing that thread (or request). The container will build an object graph with all root object&amp;#39;s dependencies. Some of those dependencies will be singletons; shared between all threads. Other dependencies might be transient; a new instance is created per dependency. Other dependencies might be thread-specific, request-specific, or with some other lifestyle. The application code itself is unaware of the way the dependencies are registered and that&amp;#39;s the way it is supposed to be.&lt;br /&gt;&lt;br /&gt;For web applications, you typically call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; at the beginning of the web request. In an ASP.NET MVC application for instance, one Controller instance will be requested from the container (by the Controller Factory) per web request. When using one of the integration packages, such as the &lt;a href="https://nuget.org/packages/SimpleInjector.MVC3"&gt;Simple Injector MVC Integration Quick Start NuGet package&lt;/a&gt; for instance, you don&amp;#39;t have to call &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; yourself, the package will ensure this is done for you. Still, &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt; is typically called once per request.&lt;br /&gt;&lt;br /&gt;The advice of building a new object graph (calling &lt;b&gt;GetInstance&amp;lt;T&amp;gt;()&lt;/b&gt;) at the beginning of a thread, also holds when manually starting a new thread. Although you can pass on data to other threads, you should not pass on container controlled dependencies to other threads. On each new thread, you should ask the container again for the dependencies. When you start passing dependencies from one thread to the other, those parts of the code have to know whether it is safe to pass those dependencies on. For instance, are those dependencies thread-safe? This might be trivial to analyze in some situations, but prevents you to change those dependencies with other implementations, since now you have to remember that there is a place in your code where this is happening and you need to know which dependencies are passed on. You are decentralizing this knowledge again, making it harder to reason about the correctness of your DI configuration and making it easier to misconfigure the container in a way that causes those concurrency problems.&lt;br /&gt;&lt;br /&gt;Running code on a new thread can be done by adding a little bit of infrastructural code. Take for instance the following example where we want to send e-mail messages asynchronously. Instead of letting the caller implement this logic, it is better to hide the logic for asynchronicity behind an abstraction; a proxy. This ensures that this logic is centralized to a single place, and by placing this proxy inside the composition root, we prevent the application code to take a dependency on the container itself (which should be prevented).&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
&lt;span style="color:Green;"&gt;// Synchronous implementation of IMailSender&lt;/span&gt;
&lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; IMailFormatter formatter;
    
    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; RealMailSender(IMailFormatter formatter)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.formatter = formatter;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// format mail&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// send mail&lt;/span&gt;
    }
}

&lt;span style="color:Green;"&gt;// Proxy for executing IMailSender asynchronously.&lt;/span&gt;
&lt;span style="color:Blue;"&gt;sealed&lt;/span&gt; &lt;span style="color:Blue;"&gt;class&lt;/span&gt; AsyncMailSenderProxy : IMailSender
{
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; ILogger logger;
    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;readonly&lt;/span&gt; Func&amp;lt;IMailSender&amp;gt; mailSenderFactory;

    &lt;span style="color:Blue;"&gt;public&lt;/span&gt; AsyncMailSenderProxy(ILogger logger,
        Func&amp;lt;IMailSender&amp;gt; mailSenderFactory)
    {
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger = logger;
        &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory = mailSenderFactory;
    }

    &lt;span style="color:Blue;"&gt;void&lt;/span&gt; IMailSender.SendMail(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Run on a new thread&lt;/span&gt;
        Task.Factory.StartNew(() =&amp;gt;
        {
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.SendMailAsync(to, message);
        });        
    }

    &lt;span style="color:Blue;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;"&gt;void&lt;/span&gt; SendMailAsync(&lt;span style="color:Blue;"&gt;string&lt;/span&gt; to, &lt;span style="color:Blue;"&gt;string&lt;/span&gt; message)
    {
        &lt;span style="color:Green;"&gt;// Here we run on a different thread and the&lt;/span&gt;
        &lt;span style="color:Green;"&gt;// services should be requested on this thread.&lt;/span&gt;
        &lt;span style="color:Blue;"&gt;var&lt;/span&gt; mailSender = &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.mailSenderFactory();

        &lt;span style="color:Blue;"&gt;try&lt;/span&gt;
        {
            mailSender.SendMail(to, message);
        }
        &lt;span style="color:Blue;"&gt;catch&lt;/span&gt; (Exception ex)
        {
            &lt;span style="color:Green;"&gt;// logging is important, since we run on a&lt;/span&gt;
            &lt;span style="color:Green;"&gt;// different thread.&lt;/span&gt;
            &lt;span style="color:Blue;"&gt;this&lt;/span&gt;.logger.Log(ex);
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;In the Composition Root, instead of registering the &lt;i&gt;MailSender&lt;/i&gt;, we register the &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; as follows:&lt;br /&gt;&lt;br /&gt;&lt;div style="color:Black;background-color:White;"&gt;&lt;pre&gt;
container.Register&amp;lt;ILogger, FileLogger&amp;gt;(Lifestyle.Singleton);
container.Register&amp;lt;IMailSender, RealMailSender&amp;gt;();
container.RegisterSingleDecorator(&lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(IMailSender),
    &lt;span style="color:Blue;"&gt;typeof&lt;/span&gt;(AsyncMailSenderProxy));
&lt;/pre&gt;&lt;/div&gt;In this case the container will ensure that when an &lt;i&gt;IMailSender&lt;/i&gt; is requested, a single &lt;i&gt;AsyncMailSenderProxy&lt;/i&gt; is returned with a &lt;i&gt;Func&amp;lt;IMailSender&amp;gt;&lt;/i&gt; delegate that will create a new &lt;i&gt;RealMailSender&lt;/i&gt; when requested. The &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterDecorator.htm"&gt;RegisterDecorator&lt;/a&gt; and &lt;a href="http://simpleinjector.cuttingedge.it/ReferenceLibrary/?topic=html/Overload_SimpleInjector_Extensions_DecoratorExtensions_RegisterSingleDecorator.htm"&gt;RegisterSingleDecorator&lt;/a&gt; overloads natively understand how to handle &lt;i&gt;Func&amp;lt;Decoratee&amp;gt;&lt;/i&gt; dependencies. The &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to&amp;ANCHOR#Generic_Decorators"&gt;Decorators&lt;/a&gt; section of the &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios&amp;referringTitle=How-to"&gt;Advanced-scenarios&lt;/a&gt; wiki page explains more about registering decorators.&lt;/div&gt;&lt;div class="ClearBoth"&gt;&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Fri, 10 May 2013 16:23:27 GMT</pubDate><guid isPermaLink="false">Updated Wiki: How-to 20130510042327P</guid></item><item><title>New Post: Scenario questions about SI</title><link>https://simpleinjector.codeplex.com/discussions/443157</link><description>&lt;div style="line-height: normal;"&gt;I'm afraid I don't fully understand your question. Could you please supply me with some more details?&lt;br /&gt;
&lt;blockquote&gt;
You cannot request an instance of a service that has been registered as scoped in a global context.&lt;br /&gt;
&lt;/blockquote&gt;
Are you talking about the Lifetime Scope lifestyle?&lt;br /&gt;
Do you mean that you can't get an instance when you didn't call &lt;code&gt;container.BeginLifetimeScope()&lt;/code&gt;?&lt;br /&gt;
&lt;blockquote&gt;
No apparent way to set a scoped instance&lt;br /&gt;
&lt;/blockquote&gt;
Can you supply me with a concrete (realistic) code example of what you're trying to achieve?&lt;br /&gt;
&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 09 May 2013 19:44:33 GMT</pubDate><guid isPermaLink="false">New Post: Scenario questions about SI 20130509074433P</guid></item><item><title>New Post: Scenario questions about SI</title><link>http://simpleinjector.codeplex.com/discussions/443157</link><description>&lt;div style="line-height: normal;"&gt;I have been evaluating Simple Injector, and am having challenges with the following scenarios. I was hoping for some comments:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;
Scoped registration and instancing are tightly coupled and code must be aware&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;
You cannot request an instance of a service that has been registered as scoped in a global context. You can only register as scoped, and then need to retrieve as scoped. This seems to violate a principle that the consumer of the service should need to know as little as possible about details like this.&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;
No apparent way to set a scoped instance&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;
You can manually set instances of globally scoped services, but you can't seem to set instances of scoped services. Example scenarios:&lt;br /&gt;
Outer, global logging instance. Some scoped calls would like to supply a scoped logging object that applies to the calling context.&lt;br /&gt;
Outer, global security credentials object. Calls outside a scope want to use the global security credentials, but some scoped calls provide their own credentials, want to set those.&lt;br /&gt;
&lt;br /&gt;
I realize you can probably do this stuff with various specialized Factory objects and/or instance caches etc., but that seems to defeat the purpose of having a DI framework. Thanks for any feedback!&lt;br /&gt;
&lt;/div&gt;</description><author>androticus</author><pubDate>Thu, 09 May 2013 19:08:16 GMT</pubDate><guid isPermaLink="false">New Post: Scenario questions about SI 20130509070816P</guid></item><item><title>New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm</title><link>http://simpleinjector.codeplex.com/discussions/443150</link><description>&lt;div style="line-height: normal;"&gt;I must admit that I myself prefer the use of the Global.asax and Application_Start. I do use the Quick Start package, but copy the need configuration to the Global.asax and uninstall the package afterwards.&lt;br /&gt;
&lt;br /&gt;
The use of the WebActivator however, was chosen because this allowed users to get started with Simple Injector just by pulling in the &amp;quot;Simple Injector MVC Integration Quick Start&amp;quot; NuGet package. That package just contains some references to other NuGet packages and one single content file. Adding a content file is a very safe. It prevents having to use tricky Visual Studio integration, which would be needed when an existing Application_Start was altered.  Besides, the addition of one single content file is much easier to grasp for developers, compared to finding out that existing files and code has been altered by some package.&lt;br /&gt;
&lt;br /&gt;
So we went for the safest way that is easiest to explain to developers. If this doesn't suit you (as it doesn't really suit myself either), I advice you to just do what I do: copy that code to the Application_Start and uninstall that package.&lt;br /&gt;
&lt;br /&gt;
Note that that although the Quick Start NuGet package takes a dependency on WebActivator, none of the Simple Injector assemblies in fact have a dependency on WebActivator, so from an architectural perspective there's no problem here. And again, if you don't want to take a dependency on WebActivator, just uninstall the Quick Start NuGet package and you're fine.&lt;br /&gt;
&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Thu, 09 May 2013 18:57:41 GMT</pubDate><guid isPermaLink="false">New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm 20130509065741P</guid></item><item><title>New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm</title><link>http://simpleinjector.codeplex.com/discussions/443150</link><description>&lt;div style="line-height: normal;"&gt;Personally, I like the direction the MVC team has gone in recent years in regards to any application_start code.  Take any of the following for examples:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;protected void Application_Start(Object sender, EventArgs e)
    {

        ViewEngineConfig.Configure();

        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

    }
&lt;/code&gt;&lt;/pre&gt;

Using WebActivator goes against this practice and also added another dependency to your project.  Technically, your SimpleInjectorIntializer class can be re-written to use the above paradigm and then remove the need for WebActivator.&lt;br /&gt;
&lt;br /&gt;
What do you think?&lt;br /&gt;
&lt;/div&gt;</description><author>omegaluz</author><pubDate>Thu, 09 May 2013 18:04:18 GMT</pubDate><guid isPermaLink="false">New Post: WebActivator Initialization vs. Global.asax app_start configuration paradigm 20130509060418P</guid></item><item><title>New Post: Simple Injector - Why and where does it loose points?</title><link>http://simpleinjector.codeplex.com/discussions/442920</link><description>&lt;div style="line-height: normal;"&gt;Hi Rod,&lt;br /&gt;
&lt;br /&gt;
Simplicity is one of the main goals of Simple Injector and Simple Injector tries to make it easier for developers to start with Dependency Injection. The feature set of Simple Injector is deliberately stripped and with that it tries to push developres in a certain direction that we feel is best; results in the most maintainable software. This doesn't mean that it is limited in its possibilities, but makes forces you to do a bit more work to get certain behavior. Take for instance making &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=How-to#Resolve-Instances-By-Key" rel="nofollow"&gt;registrations with by name/key&lt;/a&gt; and doing &lt;a href="https://simpleinjector.codeplex.com/wikipage?title=Advanced-scenarios#Property-Injection" rel="nofollow"&gt;implicit property injection&lt;/a&gt;. Both features are possible to implement in Simple Injector, but not supported out-of-the-box (OOTB), because they are abused most of the time and can lead to DI configurations that are hard to verify. We do however provide documentation that explains most scenarios that are not supported OOTB.&lt;br /&gt;
&lt;br /&gt;
Many developers fear the performance of their DI container. In reality most of the time your container´s performance isn´t an issue, but on the other hand, it´s hard to intuitively sense where the performance bottlenecks are and how to solve them. That is something Simple Injector tries to solve. Everything you do OOTB is fast. About as fast as when wiring things by hand (except the constant overhead a call to GetInstance() has, and the one-time performance hit of the initialization phase). This allows developers to stop worrying about the container's performance.&lt;br /&gt;
&lt;br /&gt;
Simple Injector however is is specially designed with a specific type of architecture in mind. Architectures that are based on the SOLID principles, work with generic abstractions, and use decorators to apply crosscutting concerns (take a look &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91" rel="nofollow"&gt;here&lt;/a&gt;, &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=92" rel="nofollow"&gt;here&lt;/a&gt;, and &lt;a href="http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=95" rel="nofollow"&gt;here&lt;/a&gt; for examples of this type of design). Especially the area of using decorators is where other containers lack and where Simple Injector tries to fill a gap. Most containers use a method called interception to apply crosscutting concerns to types, and make it really hard to apply decorators in a way that is maintainable. In our opinion, the use of decorators is much better than using interception, because decorators don´t force you to take a dependency on some interception library, and results in code that is simpler and faster. Simple Injector for instance, makes it very easy to apply (generic) decorators conditionally, based on either a predicate or a generic type constraint.&lt;br /&gt;
&lt;br /&gt;
Unity makes it possible to apply decorators, but when it comes to applying decorators conditionally, this gets really hard in Unity, and will lead to a DI configuration that becomes unmaintanable. Whether or not this is a problem, depends on the size and design of your application, of course.&lt;br /&gt;
&lt;br /&gt;
Of course I´m biased about Simple Injector, and would argue that you can do anything with Simple Injector, but perhaps not OOTB. So things that are supported by Simple Injector OOTB but are in Unity are for instance keyed registration, interception, and handling types with multiple constructors.  Simple Injector on the other hand is very fast and has very good support for decorators.&lt;br /&gt;
&lt;br /&gt;
The choice is up to you.&lt;br /&gt;
&lt;/div&gt;</description><author>dot_NET_Junkie</author><pubDate>Wed, 08 May 2013 10:46:58 GMT</pubDate><guid isPermaLink="false">New Post: Simple Injector - Why and where does it loose points? 20130508104658A</guid></item><item><title>New Post: Simple Injector - Why and where does it loose points?</title><link>http://simpleinjector.codeplex.com/discussions/442920</link><description>&lt;div style="line-height: normal;"&gt;Hi,&lt;br /&gt;
&lt;br /&gt;
I must say I was very impressed with the performance benchmarks I've seen out there regarding Simple Injector as well as the documentation you are providing. &lt;br /&gt;
&lt;br /&gt;
I'm looking into DI frameworks for my company's new product and came across SI. &lt;br /&gt;
&lt;br /&gt;
There is already new developments using Unity for Web applications. The natural way would be to go with Unity. The new product is to be a cloud based multi-threaded application with multiple activations per second so completely different picture.&lt;br /&gt;
&lt;br /&gt;
I have two questions that could help me a lot to decide and influence decisions.&lt;br /&gt;
&lt;br /&gt;
1 - What motivated you on developing such a framework considering the existing ones out there? If it was performance I would like to know about that business case&lt;br /&gt;
2 - What are the concrete feature limitations on this framework? From what I'm seeing it can do almost everything as Unity.&lt;br /&gt;
&lt;br /&gt;
Tnks in advance,&lt;br /&gt;
Rod&lt;br /&gt;
&lt;/div&gt;</description><author>RBoavida</author><pubDate>Wed, 08 May 2013 08:33:56 GMT</pubDate><guid isPermaLink="false">New Post: Simple Injector - Why and where does it loose points? 20130508083356A</guid></item></channel></rss>