2
2
{
3
3
using System ;
4
4
using Internal ;
5
+ using Internal . Contracts ;
5
6
using Microsoft . Extensions . Caching . Memory ;
6
7
using Microsoft . Extensions . DependencyInjection ;
7
8
using Internal . Services ;
8
9
using Microsoft . Extensions . Caching . Distributed ;
10
+ using Utilities . Validators ;
9
11
10
12
public class CachingTestPlugin : IServiceRegistrationPlugin
11
13
{
@@ -15,6 +17,9 @@ public class CachingTestPlugin : IServiceRegistrationPlugin
15
17
private readonly Type defaultDistributedCachingServiceType = typeof ( IDistributedCache ) ;
16
18
private readonly Type defaultDistributedCachingImplementationType = typeof ( MemoryDistributedCache ) ;
17
19
20
+ private bool shouldReplaceMemoryCache = false ;
21
+ private bool shouldReplaceDistributedCache = false ;
22
+
18
23
public Func < ServiceDescriptor , bool > ServiceSelectorPredicate
19
24
=> serviceDescriptor =>
20
25
{
@@ -26,6 +31,16 @@ public Func<ServiceDescriptor, bool> ServiceSelectorPredicate
26
31
serviceDescriptor . ImplementationType == this . defaultCachingImplementationType ||
27
32
serviceDescriptor . ImplementationType == this . defaultDistributedCachingImplementationType ;
28
33
34
+ if ( serviceDescriptor . ServiceType == this . defaultCachingServiceType )
35
+ {
36
+ this . shouldReplaceMemoryCache = serviceDescriptor . ImplementationType == defaultCachingImplementationType ;
37
+ }
38
+
39
+ if ( serviceDescriptor . ServiceType == this . defaultDistributedCachingServiceType )
40
+ {
41
+ this . shouldReplaceDistributedCache = serviceDescriptor . ImplementationType == defaultDistributedCachingImplementationType ;
42
+ }
43
+
29
44
30
45
return isValidServiceType && isValidImplementationType ;
31
46
} ;
@@ -36,10 +51,19 @@ public Action<IServiceCollection> ServiceRegistrationDelegate
36
51
{
37
52
return serviceCollection =>
38
53
{
39
- serviceCollection . ReplaceMemoryCache ( ) ;
40
- serviceCollection . ReplaceDistributedCache ( ) ;
54
+ CommonValidator . CheckForNullReference ( serviceCollection ) ;
55
+
56
+ if ( this . shouldReplaceMemoryCache )
57
+ {
58
+ serviceCollection . ReplaceMemoryCache ( ) ;
59
+ TestHelper . GlobalTestCleanup += ( ) => TestServiceProvider . GetService < IMemoryCache > ( ) ? . Dispose ( ) ;
60
+ }
41
61
42
- TestHelper . GlobalTestCleanup += ( ) => TestServiceProvider . GetService < IMemoryCache > ( ) ? . Dispose ( ) ;
62
+ if ( this . shouldReplaceDistributedCache )
63
+ {
64
+ serviceCollection . ReplaceDistributedCache ( ) ;
65
+ TestHelper . GlobalTestCleanup += ( ) => TestServiceProvider . GetService < IDistributedCacheMock > ( ) ? . Dispose ( ) ;
66
+ }
43
67
} ;
44
68
}
45
69
}
0 commit comments