{"id":434,"date":"2014-06-17T11:21:00","date_gmt":"2014-06-17T11:21:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2014\/06\/17\/dependency-injection-in-asp-net-vnext\/"},"modified":"2022-08-08T05:16:12","modified_gmt":"2022-08-08T12:16:12","slug":"dependency-injection-in-asp-net-vnext","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/dependency-injection-in-asp-net-vnext\/","title":{"rendered":"Dependency Injection in ASP.NET vNext"},"content":{"rendered":"<p align=\"justify\">Dependency Injection (DI) is a software design pattern, a particular case of the Inversion of Control pattern, in which one or more dependencies are injected into dependent objects. The pattern is used to create program designs that are loosely coupled and testable.<\/p>\n<p align=\"justify\">This article assumes that you are already familiar with DI. If not, you can read <a href=\"http:\/\/en.wikipedia.org\/wiki\/Dependency_injection\" target=\"_blank\" rel=\"noopener\">this article<\/a>&nbsp;for a brief introduction.<\/p>\n<h3>DI in ASP.NET vNext<\/h3>\n<p align=\"justify\">In ASP.NET vNext, dependency injection is a first class citizen. While in the previous versions of the framework, DI was partially supported, in ASP.NET vNext it is available throughout the entire stack. A minimalistic DI container is provided out of the box but we are leaving the door open to BYOC (Bring Your Own Container). The default container is useful in the cases when you don&rsquo;t need any advanced injection capabilities (see the known limitations section at the end of the post).<\/p>\n<p align=\"justify\">BYOC is possible because of an abstraction over the actual DI container implementation. The abstraction is the IServiceProvider interface and it represents the least set of container behavior our components are limited to assuming are present. All the framework components (MVC, Routing, SignalR, Entity Framework, etc.) rely only on the capabilities of IServiceProvider, but your own application code can use any feature that your chosen DI container has. When you BYOC, you can replace the default implementation of IServiceProvider with a wrapper around your own container.&nbsp; Once that happens, all the dependency resolution calls will be routed to your own container. In the case when you want to use your own container strictly for your own custom types, we support fallback to our default container.<\/p>\n<p align=\"justify\">Because all framework components use the same container to register services, we can now flow dependencies across the stack. This opens the door to new scenarios that were not possible before, like injecting a SignalR broadcaster into an MVC controller action. As we walk up the stack, there are different layers of dependency resolvers. All dependencies are added to a single container and everyone can see everybody else&rsquo;s services. The single container also addresses the cross-cutting concern customization story. It is trivial in the new stack to change cross-cutting concerns (e.g. logging) via a single entry point.<\/p>\n<p align=\"justify\">The out of the box container supports the following lifestyles:<\/p>\n<table style=\"width: 500px\" border=\"1\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"100\"><strong>Lifestyle<\/strong><\/td>\n<td valign=\"top\" width=\"400\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"100\">Instance<\/td>\n<td valign=\"top\" width=\"400\">A specific instance is given all the time. You are responsible for its initial creation<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"100\">Transient<\/td>\n<td valign=\"top\" width=\"400\">A new instance is created every time<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"100\">Singleton<\/td>\n<td valign=\"top\" width=\"400\">A single instance is created and it acts like a singleton<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"100\">Scoped<\/td>\n<td valign=\"top\" width=\"400\">A single instance is created inside the current scope. It is equivalent to Singleton in the current scope<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Per Request Scope<\/h3>\n<p align=\"justify\">A popular feature for DI in web applications is to create objects that have a single instance per web request. This means that the objects acts as a singleton inside that request but two distinct requests will have different instances of the objects.<\/p>\n<p align=\"justify\">In ASP.NET vNext, the Per Request Scope is achieved using a middleware and a scoped lifestyle. The middleware, when invoked, will create a new scoped container which will replace the container for the current request. All the subsequent middleware in the pipeline will then utilize the scoped container. After the request flows through the pipeline and the container middleware is signaled to complete, the scope is destroyed and all the objects created inside it are disposed.<\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/06\/4530.ContainerMiddleware.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/06\/4530.ContainerMiddleware.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p align=\"justify\">The source code for the ContainerMiddleware, is <a href=\"https:\/\/github.com\/aspnet\/Hosting\/blob\/dev\/src\/Microsoft.AspNet.RequestContainer\/ContainerMiddleware.cs\" target=\"_blank\" rel=\"noopener\">available on GitHub<\/a>.<\/p>\n<p align=\"justify\">In the rare case in which you need to create your own container scope, you can use the IServiceScopeFactory to do this. When the default implementation of the IServiceProvider is created, the IServiceScopeFactory is one of the services that are registered by default.<\/p>\n<h3>New vs Old<\/h3>\n<p align=\"justify\">For the purpose of showing the differences between DI in the old and new stack, we are going to use an MVC controller that writes a string provided through an injected dependency:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2a47376a-8b07-4d8f-84a6-e3e12ed41b65\" class=\"wlWriterEditableSmartContent\" style=\"float: none;margin: 0px;padding: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\" start=\"1\">\n<li><span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">interface <\/span><span style=\"background: #ffffff;color: #2b91af\">IMessageGenerator<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">string<\/span><span style=\"background: #ffffff;color: #000000\"> GenerateMessage();<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">class <\/span><span style=\"background: #ffffff;color: #2b91af\">HelloMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\"> : <\/span><span style=\"background: #ffffff;color: #2b91af\">IMessageGenerator<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">string<\/span><span style=\"background: #ffffff;color: #000000\"> GenerateMessage()<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">return<\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;Hello DI!&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">;<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">class <\/span><span style=\"background: #ffffff;color: #2b91af\">MessageController<\/span><span style=\"background: #ffffff;color: #000000\"> : Controller<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">private <\/span><span style=\"background: #ffffff;color: #0000ff\">readonly <\/span><span style=\"background: #ffffff;color: #2b91af\">IMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\"> messageGenerator;<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">public<\/span><span style=\"background: #ffffff;color: #000000\"> MessageController(<\/span><span style=\"background: #ffffff;color: #2b91af\">IMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\"> generator)<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">if<\/span><span style=\"background: #ffffff;color: #000000\"> (generator == <\/span><span style=\"background: #ffffff;color: #0000ff\">null<\/span><span style=\"background: #ffffff;color: #000000\">)<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">throw <\/span><span style=\"background: #ffffff;color: #0000ff\">new <\/span><span style=\"background: #ffffff;color: #2b91af\">ArgumentNullException<\/span><span style=\"background: #ffffff;color: #000000\">(<\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;generator&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">, <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;The generator dependecy is mandatory&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">);<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">this<\/span><span style=\"background: #ffffff;color: #000000\">.messageGenerator = generator;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">string<\/span><span style=\"background: #ffffff;color: #000000\"> GetMessage()<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">return <\/span><span style=\"background: #ffffff;color: #0000ff\">this<\/span><span style=\"background: #ffffff;color: #000000\">.messageGenerator.GenerateMessage();<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p align=\"justify\">None of the code above is changed between the old and the new stack. The only difference is where and how the dependency are registered and resolved.<\/p>\n<p align=\"justify\">In the old MVC stack, controller dependencies are resolved through a custom controller factory. For the purpose of this demo, we are going to implement the Poor Man&rsquo;s DI and manually compose the dependencies:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a68f94cb-8bb9-4730-89fc-d0997fd8ebe1\" class=\"wlWriterEditableSmartContent\" style=\"float: none;margin: 0px;padding: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\" start=\"1\">\n<li><span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">class <\/span><span style=\"background: #ffffff;color: #2b91af\">DIControllerFactory<\/span><span style=\"background: #ffffff;color: #000000\"> : <\/span><span style=\"background: #ffffff;color: #2b91af\">DefaultControllerFactory<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">override <\/span><span style=\"background: #ffffff;color: #2b91af\">IController<\/span><span style=\"background: #ffffff;color: #000000\"> CreateController(<\/span><span style=\"background: #ffffff;color: #2b91af\">RequestContext<\/span><span style=\"background: #ffffff;color: #000000\"> requestContext, <\/span><span style=\"background: #ffffff;color: #0000ff\">string<\/span><span style=\"background: #ffffff;color: #000000\"> controllerName)<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ If a message controller is requested&#8230;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">if<\/span><span style=\"background: #ffffff;color: #000000\"> (controllerName == <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;Message&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">)<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ &#8230; then create a new controller and set up the dependency<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">return <\/span><span style=\"background: #ffffff;color: #0000ff\">new <\/span><span style=\"background: #ffffff;color: #2b91af\">MessageController<\/span><span style=\"background: #ffffff;color: #000000\">(<\/span><span style=\"background: #ffffff;color: #0000ff\">new <\/span><span style=\"background: #ffffff;color: #2b91af\">HelloMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\">());<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Otherwise, fallback to the default implementation<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">return <\/span><span style=\"background: #ffffff;color: #0000ff\">base<\/span><span style=\"background: #ffffff;color: #000000\">.CreateController(requestContext, controllerName);<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li><span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">class <\/span><span style=\"background: #ffffff;color: #2b91af\">MvcApplication<\/span><span style=\"background: #ffffff;color: #000000\"> : <\/span><span style=\"background: #ffffff;color: #2b91af\">HttpApplication<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">protected <\/span><span style=\"background: #ffffff;color: #0000ff\">void<\/span><span style=\"background: #ffffff;color: #000000\"> Application_Start()<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">&#8230;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Register the controller factor<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">ControllerBuilder.Current.SetControllerFactory(<\/span><span style=\"background: #ffffff;color: #0000ff\">new <\/span><span style=\"background: #ffffff;color: #2b91af\">DIControllerFactory<\/span><span style=\"background: #ffffff;color: #000000\">());<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">&#8230;<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p align=\"justify\">The controller factory will inject a concrete implementation (HelloMessageGenerator) of the interface (IMessageGenerator) in the Message controller.<\/p>\n<p align=\"justify\">The same code, ported to ASP.NET vNext, doesn&rsquo;t need a Poor Man&rsquo;s DI implementation. As mentioned before, in this new stack, a DI container is available out of the box. Thus, all we need to do is tell it what is the mapping between the interface and the concrete implementation.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d06f89cb-b0ff-498f-b100-3501b20c9abb\" class=\"wlWriterEditableSmartContent\" style=\"float: none;margin: 0px;padding: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\" start=\"1\">\n<li><span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">class <\/span><span style=\"background: #ffffff;color: #2b91af\">Startup<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">void<\/span><span style=\"background: #ffffff;color: #000000\"> Configure(<\/span><span style=\"background: #ffffff;color: #2b91af\">IBuilder<\/span><span style=\"background: #ffffff;color: #000000\"> app)<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">&#8230;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">app.UseServices(services =&gt;<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">&#8230;<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Set up the dependencies<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">services.AddTransient&lt;<\/span><span style=\"background: #ffffff;color: #2b91af\">IMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\">, <\/span><span style=\"background: #ffffff;color: #2b91af\">HelloMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\">&gt;();<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">&#8230;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">});<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">&#8230;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p align=\"justify\">The default implementation of the controller factory in ASP.NET vNext uses the IServiceProvider to resolve the dependencies. If you BYOC, none of the code above will change. You would only have to tell the application to use a different implementation of IServiceProvider.<\/p>\n<h3>Replacing the default DI container<\/h3>\n<p align=\"justify\">The code below shows how the previous sample can be rewritten to use Autofac instead of the out of the box container.<\/p>\n<p align=\"justify\">The code uses compiler directives to use Autofac code only for the full .NET 4.5 Framework because Autofac is not available for the .NET Core Framework 4.5. In Visual Studio &ldquo;14&rdquo; CTP you can change the target framework of a project by right clicking on it and going to Properties -&gt; Active Target Framework.<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8887b1b1-885d-42a6-a1b9-1ef61418c647\" class=\"wlWriterEditableSmartContent\" style=\"float: none;margin: 0px;padding: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\" start=\"1\">\n<li><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> System;<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.AspNet.Builder;<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.AspNet.Routing;<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.Framework.DependencyInjection;<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.Framework.DependencyInjection.Fallback;<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.AspNet.Mvc;<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.AspNet.RequestContainer;<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> DISample.Models;<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">#if<\/span><span style=\"background: #ffffff;color: #000000\"> NET45<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Autofac;<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.Framework.DependencyInjection.Autofac;<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #0000ff\">using<\/span><span style=\"background: #ffffff;color: #000000\"> Microsoft.Framework.OptionsModel;<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">#endif<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">namespace<\/span><span style=\"background: #ffffff;color: #000000\"> DISample<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">class <\/span><span style=\"background: #ffffff;color: #2b91af\">Startup<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #0000ff\">public <\/span><span style=\"background: #ffffff;color: #0000ff\">void<\/span><span style=\"background: #ffffff;color: #000000\"> Configure(<\/span><span style=\"background: #ffffff;color: #2b91af\">IBuilder<\/span><span style=\"background: #ffffff;color: #000000\"> app)<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Add the MVC services<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2b91af\">ServiceCollection<\/span><span style=\"background: #ffffff;color: #000000\"> services = <\/span><span style=\"background: #ffffff;color: #0000ff\">new <\/span><span style=\"background: #ffffff;color: #2b91af\">ServiceCollection<\/span><span style=\"background: #ffffff;color: #000000\">();<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">services.AddMvc();<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">services.Add(<\/span><span style=\"background: #ffffff;color: #2b91af\">OptionsServices<\/span><span style=\"background: #ffffff;color: #000000\">.GetDefaultServices());<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li><span style=\"background: #ffffff;color: #008000\">\/\/ The NET45 symbol is defined when the project targets .NET Framework 4.5<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">#if<\/span><span style=\"background: #ffffff;color: #000000\"> NET45<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Create the autofac container<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2b91af\">ContainerBuilder<\/span><span style=\"background: #ffffff;color: #000000\"> builder = <\/span><span style=\"background: #ffffff;color: #0000ff\">new <\/span><span style=\"background: #ffffff;color: #2b91af\">ContainerBuilder<\/span><span style=\"background: #ffffff;color: #000000\">();<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Register the message generator through autofac<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">builder.RegisterType&lt;<\/span><span style=\"background: #ffffff;color: #2b91af\">HelloMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\">&gt;().As&lt;<\/span><span style=\"background: #ffffff;color: #2b91af\">IMessageGenerator<\/span><span style=\"background: #ffffff;color: #000000\">&gt;();<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Create the container and use the default application services as a fallback<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2b91af\">AutofacRegistration<\/span><span style=\"background: #ffffff;color: #000000\">.Populate(<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">builder, <\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">services,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">fallbackServiceProvider: app.ApplicationServices);<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2b91af\">IContainer<\/span><span style=\"background: #ffffff;color: #000000\"> container = builder.Build();<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ Replace the default container<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">app.ApplicationServices = container.Resolve&lt;<\/span><span style=\"background: #ffffff;color: #2b91af\">IServiceProvider<\/span><span style=\"background: #ffffff;color: #000000\">&gt;();<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #0000ff\">#else<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #808080\">\/\/ Here we are running on .NET Core Framework 4.5 so we cannot use Autofac<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #808080\">services.AddTransient&lt;IMessageGenerator, HelloMessageGenerator&gt;();<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #808080\">app.ApplicationServices = services.BuildServiceProvider(app.ApplicationServices);<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background: #ffffff;color: #0000ff\">#endif<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #008000\">\/\/ MVC requires the container middleware<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">app.UseMiddleware(<\/span><span style=\"background: #ffffff;color: #0000ff\">typeof<\/span><span style=\"background: #ffffff;color: #000000\">(<\/span><span style=\"background: #ffffff;color: #2b91af\">ContainerMiddleware<\/span><span style=\"background: #ffffff;color: #000000\">));<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">app.UseMvc(routes =&gt;<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">routes.MapRoute(<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">name: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;default&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">template: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;{controller}\/{action}\/{id?}&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">defaults: <\/span><span style=\"background: #ffffff;color: #0000ff\">new<\/span><span style=\"background: #ffffff;color: #000000\"> { controller = <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;Message&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">, action = <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;GetMessage&#8221;<\/span><span style=\"background: #ffffff;color: #000000\"> });<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">});<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p align=\"justify\">In order to compile the code above, you must add the dependency injection dependencies to project.json. Since Autofac is not available for .NET Core Framework 4.5, the Autofac dependency is only defined for in the net45 section:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1cbfa81f-4f91-4db6-a941-f69d69627fd8\" class=\"wlWriterEditableSmartContent\" style=\"float: none;margin: 0px;padding: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\" start=\"1\">\n<li>{<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;dependencies&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: {<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Helios&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.AspNet.Mvc&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.AspNet.Identity.Entity&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.AspNet.Identity.Security&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.AspNet.Security.Cookies&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.AspNet.Server.WebListener&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.AspNet.StaticFiles&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.Data.Entity&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.Data.Entity.SqlServer&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.Framework.ConfigurationModel.Json&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.VisualStudio.Web.BrowserLink.Loader&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;14.0-alpha&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.Framework.DependencyInjection&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">},<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;commands&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: {<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;web&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;Microsoft.AspNet.Hosting &#8211;server Microsoft.AspNet.Server.WebListener &#8211;server.urls http:\/\/localhost:5000&#8221;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">},<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;configurations&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: {<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;net45&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: {<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;dependencies&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: {<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;System.Data&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;System.ComponentModel.DataAnnotations&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Microsoft.Framework.DependencyInjection.Autofac&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;0.1-alpha-build-*&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">,<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;Autofac&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: <\/span><span style=\"background: #ffffff;color: #a31515\">&#8220;3.3.0&#8221;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">},<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #2e75b6\">&#8220;k10&#8221;<\/span><span style=\"background: #ffffff;color: #000000\">: {<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<li><span style=\"background: #ffffff;color: #000000\">}<\/span><\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<h3>Best Practices for DI in ASP.NET vNext<\/h3>\n<p align=\"justify\">When using DI, we recommend the following practices:<\/p>\n<ol>\n<li>\n<div align=\"justify\">Register all dependencies in the application startup method, before doing anything else.<\/div>\n<\/li>\n<li>\n<div align=\"justify\">Avoid consuming the IServiceProvider interface directly. Instead, add explicit dependencies as constructor parameters and let the callers resolve them. Use abstract factories when the number of dependencies becomes hard to manage.<\/div>\n<\/li>\n<li>\n<div align=\"justify\">Code against contracts rather than actual implementations.<\/div>\n<\/li>\n<\/ol>\n<h3>Known Limitations<\/h3>\n<p align=\"justify\">The out of the box container for ASP.NET vNext Alpha has a few known limitations:<\/p>\n<ul>\n<li>\n<div align=\"justify\">It only supports constructor injection<\/div>\n<\/li>\n<li>\n<div align=\"justify\">It can only resolve types with one and only one public constructor<\/div>\n<\/li>\n<li>\n<div align=\"justify\">It doesn&rsquo;t support advanced features (like per thread scope or auto discovery)<\/div>\n<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p align=\"justify\">ASP.NET vNext is DI-friendly and it uses dependency injection throughout the stack. While there is a default minimalistic DI container out of the box, you can Bring Your Own Container and use advanced DI capabilities. The source code for Dependency Injection <a href=\"https:\/\/github.com\/aspnet\/DependencyInjection\">is available on GitHub<\/a>.<\/p>\n<p align=\"justify\">We&rsquo;d love to hear your feedback. Please provide it in <a href=\"http:\/\/github.com\/aspnet\">Github<\/a>, comments on this blog, or the <a href=\"http:\/\/forums.asp.net\/1255.aspx\/1?ASP+NET+vNext\">ASP.NET vNext forum<\/a>. If you ask a question in Stack Overflow, use <a href=\"http:\/\/stackoverflow.com\/questions\/tagged\/asp.net-vnext\">asp.net-vnext<\/a>tag.<\/p>\n<p align=\"justify\">ASP.NET vNext is an open source project released under Apache License Version 2.0 by <a href=\"http:\/\/msopentech.com\/\">Microsoft Open Technologies<\/a>, Inc. You can follow its progress and find instructions on how to contribute on <a href=\"https:\/\/github.com\/aspnet\">https:\/\/github.com\/aspnet<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dependency Injection (DI) is a software design pattern, a particular case of the Inversion of Control pattern, in which one or more dependencies are injected into dependent objects. The pattern is used to create program designs that are loosely coupled and testable. This article assumes that you are already familiar with DI. If not, you [&hellip;]<\/p>\n","protected":false},"author":419,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[31,7465,7469],"class_list":["post-434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-asp-net","tag-asp-net-vnext","tag-dependency-injection"],"acf":[],"blog_post_summary":"<p>Dependency Injection (DI) is a software design pattern, a particular case of the Inversion of Control pattern, in which one or more dependencies are injected into dependent objects. The pattern is used to create program designs that are loosely coupled and testable. This article assumes that you are already familiar with DI. If not, you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/419"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=434"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/434\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}