File tree Expand file tree Collapse file tree 3 files changed +92
-1
lines changed Expand file tree Collapse file tree 3 files changed +92
-1
lines changed Original file line number Diff line number Diff line change 44[ ![ NuGet] ( https://img.shields.io/nuget/v/Unity.Microsoft.DependencyInjection.svg )] ( https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection )
55
66# Unity.Microsoft.DependencyInjection
7- Unity extension to integrate with Microsoft.Extensions.DependencyInjection compliant systems
7+ Unity extension to integrate with [ Microsoft.Extensions.DependencyInjection.Abstractions] ( https://github.com/aspnet/DependencyInjection ) compliant systems
8+
9+ ## Get Started
10+ - Reference the ` Unity.Microsoft.DependencyInjection ` package from NuGet.
11+ ```
12+ Install-Package Unity.Microsoft.DependencyInjection
13+ ```
14+
15+ ## First way:
16+ - In the ` WebHostBuilder ` add ` ConfigureServices(services => services.AddUnity()) ` method
17+
18+ ``` C#
19+ public static IWebHost BuildWebHost (string [] args ) =>
20+ WebHost .CreateDefaultBuilder (args )
21+ .ConfigureServices (services => services .AddUnity ())
22+ .UseStartup <Startup >()
23+ .Build ();
24+ ```
25+ - Add method to your ` Startup ` class
26+ ``` C#
27+ public void ConfigureContainer (IUnityContainer container )
28+ {
29+ container .RegisterType <IMyService , MyService >();
30+ }
31+ ```
32+
33+ ## Second way:
34+ - In the ` ConfigureServices ` method of your ` Startup ` class...
35+ - Register services from the ` IServiceCollection ` .
36+ - Build your container.
37+ - Call ` ConfigureServices ` extension on ` IUnityContainer ` and return it.
38+
39+ ``` C#
40+ public IServiceProvider ConfigureServices (IServiceCollection services )
41+ {
42+ services .AddMvc ();
43+
44+ var container = new UnityContainer ();
45+
46+ container .RegisterType <IMyService , MyService >();
47+
48+ return container .ConfigureServices (services );
49+ }
50+ ```
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Text ;
4+ using Microsoft . Extensions . DependencyInjection ;
5+
6+ namespace Unity . Microsoft . DependencyInjection
7+ {
8+ public static class ServiceCollectionExtensions
9+ {
10+ public static IServiceCollection AddUnity ( this IServiceCollection services , Action < IUnityContainer > configurationAction = null )
11+ {
12+ return services . AddSingleton < IServiceProviderFactory < IUnityContainer > > ( new ServiceProviderFactory ( configurationAction ) ) ;
13+ }
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Text ;
4+ using Microsoft . Extensions . DependencyInjection ;
5+
6+ namespace Unity . Microsoft . DependencyInjection
7+ {
8+ internal class ServiceProviderFactory : IServiceProviderFactory < IUnityContainer >
9+ {
10+ private readonly Action < IUnityContainer > _configurationAction ;
11+
12+ public ServiceProviderFactory ( Action < IUnityContainer > configurationAction = null )
13+ {
14+ _configurationAction = configurationAction ?? ( container => { } ) ;
15+ }
16+
17+ public IUnityContainer CreateBuilder ( IServiceCollection serviceCollection )
18+ {
19+ var unityContainer = new UnityContainer ( ) ;
20+
21+ unityContainer . Configure ( serviceCollection ) ;
22+
23+ _configurationAction ( unityContainer ) ;
24+
25+ return unityContainer ;
26+ }
27+
28+ public IServiceProvider CreateServiceProvider ( IUnityContainer unityContainer )
29+ {
30+ return new ServiceProvider ( unityContainer ) ;
31+ }
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments