.NET Core 3.0 Dependency injection extension
September 15, 2022 ยท View on GitHub
Service Provider using Castle Windsor container for ASP.NET Core 3.x. Fully replaces ASP.NET Core Default Service Provider (container).
How does it work?
Registering services
You can register service either IServiceCollection in startup class. Just as you would with standard .NET Dependency Injection add your registration to the Startup class.
Alternatively you can create ConfigureServices method in Startup class that gets passed the IWindsorContainer instance and register the services there
public void ConfigureContainer (IWindsorContainer container)
{
container.Install(new MyInstaller());
}
To match lifestyle of the registered components to .NET Service Scope, use LifeStyle.ScopedToNetServiceScope().
Services lifestyle
Because there are subtle differences between .NET and Castle Windsor lifestyle semantics services registered via IServiceCollection are following .NET semantics:
| .NET lifestyle | Description |
|---|---|
| Scoped | Lifecycle of the component is bound to the scope associated with IServiceProvider that resolved it. Even if it's not the closest one. The instance is disposed when IServiceScope is disposed. |
| Transient | New instance is provided when resolved but lifecycle is bound to the scope associated with IServiceProvider that resolved it. All instances are disposed when IServiceScope is disposed. |
| Singleton | Only one instance ever exists and it's disposed once outermost IServiceProvider is disposed (usually application shutdown). |
What do I need to set it up?
Using the .NET lifestyle semantics
All services injected into controllers will have to be registered with the lifestyles described in "Services lifestyle"
- Add
Castle.Windsor.Extensions.Hostingpackage to your application. - Add
UseWindsorContainerServiceProvider()when creating the Host
This will register anHost.CreateDefaultBuilder(args) .UseWindsorContainerServiceProvider()IServiceProviderFactory - Any services registered in
Startup.ConfigureServiceswill be registered withIWindsorContainer. No need to cross-wire sinceIWindsorContaineris the onlyIServiceProvider - To access the container directly inject either
IWindsorContainerorIServiceProvider
Using the Castle Windsor lifestyle semantics
This will allow you to leave your existing Castle Windsor registrations untouched.
Using .NET lifestyle semantics, any service using the Castle Windsor transient lifestyle, and directly injected into the controller, will not be released. To allow the standard Windsor behavior for services directly injected in the controllers, you will have to allow Castle Windsor to resolve and release the controllers. To do that add AddControllerAsServices to the call to ConfigureServices in your Startup class:
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.AddControllersAsServices();
}