-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathServiceProvider.cs
130 lines (96 loc) · 3.48 KB
/
ServiceProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Microsoft.Extensions.DependencyInjection;
using System;
using Unity.Lifetime;
using Unity.Microsoft.DependencyInjection.Lifetime;
namespace Unity.Microsoft.DependencyInjection
{
public class ServiceProvider : IServiceProvider,
ISupportRequiredService,
IKeyedServiceProvider,
IServiceScopeFactory,
IServiceScope,
IDisposable
{
private IUnityContainer _container;
#if DEBUG
private string id = Guid.NewGuid().ToString();
#endif
internal ServiceProvider(IUnityContainer container)
{
_container = container;
_container.RegisterInstance<IServiceScope>(this, new ExternallyControlledLifetimeManager());
_container.RegisterInstance<IServiceProvider>(this, new ServiceProviderLifetimeManager(this));
_container.RegisterInstance<IServiceScopeFactory>(this, new ExternallyControlledLifetimeManager());
}
#region IServiceProvider
public object GetService(Type serviceType)
{
if (null == _container)
throw new ObjectDisposedException(nameof(IServiceProvider));
try
{
return _container.Resolve(serviceType, null);
}
catch { /* Ignore */}
return null;
}
public object GetRequiredService(Type serviceType)
{
if (null == _container)
throw new ObjectDisposedException(nameof(IServiceProvider));
return _container.Resolve(serviceType, null);
}
#endregion
#region IServiceScopeFactory
public IServiceScope CreateScope()
{
return new ServiceProvider(_container.CreateChildContainer());
}
#endregion
#region IServiceScope
IServiceProvider IServiceScope.ServiceProvider => this;
#endregion
#region Public Members
public static IServiceProvider ConfigureServices(IServiceCollection services)
{
return new ServiceProvider(new UnityContainer()
.AddExtension(new MdiExtension())
.AddServices(services));
}
public static explicit operator UnityContainer(ServiceProvider c)
{
return (UnityContainer)c._container;
}
#endregion
#region Disposable
protected virtual void Dispose(bool disposing)
{
IDisposable disposable = _container;
_container = null;
disposable?.Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public object GetKeyedService(Type serviceType, object serviceKey)
{
if (null == _container)
throw new ObjectDisposedException(nameof(IServiceProvider));
try
{
return _container.Resolve(serviceType, (string) serviceKey);
}
catch { /* Ignore */}
return null;
}
public object GetRequiredKeyedService(Type serviceType, object serviceKey)
{
if (null == _container)
throw new ObjectDisposedException(nameof(IServiceProvider));
return _container.Resolve(serviceType, (string) serviceKey);
}
#endregion
}
}