1
1
using System ;
2
+ using System . Diagnostics ;
2
3
using System . Linq ;
3
4
using System . Reflection ;
5
+ using System . Security . Cryptography . X509Certificates ;
4
6
using Microsoft . Extensions . DependencyInjection ;
5
7
6
8
namespace Coderr . Server . Abstractions . Boot
7
9
{
8
10
public static class RegisterExtensions
9
11
{
12
+
10
13
public static void RegisterContainerServices ( this IServiceCollection serviceCollection , Assembly assembly )
11
14
{
12
- var containerServices = assembly . GetTypes ( ) ;
15
+ var gotWrongAttribute = (
16
+ from type in assembly . GetTypes ( )
17
+ let attributes = type . GetCustomAttributes ( )
18
+ where attributes . Count ( x => x . GetType ( ) . FullName == "Griffin.Container.ContainerService" ) > 0
19
+ select type ) . ToList ( ) ;
20
+ if ( gotWrongAttribute . Count > 0 )
21
+ {
22
+ throw new InvalidOperationException (
23
+ $ "Types \' { string . Join ( "," , gotWrongAttribute ) } \' was decorated with the wrong attribute") ;
24
+ }
25
+
26
+ var containerServices = assembly . GetTypes ( )
27
+ . Where ( x => x . GetCustomAttribute < ContainerServiceAttribute > ( ) != null ) ;
28
+
13
29
foreach ( var containerService in containerServices )
14
30
{
15
- var gotWrongAttribute = containerService
16
- . GetCustomAttributes ( )
17
- . Count ( x => x . GetType ( ) . FullName == "Griffin.Container.ContainerService" ) == 1 ;
18
- if ( gotWrongAttribute )
19
- {
20
- throw new InvalidOperationException (
21
- $ "Type \' { containerService } \' was decorated with the wrong attribute") ;
22
- ;
23
- }
31
+ Debug . WriteLine ( Assembly . GetCallingAssembly ( ) . GetName ( ) . Name + " registers " + containerService . FullName ) ;
24
32
25
33
var attr = containerService . GetCustomAttribute < ContainerServiceAttribute > ( ) ;
26
- if ( attr == null )
27
- continue ;
28
-
29
34
var interfaces = containerService . GetInterfaces ( ) ;
35
+ var lifetime = ConvertLifetime ( attr ) ;
30
36
31
37
// Hack so that the same instance is resolved for each interface
38
+ bool isRegisteredAsSelf = false ;
32
39
if ( interfaces . Length > 1 || attr . RegisterAsSelf )
33
- serviceCollection . RegisterService ( attr , containerService , containerService ) ;
40
+ {
41
+ serviceCollection . Add ( new ServiceDescriptor ( containerService , containerService , lifetime ) ) ;
42
+ isRegisteredAsSelf = true ;
43
+ }
44
+
34
45
35
46
foreach ( var @interface in interfaces )
36
47
{
37
- serviceCollection . RegisterService ( attr , @interface , containerService ) ;
48
+ var sd = isRegisteredAsSelf
49
+ ? new ServiceDescriptor ( @interface , x => x . GetService ( containerService ) , lifetime ) // else we don't get the same instance in the scope.
50
+ : new ServiceDescriptor ( @interface , containerService , lifetime ) ;
51
+ serviceCollection . Add ( sd ) ;
38
52
}
39
53
}
40
54
}
@@ -47,27 +61,33 @@ public static void RegisterMessageHandlers(this IServiceCollection serviceCollec
47
61
. ToList ( ) ;
48
62
foreach ( var type in types )
49
63
{
64
+ Debug . WriteLine ( Assembly . GetCallingAssembly ( ) . GetName ( ) . Name + " registers " + type . FullName ) ;
65
+
50
66
serviceCollection . AddScoped ( type , type ) ;
51
67
52
68
var ifs = type . GetInterfaces ( )
53
69
. Where ( x => x . Name . Contains ( "IMessageHandler" ) || x . Name . Contains ( "IQueryHandler" ) )
54
70
. ToList ( ) ;
55
71
foreach ( var @if in ifs )
56
72
{
57
- serviceCollection . AddScoped ( @if , type ) ;
73
+ serviceCollection . AddScoped ( @if , x => x . GetService ( type ) ) ;
58
74
}
59
75
}
60
76
}
61
77
62
- private static void RegisterService ( this IServiceCollection serviceCollection , ContainerServiceAttribute attr ,
63
- Type service , Type implementation )
78
+ private static ServiceLifetime ConvertLifetime ( ContainerServiceAttribute attr )
64
79
{
65
80
if ( attr . IsSingleInstance )
66
- serviceCollection . AddSingleton ( service , implementation ) ;
67
- else if ( attr . IsTransient )
68
- serviceCollection . AddTransient ( service , implementation ) ;
69
- else
70
- serviceCollection . AddScoped ( service , implementation ) ;
81
+ {
82
+ return ServiceLifetime . Singleton ;
83
+ }
84
+
85
+ if ( attr . IsTransient )
86
+ {
87
+ return ServiceLifetime . Transient ;
88
+ }
89
+
90
+ return ServiceLifetime . Scoped ;
71
91
}
72
92
}
73
93
}
0 commit comments