Unity.Microsoft.DependencyInjection package
Перейти к файлу
Eugene Sadovoi a3b231fd29
Update README.md
2019-12-11 22:58:57 -08:00
src Fixed #70 2019-12-11 22:47:43 -08:00
tests Switching to preview build 2019-12-08 13:14:34 -08:00
.gitignore Initial commit 2017-10-27 09:11:50 +02:00
LICENSE Initial commit 2017-10-27 09:11:50 +02:00
README.md Update README.md 2019-12-11 22:58:57 -08:00
appveyor.yml Latest 2019-12-08 13:46:49 -08:00
package.props Fixed #70 2019-12-11 22:47:43 -08:00
package.sln Merged test from James Doran 2019-12-06 13:19:24 -08:00

README.md

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 initialize 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>();
}

Resolving Controllers from Unity

By default ASP resolves controllers using built in activator. To enable resolution of controllers from Unity you need to add following line to MVC configuration:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddControllersAsServices(); <-- Add this line
    ...
}

Examples

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