--- title: ServiceStack's IOC --- ::: info When using ASP.NET Core **Endpoint Routing** refer to [ASP.NET Core IOC](/net-ioc) instead ::: ServiceStack uses a slightly modified version of [Funq](http://funq.codeplex.com/) - which was adopted because of its excellent [performance and memory characteristics](http://www.servicestack.net/benchmarks/). ServiceStack's version of Funq has been enhanced with Expression-based Auto-wiring and lifetime Request Scope. If you so wish, you can still elect to use your favorite IOC by creating an `IContainerAdapter` for them. See below for examples of adapters for popular IOC's. All ServiceStack's customizable hooks support Auto-wiring out-of-the-box, namely: - [Services](/your-first-webservice-explained) - [Request and Response Filter attributes](/filter-attributes) _(are executed before a service gets called)_ - [Validators](/validation) _(validates a Request DTO before the service gets called)_ For each of these features, dependencies are resolved for all parameters in the constructor that has the most arguments as well as all public properties. ## .NET Core Compatible Registration APIs To retain compatibility with ASP .NET Core IOC we recommend using Funq's `IServiceCollection` and `IServiceProvider` interfaces when registering dependencies, letting you use a single consistent API to register dependencies in both ServiceStack's and .NET Core's IOC making it easy to move registrations between the two, e.g: ```csharp public void Configure(Container container) { //Register Singleton instance using Factory function container.AddSingleton(c => new Bar { Name = "bar" }); //Equivalent to: container.Register(c => new Bar { Name = "bar" }); //Register Auto-Wired Transient instance container.AddTransient(); //Equivalent to: container.RegisterAutoWiredAs().ReusedWithin(ReuseScope.None); //Register Auto-Wired Request Scope instance container.AddScoped(); //Equivalent to: container.RegisterAutoWiredAs().ReusedWithin(ReuseScope.Request); } ``` ## Autowire Registration You can register dependencies so that all the dependencies are automatically auto-wired. Just like the hooks above, once resolved Funq will create a new instance resolving dependencies for the constructor that has the most arguments as well as all public properties. ```csharp public override void Configure(Container container) { container.AddSingleton(); container.AddSingleton(); } ``` There's also support for registration of run-time types with these APIs below: ```csharp container.AddSingleton(typeof(MyType)); container.AddSingleton(typeof(IMyType),typeof(MyType)); ``` ## Custom Registration Funq also supports custom creation of instances. When used no auto-wiring is performed and it's left up to you to explicitly resolve all your dependencies. ### Using Custom Factories In addition to Auto-wiring Funq allows you to customize the creation of your instance with custom delegates. This is useful when your dependencies require custom configuration. E.g: ```csharp container.AddSingleton(c => new MyType(c.GetRequiredService(), connectionString)); container.AddSingleton(c => new MyType(c.GetRequiredService(), connectionString)); container.AddSingleton(c => CreateAndInitializeMyType( c.GetRequiredService(), c.GetRequiredService)); ``` ### Register instances Other than factories, you can also register custom instances where instead of returning a lambda above you can return an instance: ```csharp container.AddSingleton(new MyType(c.GetRequiredService(), connectionString)); ``` ::: info When using the methods above, the properties and the constructor of the registered type aren't auto-wired (ie **the properties and the constructor are not injected**) ::: ## Object lifetime By default all dependencies registered in Funq have singleton scope, where the same instance is injected into all dependencies. This behaviour can be changed by defining the scope explicitly, which is supported with the following APIs: ```csharp container.AddSingleton(c => new MyType()); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(typeof(MyType)); container.AddSingleton(typeof(IMyType), typeof(MyType)); ``` ### Supported Lifetime Scopes - `AddSingleton`: Singleton scope (a instance is used per application lifetime) - `AddScoped`: Request scope (a instance is used per request lifetime) - `AddTransient`: Transient scope (a new instance is created every time) #### Scoped Dependencies In ASP.NET Core ServiceStack is pre-configured to use a `NetCoreContainerAdapter` where it will also resolve any dependencies declared in your .NET Core Startup using `app.ApplicationServices`. One side-effect of this is that when resolving **Scoped** dependencies it resolves them in a Singleton scope instead of the Request Scope had they instead been resolved from `context.RequestServices.GetService()`. One way to be able to inject scoped dependencies into your Services is to register the `IHttpContextAccessor` where they'll be resolved from ASP.NET Core's RequestServices context: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddScoped(); } ``` Otherwise if you need to resolve Request Scoped .NET Core dependencies you can resolve them from `IRequest`, e.g: ```csharp public object Any(MyRequest request) { var requestScope = base.Request.GetService(); } ``` Alternatively you can register the dependencies in ServiceStack's IOC instead, e.g: ```csharp public override void Configure(Container container) { container.AddScoped(); } ``` Where they'd be resolved within ServiceStack's Request Scope instead of via ASP.NET Core's RequestServices. ### ASP.NET Core IServiceProvider APIs Registering dependencies in ServiceStack's IOC are only available within ServiceStack, you can access **scoped ASP.NET Core dependencies** and create Custom IOC Scopes using the `IRequest` APIs below: - `IRequest.CreateScope()` - `IRequest.GetService()` - `IRequest.GetService()` - `IRequest.GetRequiredService()` - `IRequest.GetRequiredService()` - `IRequest.GetServices()` - `IRequest.GetServices()` Which can be used to create custom scopes that utilizes ASP.NET Entity Framework Identity classes in your ServiceStack Services: ```csharp using (var scope = Request.CreateScope()) { var RoleManager = scope.ServiceProvider.GetRequiredService>(); var UserManager = scope.ServiceProvider.GetRequiredService>(); var managerUser = await UserManager.FindByEmailAsync("manager@gmail.com"); if (managerUser == null) { assertResult(await UserManager.CreateAsync(new ApplicationUser { DisplayName = "Test Manager", Email = "manager@gmail.com", UserName = "manager@gmail.com", FirstName = "Test", LastName = "Manager", }, "p@55wOrd")); managerUser = await UserManager.FindByEmailAsync("manager@gmail.com"); await UserManager.AddToRoleAsync(managerUser, "Manager"); } } ``` ## Advanced Usages ### Autowiring Generic Type Definitions Whilst ServiceStack's IOC doesn't have a native support for registering types based on a generic type definition, it's easy to use the Runtime Type APIs and register them yourself. E.g the example below will register and autowire all types that implement `ICommand` in the current assembly: ```csharp GetType().Assembly.GetTypes() .Where(x => x.IsOrHasGenericInterfaceTypeOf(typeof(ICommand<>))) .Each(x => container.AddSingleton(x)); ``` ## Use another IoC container ```csharp public interface IResolver { T TryResolve(); } public interface IContainerAdapter : IResolver { T Resolve(); } ``` #### Example Usage ```csharp IKernel kernel = new StandardKernel(); container.Adapter = new NinjectIocAdapter(kernel); ``` Adding a custom ContainerAdapter allows your services to resolve its dependencies from an alternative IOC, in this way they act like a **dependency repository** where the services are still registered in Funq but all their dependencies are resolved by the ContainerAdapter specified. Dependencies in constructors are resolved by calling `IContainerAdapter.Resolve()` whilst public property dependencies are resolved with `IContainerAdapter.TryResolve()`, the idea is you can have missing constructor dependencies throw an exception whilst you can be more lax about property dependencies, where your service can continue to execute without them (should you wish). Here are some example how to use some popular IoC containers side-by-side with Funq. Of course you're not only limited to the these IoC containers here: ### Use Ninject ```csharp public class NinjectIocAdapter : IContainerAdapter { private readonly IKernel kernel; public NinjectIocAdapter(IKernel kernel) { this.kernel = kernel; } public T Resolve() { return this.kernel.Get(); } public T TryResolve() { return this.kernel.CanResolve() ? this.kernel.Get() : default(T); } } ``` Then in the `AppHost` `Configure(Container container)` method you need to enable this adapter: ```csharp //Create Ninject IoC container IKernel kernel = new StandardKernel(); //Now register all depedencies to your custom IoC container //... //Register Ninject IoC container, so ServiceStack can use it container.Adapter = new NinjectIocAdapter(kernel); ``` ### Use StructureMap ```csharp public class StructureMapContainerAdapter : IContainerAdapter { public T TryResolve() { return ObjectFactory.TryGetInstance(); } public T Resolve() { return ObjectFactory.TryGetInstance(); } } ``` In `AppHost` `Configure`: ```csharp //Configure User Defined REST Paths container.Adapter = new StructureMapContainerAdapter(); //Register your dependencies ObjectFactory.Inject(typeof(IFoo), new Foo()); ``` ::: info Due to a behavior of StructureMap, you need your `AppHost` declare as `internal`, eg: `internal class AppHost : AppHostBase` ::: ### Use Windsor ```csharp // The "ApplicationAssemblyFilter" is a custom class that just helps to // automate registration to assemblies which match a particular pattern // in the app path public class ApplicationAssemblyFilter : AssemblyFilter { public ApplicationAssemblyFilter() : base(AppDomain.CurrentDomain.BaseDirectory, Assembly.GetExecutingAssembly().GetName().Name + ".*.dll"){} } public class WindsorContainerAdapter : IContainerAdapter, IDisposable { private readonly IWindsorContainer container; public WindsorContainerAdapter() { container = new WindsorContainer().Install(FromAssembly.InThisApplication(), FromAssembly.InDirectory(new ApplicationAssemblyFilter())); } public T TryResolve() { if (container.Kernel.HasComponent(typeof(T))) { return (T)container.Resolve(typeof(T)); } return default(T); } public T Resolve() { return container.Resolve(); } public void Dispose() { container.Dispose(); } } ``` ### Use Autofac ```csharp public class AutofacIocAdapter : IContainerAdapter { private readonly Autofac.IContainer container; public AutofacIocAdapter(Autofac.IContainer container) => this.container = container; public T TryResolve() { if (container.TryResolve(out var scope) && scope.TryResolve(typeof(T), out var scopeComponent)) return (T)scopeComponent; if (container.TryResolve(typeof(T), out var component)) return (T)component; return default; } public T Resolve() { var ret = TryResolve(); return !ret.Equals(default) ? ret : throw new Exception($"Error trying to resolve '{typeof(T).Name}'"); } } ``` Then in the `AppHost` `Configure(Container container)` method you need to enable this adapter: ```csharp //Create Autofac builder var builder = new ContainerBuilder(); //Now register all depedencies to your custom IoC container //... //Register Autofac IoC container adapter, so ServiceStack can use it IContainerAdapter adapter = new AutofacIocAdapter(builder.Build()); container.Adapter = adapter; ``` ### Use SimpleInjector ```csharp public class SimpleInjectorIocAdapter : IContainerAdapter { private readonly Container container; public SimpleInjectorIocAdapter(Container container) { this.container = container; } public T Resolve() { return (T)this.container.GetInstance(typeof(T)); } public T TryResolve() { var registration = this.container.GetRegistration(typeof(T)); return registration == null ? default(T) : (T)registration.GetInstance(); } } ``` Then in the `AppHost` `Configure(Container container)` method you need to enable this adapter: ```csharp //Create SimpleInjector IoC container Container container = new Container(); //Now register all depedencies to your custom IoC container //... //Register SimpleInjector IoC container, so ServiceStack can use it container.Adapter = new SimpleInjectorIocAdapter (container); ``` ### Use Unity ```csharp public class UnityIocAdapter : IContainerAdapter { private readonly IUnityContainer container; public UnityIocAdapter(IUnityContainer container) { this.container = container; } public T Resolve() { return container.Resolve(); } public T TryResolve() { if(container.IsRegistered()) { return container.Resolve(); } return default(T); } } ``` Then in the `AppHost` `Configure(Container container)` method you need to enable this adapter: ```csharp //Create Unity IoC container var unityContainer = new UnityContainer(); //Now register all depedencies to your custom IoC container //... //Register SimpleInjector IoC container, so ServiceStack can use it container.Adapter = new UnityIocAdapter(unityContainer); ``` ### Disposing of your services The `AppHost.Release(instance)` method gets called for every resolved service right after it's used. This is the [default implementation](https://github.com/ServiceStack/ServiceStack/blob/f2d8eadaf5f4eecf1eadae918243472b039e5dfe/src/ServiceStack/ServiceStackHost.cs#L787-L805) (which can be overridden): ```csharp var iocAdapterReleases = Container.Adapter as IRelease; if (iocAdapterReleases != null) { iocAdapterReleases.Release(instance); } else { var disposable = instance as IDisposable; if (disposable != null) disposable.Dispose(); } ``` Which will first try to call your ContainerAdapter if it implements `IRelease` otherwise if the service is `IDisposable` it will just dispose of it itself. So to have resolved services released back into your IOC, implement the `IRelease` interface on your IContainerAdapter, e.g: ```csharp public class WindsorContainerAdapter : IContainerAdapter, IRelease { private readonly IWindsorContainer _container; public void Release(object instance) { _container.Release(instance); } } ``` Otherwise you can override default implementation in your `AppHost.Release(instance)` if you want to do something other than the default implementation. # Community Resources - [ServiceStack: IoC with Microsoft Unity](http://www.agile-code.com/blog/servicestack-ioc-with-microsoft-unity/) by [@zoranmax](https://twitter.com/zoranmax) - [ServiceStack extensibility using MEF](http://bhameyie.com/2013/09/03/servicestack-extensibility-using-mef/) by [@bhameyie](https://twitter.com/bhameyie)