@@ -16,13 +16,15 @@ namespace Castle.Facilities.WcfIntegration
16
16
{
17
17
using System ;
18
18
using System . ServiceModel . Discovery ;
19
+ using System . Threading ;
20
+
19
21
using Castle . Core . Internal ;
20
22
21
23
public class DiscoveryProxyCache : DiscoveryEndpointProvider , IDisposable
22
24
{
23
25
private readonly DiscoveryEndpointProvider inner ;
24
26
private volatile DiscoveryEndpoint endpoint ;
25
- private readonly Lock @lock = Lock . Create ( ) ;
27
+ private readonly ReaderWriterLockSlim @lock = new ReaderWriterLockSlim ( ) ;
26
28
27
29
public DiscoveryProxyCache ( DiscoveryEndpointProvider inner )
28
30
{
@@ -36,36 +38,57 @@ public DiscoveryProxyCache(DiscoveryEndpointProvider inner)
36
38
37
39
public override DiscoveryEndpoint GetDiscoveryEndpoint ( )
38
40
{
39
- using ( var locker = @lock . ForReadingUpgradeable ( ) )
41
+ @lock . EnterUpgradeableReadLock ( ) ;
42
+ try
40
43
{
41
44
if ( endpoint != null )
42
45
return endpoint ;
46
+ @lock . EnterWriteLock ( ) ;
47
+ try
48
+ {
49
+ if ( endpoint == null )
50
+ endpoint = inner . GetDiscoveryEndpoint ( ) ;
43
51
44
- locker . Upgrade ( ) ;
45
-
46
- if ( endpoint == null )
47
- endpoint = inner . GetDiscoveryEndpoint ( ) ;
48
-
49
- return endpoint ;
52
+ return endpoint ;
53
+ }
54
+ finally
55
+ {
56
+ @lock . ExitWriteLock ( ) ;
57
+ }
58
+ }
59
+ finally
60
+ {
61
+ @lock . ExitUpgradeableReadLock ( ) ;
50
62
}
51
63
}
52
64
53
65
private void DiscoveryEndpointFaulted ( object sender , DiscoveryEndpointFaultEventArgs args )
54
66
{
55
- using ( var locker = @lock . ForReadingUpgradeable ( ) )
67
+ @lock . EnterUpgradeableReadLock ( ) ;
68
+ try
56
69
{
57
70
if ( args . Culprit != endpoint )
58
71
return ;
59
-
60
- locker . Upgrade ( ) ;
61
-
62
- endpoint = null ;
72
+ @lock . EnterWriteLock ( ) ;
73
+ try
74
+ {
75
+ endpoint = null ;
76
+ }
77
+ finally
78
+ {
79
+ @lock . ExitWriteLock ( ) ;
80
+ }
81
+ }
82
+ finally
83
+ {
84
+ @lock . ExitUpgradeableReadLock ( ) ;
63
85
}
64
86
}
65
87
66
88
void IDisposable . Dispose ( )
67
89
{
68
90
AbstractChannelBuilder . DiscoveryEndpointFaulted -= DiscoveryEndpointFaulted ;
91
+ @lock . Dispose ( ) ;
69
92
}
70
93
}
71
94
}
0 commit comments