2017-12-28 16:23:55 +03:00
[![Build status ](https://ci.appveyor.com/api/projects/status/sevk2yb2jokf8ltr/branch/master?svg=true )](https://ci.appveyor.com/project/IoC-Unity/microsoft-dependency-injection/branch/master)
2018-01-04 04:09:34 +03:00
[![License ](https://img.shields.io/badge/license-apache%202.0-60C060.svg )](https://github.com/IoC-Unity/microsoft-dependency-injection/blob/master/LICENSE)
2018-01-04 04:08:59 +03:00
[![NuGet ](https://img.shields.io/nuget/dt/Unity.Microsoft.DependencyInjection.svg )](https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection)
[![NuGet ](https://img.shields.io/nuget/v/Unity.Microsoft.DependencyInjection.svg )](https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection)
2017-12-28 16:23:55 +03:00
2018-01-04 04:08:59 +03:00
# Unity.Microsoft.DependencyInjection
2020-05-14 23:37:37 +03:00
2018-01-10 05:17:43 +03:00
Unity extension to integrate with [Microsoft.Extensions.DependencyInjection ](https://github.com/aspnet/DependencyInjection ) compliant systems
2018-01-05 21:01:39 +03:00
2018-01-12 03:59:14 +03:00
## Getting Started
2020-05-14 23:37:37 +03:00
2018-01-05 21:01:39 +03:00
- Reference the `Unity.Microsoft.DependencyInjection` package from NuGet.
2020-05-14 23:37:37 +03:00
```shell
2018-01-05 21:01:39 +03:00
Install-Package Unity.Microsoft.DependencyInjection
```
2018-01-10 05:10:10 +03:00
## Registration:
2020-05-14 23:37:37 +03:00
2018-01-10 05:17:43 +03:00
- In the `WebHostBuilder` add `UseUnityServiceProvider(...)` method
2018-01-05 21:01:39 +03:00
```C#
2019-12-12 10:00:44 +03:00
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseUnityServiceProvider() < ---- Add this line
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup< Startup > ();
});
2018-01-05 21:01:39 +03:00
```
2018-01-10 23:59:22 +03:00
2018-04-26 16:01:28 +03:00
- In case Unity container configured via application configuration or by convention this container could be used to initialize service provider.
2018-01-10 23:59:22 +03:00
```C#
2019-12-12 10:00:44 +03:00
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
2020-11-24 15:51:32 +03:00
.UseUnityServiceProvider(_container) //< ---- Add this line
2019-12-12 10:00:44 +03:00
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup< Startup > ();
});
2018-01-10 23:59:22 +03:00
```
2018-01-10 23:07:41 +03:00
- Add optional method to your `Startup` class
2020-05-14 23:37:37 +03:00
2018-01-05 21:01:39 +03:00
```C#
public void ConfigureContainer(IUnityContainer container)
{
2018-01-10 05:10:10 +03:00
// Could be used to register more types
2018-01-05 21:01:39 +03:00
container.RegisterType< IMyService , MyService > ();
}
```
2019-01-23 03:22:18 +03:00
### 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:
2020-05-14 23:37:37 +03:00
2019-01-23 03:22:18 +03:00
```C#
public void ConfigureServices(IServiceCollection services)
{
2019-12-09 00:27:32 +03:00
...
2020-11-24 15:51:32 +03:00
services.AddMvc()
.AddControllersAsServices() //< -- Add this line
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
2019-12-09 00:27:32 +03:00
...
2019-01-23 03:22:18 +03:00
}
```
2018-01-11 01:31:53 +03:00
2018-01-11 01:32:32 +03:00
## Examples
2019-12-12 10:01:44 +03:00
For example of using Unity with Core 3.1 Web application follow [this link ](https://github.com/unitycontainer/examples/tree/master/src/web/ASP.Net.Unity.Example )
2018-01-05 21:01:39 +03:00
2020-05-14 23:37:37 +03:00
## Code of Conduct
This project has adopted the code of conduct defined by the [Contributor Covenant ](https://www.contributor-covenant.org/ ) to clarify expected behavior in our community. For more information, see the [.NET Foundation Code of Conduct ](https://www.dotnetfoundation.org/code-of-conduct )
## Contributing
See the [Contributing guide ](https://github.com/unitycontainer/unity/blob/master/CONTRIBUTING.md ) for more information.
## .NET Foundation
Unity Container is a [.NET Foundation ](https://dotnetfoundation.org/projects/unitycontainer ) project