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.DependencyInjection
package from NuGet.
- In the
WebHostBuilder
addConfigureServices(services => services.AddUnity())
method
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddUnity())
.UseStartup<Startup>()
.Build();
- Add method to your
Startup
class
public void ConfigureContainer(IUnityContainer container)
{
container.RegisterType<IMyService, MyService>();
}
- In the
ConfigureServices
method of yourStartup
class...- Register services from the
IServiceCollection
. - Build your container.
- Create an
UnityServiceProvider
using 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);
}