How does ASP.NET 5 inject startup's dependencies? - dependency-injection

I'm studying ASP.NET 5 documentation (It's great and better than the old one.) I understand that ASP.NET 5 includes a simple built-in inversion of control (IoC) container that supports constructor injection by default. As far as I know, configuring services and dependencies are done inside ConfigureServices() method.
The ConfigureServices() method is called after StartUp method.
So my question is: how does ASP.NET 5 internally inject Startup's dependencies?
I'd like to know that because if I want to inject another dependency, for example IFooEnviroment how can I do that?

The logic for this lives in the Hosting layer of ASP.NET 5:
the Startup type is determined
the hosting layer registers the required runtime services in the IServiceCollection
An internal service provider is build
an instance of the Startup class is created
You can of course register your own services in ConfigureServices. But they won't be available in the constructor as you already figured. There is no way to add your own services to the runtime services. This makes sense because there should be a difference between runtime services and application services.
You can however inject services registered in the ConfigureServices() method in the Configure() method.

Related

Where should I configure my DI container for domain infrastructure services in DDD?

I am trying to figure our where the code for configuring my dependency injection container for my domain repository services lives.
My initial thought was to let the client configure all services, but then the client needs to be aware of the repository service, which I don't want to expose in the client.
I was thinking that each layer could configure its own service dependencies via an exposed configuration method or class?
My initial thought was to let the client configure all services, but
then the client needs to be aware of the repository service, which I
don't want to expose in the client.
What I tend to do is still follow the Composition Root pattern, where my web project would be the first place where the IoC wiring-up would start (i.e. the usual sort of 3rd party library that would hook itself into the controller factory). But I then configure the rest of my IoC bindings by referencing an IoC module (in my case I usually use Ninject, so I reference a NinjectModule from my web project). But I put this NinjectModule in a separate infrastructure project.
I can then reference this IoC infrastructure project from my web project and the IoC project can reference every other project in the solution.
This way I don't have to create project references in my web project to things I might not want my web project to access, like my repository layer.
The answer has already been given in comments, I'm just wrapping it up in a proper answer here.
Unless you use a special-purpose architecture like a plug-in architecture, you should always configure the DI container at a single place, as near to the application entry point as possible. This is called the composition root.
It doesn't matter if the configured type is a repository, factory, domain service or application service. All DI configuration should go in one place.
See this blog post for a good introduction on the details of composition roots.

Using IoC (Castle Windsor) with dependencies determined at request time

I have an existing MVC app where I use Castle Windsor as IoC container. It runs in Azure as a Web App.
We have recently expanded the functionality with a new controller based on ASP.Net Web API.
The container is set up and configured in global.asax
But - due to the different nature of the MVC framework and the Web API framework, specifically how they handle HttpContext, we have some code at the core part of our code, that needs to work according to which controller method was executed. The code for doing this is seperated in two different classes (implementing the same interface).
The solution we have now is to configure the container with both instances, inject both instances to the class, and at runtime figure out (by investigating the HttpContext data) which to execute code on.
But I think that sucks.
Is there any way we can do this better? Can we instruct Castle Windsor to figure out which instance to inject at runtime - based on e.g. a value in the URI?

Choosing correct lifetime scope for Autofac components in MVC 5, SignlaR, NServiceBus

I am trying to understand how to correctly register components and dependencies with Autofac in a MVC 5 application that also uses the following:
Application Services (billing service, etc.)
Hangfire (there is a Hangfire.Autofac integration package)
SignalR (there is a Autofac.SignalR integration package)
NServiceBus (there is a NServiceBus.Autofac integration package)
Quartz.NET (there is a Quartz.Autofac integration package)
Entity Framework (all of the above may or may not need a DbContext injected)
While I understand the purpose of e.g. builder.RegisterType<MyDbContext>().InstancePerRequest() when used as per-request and injected into an MVC controller, I am unclear how the other components need to be registered.
The MVC container I register in OWIN startup.cs class with:
var resolver = new Autofac.Integration.Mvc.AutofacDependencyResolver( container );
DependencyResolver.SetResolver( resolver );
I have an extra Autofac container for SignalR, because it would not work otherwise.
So it all comes down to the question: With what lifetime scope should I register a MyDbContext that is used by a MVC Controller, a SignalR hub, an NServiceBus Message handler, and some application service at the same time?
Is it enough to register MyDbContext as .InstancePerRequest() or is it neccessary to have multiple containers, and with that, should I register MyDbContext with each container?
I am new to Autofac so I'm at a loss as to how I could control when those components are disposed, recreated, etc.
Thank you.

design pattern for adding a web service in ASP.NET MVC3

Firstly - I'm not asking this question How to host a web service in MVC3? I know where the button is :-)
I'm trying to find an example of best practices of how to use the new DI / Common Service Locator framework in order to make web service calls (and code dependent on web service calls) testable. I've no experience of using NInject or the like - is that the way to go?
Pretty much the same way one deals with any external depenency -- wrap it up in an interface, make the controller take an instance of the interface as a constructor parameter. Implementation-wise, you can handle things a number of ways, we have typically made the service wrapper take the service as a dependency and let structuremap worry about lifecycle. Not horribly familiar with NInject so I'm not sure if there is a better way there but I'd suspect they have similar capabilities.
I don't know what is the best practice but I think you can do this with Windsor's WCF facility (Ninject has a WCF extension as well). Register your service, then set your dependency resolver and let MVC's dependency resolver to do the hard work, constructor injection for example:
Register your service:
container = new WindsorContainer().AddFacility<WcfFacility>();
container.Register(Component
.For<IService>()
.On(WcfEndpoint.FromConfiguration("...")))
.LifeStyle.Transient);
Set dependency resolver:
DependencyResolver.SetResolver(new WindsorDependencyResolver(container));
Then MVC3's new dependency resolver should be able to inject your service proxy into the constructor, for example:
public HomeController(IService service)
{
// ...
}

How do I use Windsor Container in a different assembly but same application?

From what I understand about Windsor Container and MVC applications, is that there should only be one instance of the container and it's usually registered in Global.asax for the running life of the application.
I've separated out my business layer in to a separate assembly from the web application and obviously I can't get to that instance unless I register the web application in the other assembly which would create a circular reference.
How do I use the container throughout my application? User a global static reference in my business layer assembly?
No - you don't. The documentation should give you an idea of how to use it.

Resources