44
55namespace Unity . Microsoft . DependencyInjection
66{
7- public class ServiceProvider : IServiceProvider ,
8- IServiceScopeFactory ,
9- IServiceScope ,
7+ public class ServiceProvider : IServiceProvider ,
8+ IServiceScopeFactory ,
9+ IServiceScope ,
1010 IDisposable
1111 {
12- private readonly UnityConfigurationOptions _options ;
12+ private IUnityContainer _container ;
1313
14- internal ServiceProvider ( UnityConfigurationOptions options )
14+
15+ internal ServiceProvider ( IUnityContainer container )
1516 {
16- _options = options ;
17- _options . UnityContainer . RegisterInstance < IServiceScope > ( this , new ExternallyControlledLifetimeManager ( ) ) ;
18- _options . UnityContainer . RegisterInstance < IServiceProvider > ( this , new ExternallyControlledLifetimeManager ( ) ) ;
19- _options . UnityContainer . RegisterInstance < IServiceScopeFactory > ( this , new ExternallyControlledLifetimeManager ( ) ) ;
17+ _container = container ;
18+ _container . RegisterInstance < IServiceScope > ( this , new ExternallyControlledLifetimeManager ( ) ) ;
19+ _container . RegisterInstance < IServiceProvider > ( this , new ExternallyControlledLifetimeManager ( ) ) ;
20+ _container . RegisterInstance < IServiceScopeFactory > ( this , new ExternallyControlledLifetimeManager ( ) ) ;
2021 }
2122
2223 #region IServiceProvider
2324
2425 public object GetService ( Type serviceType )
2526 {
26- ResolutionParameters parameters = new ResolutionParameters { Type = serviceType } ;
27-
28- _options . ResolveConfiguration ? . Invoke ( parameters ) ;
29-
3027 try
3128 {
32- return _options . UnityContainer . Resolve ( parameters . Type , parameters . Name , parameters . ResolverOverrides ?? Array . Empty < Resolution . ResolverOverride > ( ) ) ;
33- }
34- catch
35- {
36- parameters . ResolutionFailureHanlder ? . Invoke ( serviceType ) ;
29+ return _container . Resolve ( serviceType ) ;
3730 }
31+ catch { /* Ignore */ }
3832
3933 return null ;
4034 }
@@ -46,10 +40,7 @@ public object GetService(Type serviceType)
4640
4741 public IServiceScope CreateScope ( )
4842 {
49- var childOptions = _options . With ( _options . UnityContainer . CreateChildContainer ( ) ) ;
50- childOptions . CreateScope ? . Invoke ( childOptions ) ;
51-
52- return new ServiceProvider ( childOptions ) ;
43+ return new ServiceProvider ( _container . CreateChildContainer ( ) ) ;
5344 }
5445
5546 #endregion
@@ -66,15 +57,13 @@ public IServiceScope CreateScope()
6657
6758 public static IServiceProvider ConfigureServices ( IServiceCollection services )
6859 {
69- var container = new UnityContainer ( ) . AddExtension ( new MdiExtension ( ) )
70- . AddServices ( services ) ;
71-
72- return new ServiceProvider ( new UnityConfigurationOptions { UnityContainer = container } ) ;
60+ return new ServiceProvider ( new UnityContainer ( ) . AddExtension ( new MdiExtension ( ) )
61+ . AddServices ( services ) ) ;
7362 }
7463
7564 public static explicit operator UnityContainer ( ServiceProvider c )
7665 {
77- return ( UnityContainer ) c . _options . UnityContainer ;
66+ return ( UnityContainer ) c . _container ;
7867 }
7968
8069 #endregion
@@ -88,14 +77,11 @@ public void Dispose()
8877 GC . SuppressFinalize ( this ) ;
8978 }
9079
91- protected virtual void Dispose ( bool _ )
92- {
93- _options . UnityContainer ? . Dispose ( ) ;
94- }
95-
96- ~ ServiceProvider ( )
80+ private void Dispose ( bool _ )
9781 {
98- Dispose ( false ) ;
82+ IDisposable disposable = _container ;
83+ _container = null ;
84+ disposable ? . Dispose ( ) ;
9985 }
10086
10187 #endregion
0 commit comments