Unity.Microsoft.DependencyInjection package
Перейти к файлу
Eugene Sadovoi 0a428ed1b8
Create FUNDING.yml
2021-06-14 10:03:42 -07:00
.github Create FUNDING.yml 2021-06-14 10:03:42 -07:00
src Copyright © .NET Foundation and Contributors. 2020-05-13 15:50:57 -07: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 Copyright © .NET Foundation and Contributors. 2020-05-13 15:50:57 -07:00
README.md Update the README for .NET 5.0 2020-11-24 13:51:32 +01:00
appveyor.yml Latest 2019-12-08 13:46:49 -08:00
package.props Releasing 5.11.6 2020-01-06 11:03:01 -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 IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseUnityServiceProvider()   <---- Add this line
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
  • In case Unity container configured via application configuration or by convention this container could be used to initialize service provider.
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseUnityServiceProvider(_container)   //<---- Add this line
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
  • 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.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