-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathContainerTypeExtensions.cs
More file actions
136 lines (117 loc) · 5.46 KB
/
Copy pathContainerTypeExtensions.cs
File metadata and controls
136 lines (117 loc) · 5.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Linq;
using Funq;
using ServiceStack.Web;
namespace ServiceStack
{
#if NETCORE
public interface IHasServiceScope : IServiceProvider
{
Microsoft.Extensions.DependencyInjection.IServiceScope ServiceScope { get; set; }
}
public static class ServiceScopeExtensions
{
public static Microsoft.Extensions.DependencyInjection.IServiceScope StartScope(this IRequest request)
{
if (request is IHasServiceScope hasScope)
{
var scopeFactory = (Microsoft.Extensions.DependencyInjection.IServiceScopeFactory)
hasScope.GetService(typeof(Microsoft.Extensions.DependencyInjection.IServiceScopeFactory));
if (scopeFactory == null)
return null;
var scope = scopeFactory.CreateScope();
hasScope.ServiceScope = scope;
return scope;
}
return null;
}
}
#endif
public static class ContainerTypeExtensions
{
/// <summary>
/// Registers the type in the IoC container and
/// adds auto-wiring to the specified type.
/// </summary>
public static void RegisterAutoWiredType(this Container container, Type serviceType, Type inFunqAsType,
ReuseScope scope = ReuseScope.None)
{
if (serviceType.IsAbstract || serviceType.ContainsGenericParameters)
return;
var methodInfo = typeof(Container).GetMethodInfo(nameof(Container.RegisterAutoWiredAs), Type.EmptyTypes);
var registerMethodInfo = methodInfo.MakeGenericMethod(new[] { serviceType, inFunqAsType });
var registration = registerMethodInfo.Invoke(container, TypeConstants.EmptyObjectArray) as IRegistration;
registration.ReusedWithin(scope);
}
/// <summary>
/// Registers a named instance of type in the IoC container and
/// adds auto-wiring to the specified type.
/// </summary>
public static void RegisterAutoWiredType(this Container container, string name, Type serviceType, Type inFunqAsType,
ReuseScope scope = ReuseScope.None)
{
if (serviceType.IsAbstract || serviceType.ContainsGenericParameters)
return;
var methodInfo = typeof(Container).GetMethodInfo("RegisterAutoWiredAs", new[] { typeof(string) });
var registerMethodInfo = methodInfo.MakeGenericMethod(serviceType, inFunqAsType);
var registration = registerMethodInfo.Invoke(container, new[] { name }) as IRegistration;
registration.ReusedWithin(scope);
}
/// <summary>
/// Registers the type in the IoC container and
/// adds auto-wiring to the specified type.
/// The reuse scope is set to none (transient).
/// </summary>
public static void RegisterAutoWiredType(this Container container, Type serviceType,
ReuseScope scope = ReuseScope.None)
{
//Don't try to register base service classes
if (serviceType.IsAbstract || serviceType.ContainsGenericParameters)
return;
var methodInfo = typeof(Container).GetMethodInfo("RegisterAutoWired", Type.EmptyTypes);
var registerMethodInfo = methodInfo.MakeGenericMethod(serviceType);
var registration = registerMethodInfo.Invoke(container, TypeConstants.EmptyObjectArray) as IRegistration;
registration.ReusedWithin(scope);
}
/// <summary>
/// Registers the type in the IoC container and
/// adds auto-wiring to the specified type.
/// The reuse scope is set to none (transient).
/// </summary>
public static void RegisterAutoWiredType(this Container container, string name, Type serviceType,
ReuseScope scope = ReuseScope.None)
{
//Don't try to register base service classes
if (serviceType.IsAbstract || serviceType.ContainsGenericParameters)
return;
var methodInfo = typeof(Container).GetMethodInfo("RegisterAutoWired", new[] { typeof(string) });
var registerMethodInfo = methodInfo.MakeGenericMethod(serviceType);
var registration = registerMethodInfo.Invoke(container, new[] { name }) as IRegistration;
registration.ReusedWithin(scope);
}
/// <summary>
/// Registers the types in the IoC container and
/// adds auto-wiring to the specified types.
/// The reuse scope is set to none (transient).
/// </summary>
public static void RegisterAutoWiredTypes(this Container container, IEnumerable<Type> serviceTypes,
ReuseScope scope = ReuseScope.None)
{
foreach (var serviceType in serviceTypes)
container.RegisterAutoWiredType(serviceType, scope);
}
/// <summary>
/// Register a singleton instance as a runtime type
/// </summary>
public static Container Register(this Container container, object instance, Type asType)
{
var mi = container.GetType()
.GetMethods()
.First(x => x.Name == "Register" && x.GetParameters().Length == 1 && x.ReturnType == typeof(void))
.MakeGenericMethod(asType);
mi.Invoke(container, new[] { instance });
return container;
}
}
}