https://github.com/unitycontainer/microsoft-dependency-injection
Unity.Microsoft.DependencyInjection package
https://github.com/unitycontainer/microsoft-dependency-injection
Last synced: 11 months ago
JSON representation
Unity.Microsoft.DependencyInjection package
- Host: GitHub
- URL: https://github.com/unitycontainer/microsoft-dependency-injection
- Owner: unitycontainer
- License: apache-2.0
- Created: 2017-10-20T00:31:08.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-05-12T14:08:23.000Z (almost 2 years ago)
- Last Synced: 2025-03-16T09:36:38.940Z (12 months ago)
- Language: C#
- Homepage:
- Size: 153 KB
- Stars: 63
- Watchers: 6
- Forks: 36
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://ci.appveyor.com/project/IoC-Unity/microsoft-dependency-injection/branch/master)
[](https://github.com/IoC-Unity/microsoft-dependency-injection/blob/master/LICENSE)
[](https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection)
[](https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection)
# Unity.Microsoft.DependencyInjection
Unity extension to integrate with [Microsoft.Extensions.DependencyInjection](https://github.com/aspnet/DependencyInjection) compliant systems
## Getting Started
- Reference the `Unity.Microsoft.DependencyInjection` package from NuGet.
```shell
Install-Package Unity.Microsoft.DependencyInjection
```
## Registration:
- In the `WebHostBuilder` add `UseUnityServiceProvider(...)` method
```C#
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseUnityServiceProvider() <---- Add this line
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});
```
- In case Unity container configured via application configuration or by convention this container could be used to initialize service provider.
```C#
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseUnityServiceProvider(_container) //<---- Add this line
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});
```
- Add optional method to your `Startup` class
```C#
public void ConfigureContainer(IUnityContainer container)
{
// Could be used to register more types
container.RegisterType();
}
```
### 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:
```C#
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](https://github.com/unitycontainer/examples/tree/master/src/web/ASP.Net.Unity.Example)
## 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