/DS.Unity.Extensions.DependencyInjection

An unofficial Unity implementation of the interfaces in Microsoft.Extensions.DependencyInjection.Abstractions

Primary LanguageC#

DS.Unity.Extensions.DependencyInjection NuGet Version

An unofficial Unity 4.0.1 implementation of the interfaces in Microsoft.Extensions.DependencyInjection.Abstractions

For Unity 5+ please use official implementation

Get Started

  • Reference the DS.Unity.Extensions.DependencyInjection package from NuGet.

First way:

  • In the WebHostBuilder add ConfigureServices(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>();
}

Second way:

  • In the ConfigureServices method of your Startup class...
    • Register services from the IServiceCollection.
    • Build your container.
    • Create an UnityServiceProvider using the container and return it.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.AddMvc();
  
  var container = new UnityContainer();
  container.Populate(services);
  
  container.RegisterType<IMyService, MyService>();
  
  return new UnityServiceProvider(container);
}