An unofficial Unity 4.0.1 implementation of the interfaces in Microsoft.Extensions.DependencyInjection.Abstractions
For Unity 5+ please use official implementation
- Reference the
DS.Unity.Extensions.DependencyInjectionpackage from NuGet.
- In the
WebHostBuilderaddConfigureServices(services => services.AddUnity())method
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddUnity())
.UseStartup<Startup>()
.Build();- Add method to your
Startupclass
public void ConfigureContainer(IUnityContainer container)
{
container.RegisterType<IMyService, MyService>();
}- In the
ConfigureServicesmethod of yourStartupclass...- Register services from the
IServiceCollection. - Build your container.
- Create an
UnityServiceProviderusing the container and return it.
- Register services from the
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var container = new UnityContainer();
container.Populate(services);
container.RegisterType<IMyService, MyService>();
return new UnityServiceProvider(container);
}