unitycontainer / microsoft-dependency-injection

Unity.Microsoft.DependencyInjection package
Apache License 2.0
63 stars 36 forks source link

Build status License NuGet NuGet

Unity.Microsoft.DependencyInjection

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

Getting Started

Install-Package Unity.Microsoft.DependencyInjection

Registration:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseUnityServiceProvider()   <---- Add this line
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseUnityServiceProvider(_container)   //<---- Add this line
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
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.AddMvc()
        .AddControllersAsServices()  //<-- Add this line
        .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
    ...
}

Examples

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

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct

Contributing

See the Contributing guide for more information.

.NET Foundation

Unity Container is a .NET Foundation project