This repository was archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathContainerServiceProvider.cs
More file actions
116 lines (102 loc) · 3.66 KB
/
ContainerServiceProvider.cs
File metadata and controls
116 lines (102 loc) · 3.66 KB
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
namespace Microsoft.AspNet.WebFormsDependencyInjection.CastleWindsor
{
using global::Castle.MicroKernel.Registration;
using global::Castle.MicroKernel.Resolvers;
using global::Castle.Windsor;
using global::Castle.MicroKernel;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Hosting;
/// <summary>
/// The Castle Windsor adapter for WebObjectActivator
/// </summary>
class ContainerServiceProvider : IServiceProvider, IRegisteredObject
{
private const int TypesCannontResolveCacheCap = 100000;
private readonly IServiceProvider _next;
private readonly ConcurrentDictionary<Type, bool> _typesCannotResolve = new ConcurrentDictionary<Type, bool>();
public IWindsorContainer Container { get; internal set; }
public ContainerServiceProvider(IServiceProvider next)
{
_next = next;
HostingEnvironment.RegisterObject(this);
Container = new WindsorContainer();
Container.Register(Component.For<ILazyComponentLoader>().ImplementedBy<WebFormsComponentsLoader>());
}
/// <summary>
/// Implementation of IServiceProvider. Asp.net will call this method to
/// create the instances of Page/UserControl/HttpModule etc.
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
public object GetService(Type serviceType)
{
//
// Try unresolvable types
if (_typesCannotResolve.ContainsKey(serviceType))
{
return DefaultCreateInstance(serviceType);
}
//
// Try the container
object result = null;
//
// registered component or WebForms type
if (Container.Kernel.HasComponent(serviceType) || serviceType.IsWebFormsComponent())
{
try
{
result = Container.Resolve(serviceType);
}
catch (ComponentNotFoundException)
{
// Ignore and continue
}
}
//
// Try the next provider
if (result == null)
{
result = _next?.GetService(serviceType);
}
//
// Default activation
if (result == null)
{
if ((result = DefaultCreateInstance(serviceType)) != null)
{
// Cache it so we don't need to bother container again
if (_typesCannotResolve.Count < TypesCannontResolveCacheCap)
{
_typesCannotResolve.TryAdd(serviceType, true);
}
}
}
return result;
}
public void Stop(bool immediate)
{
HostingEnvironment.UnregisterObject(this);
Container.Dispose();
}
internal IServiceProvider NextServiceProvider
{
get { return _next; }
}
internal IDictionary<Type, bool> TypeCannotResolveDictionary
{
get { return _typesCannotResolve; }
}
protected virtual object DefaultCreateInstance(Type type)
{
return Activator.CreateInstance(
type,
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.CreateInstance,
null,
null,
null);
}
}
}