/microsoft-dependency-injection

Unity.Microsoft.DependencyInjection package

Primary LanguageC#Apache License 2.0Apache-2.0

Build status License NuGet NuGet

Unity.Microsoft.DependencyInjection

Unity extension to integrate with Microsoft.Extensions.DependencyInjection compliant systems

Getting Started

  • Reference the Unity.Microsoft.DependencyInjection package from NuGet.
Install-Package Unity.Microsoft.DependencyInjection

Registration:

  • In the WebHostBuilder add UseUnityServiceProvider(...) method
public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
           .UseUnityServiceProvider()   <---- Add this line
           .UseStartup<Startup>()
           .Build();
  • In case Unity container configured via application configuration or by convention this container could be used to initalize service provider.
public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
           .UseUnityServiceProvider(_container)   <---- or add this line
           .UseStartup<Startup>()
           .Build();
  • Add optional method to your Startup class
public void ConfigureContainer(IUnityContainer container)
{
  // Could be used to register more types
  container.RegisterType<IMyService, MyService>();
}

Startup

At the moment it is not possible to resolve Startup class from Unity even if it is configured as default container. An Issue has been filed with ASPNET team to fix it. Once it is resolved it would be possible to resolve Startap class itself from the Unity container.

Examples

For example of using Unity with Core 2.0 Web application follow this link