@@ -22,13 +22,14 @@ namespace Castle.Facilities.WcfIntegration
22
22
using Castle . Core ;
23
23
using Castle . Core . Internal ;
24
24
using System ;
25
+ using System . Threading ;
25
26
26
- public class InMemoryServiceCatalog : IServiceCatalogImplementation
27
+ public class InMemoryServiceCatalog : IServiceCatalogImplementation
27
28
{
28
29
private readonly List < ILoadBalancePolicy > policies ;
29
30
private readonly ILoadBalancePolicyFactory policyFactory ;
30
31
private readonly Dictionary < EndpointAddress , EndpointDiscoveryMetadata > endpoints ;
31
- private readonly Lock @lock = Lock . Create ( ) ;
32
+ private readonly ReaderWriterLockSlim @lock = new ReaderWriterLockSlim ( ) ;
32
33
33
34
public InMemoryServiceCatalog ( )
34
35
: this ( new ContractLoadBalancePolicyFactory < RoundRobinPolicy > ( ) )
@@ -44,84 +45,133 @@ public InMemoryServiceCatalog(ILoadBalancePolicyFactory policyFactory)
44
45
45
46
public virtual EndpointDiscoveryMetadata [ ] ListEndpoints ( )
46
47
{
47
- using ( @lock . ForReading ( ) )
48
+ @lock . EnterReadLock ( ) ;
49
+ try
48
50
{
49
51
return endpoints . Values . ToArray < EndpointDiscoveryMetadata > ( ) ;
50
52
}
53
+ finally
54
+ {
55
+ @lock . ExitReadLock ( ) ;
56
+ }
51
57
}
52
58
53
59
public virtual void FindEndpoints ( FindRequestContext findRequestContext )
54
60
{
55
- using ( @lock . ForReading ( ) )
56
- {
57
- foreach ( var endpoint in MatchTargets ( findRequestContext . Criteria ) )
58
- {
59
- findRequestContext . AddMatchingEndpoint ( endpoint ) ;
60
- }
61
- }
61
+ @lock . EnterReadLock ( ) ;
62
+ try
63
+ {
64
+ foreach ( var endpoint in MatchTargets ( findRequestContext . Criteria ) )
65
+ {
66
+ findRequestContext . AddMatchingEndpoint ( endpoint ) ;
67
+ }
68
+ }
69
+ finally
70
+ {
71
+ @lock . ExitReadLock ( ) ;
72
+ }
62
73
}
63
74
64
75
public virtual EndpointDiscoveryMetadata [ ] FindEndpoints ( FindCriteria criteria )
65
76
{
66
- using ( @lock . ForReading ( ) )
67
- {
68
- return MatchTargets ( criteria ) . ToArray ( ) ;
69
- }
77
+ @lock . EnterReadLock ( ) ;
78
+ try
79
+ {
80
+ return MatchTargets ( criteria ) . ToArray ( ) ;
81
+ }
82
+ finally
83
+ {
84
+ @lock . ExitReadLock ( ) ;
85
+ }
70
86
}
71
87
72
88
public virtual bool RegisterEndpoint ( EndpointDiscoveryMetadata endpoint )
73
89
{
74
90
var registered = false ;
75
91
if ( AcceptEndpoint ( endpoint ) )
76
92
{
77
- using ( var locker = @lock . ForReadingUpgradeable ( ) )
78
- {
79
- policies . ForEach ( policy => registered = registered | policy . RegisterTarget ( endpoint ) ) ;
80
-
81
- locker . Upgrade ( ) ;
82
-
83
- if ( registered == false )
84
- {
85
- var newPolicies = policyFactory . CreatePolicies ( endpoint ) ;
86
- Array . ForEach ( newPolicies , newPolicy =>
87
- {
88
- registered = registered | newPolicy . RegisterTarget ( endpoint ) ;
89
- policies . Add ( newPolicy ) ;
90
- } ) ;
91
- }
92
-
93
- if ( registered )
94
- endpoints [ endpoint . Address ] = endpoint ;
95
- }
93
+ @lock . EnterUpgradeableReadLock ( ) ;
94
+ try
95
+ {
96
+ policies . ForEach ( policy => registered = registered | policy . RegisterTarget ( endpoint ) ) ;
97
+ @lock . EnterWriteLock ( ) ;
98
+ try
99
+ {
100
+ if ( registered == false )
101
+ {
102
+ var newPolicies = policyFactory . CreatePolicies ( endpoint ) ;
103
+ Array . ForEach ( newPolicies , newPolicy =>
104
+ {
105
+ registered = registered | newPolicy . RegisterTarget ( endpoint ) ;
106
+ policies . Add ( newPolicy ) ;
107
+ } ) ;
108
+ }
109
+
110
+ if ( registered )
111
+ {
112
+ endpoints [ endpoint . Address ] = endpoint ;
113
+ }
114
+ }
115
+ finally
116
+ {
117
+ @lock . ExitWriteLock ( ) ;
118
+ }
119
+ }
120
+ finally
121
+ {
122
+ @lock . ExitUpgradeableReadLock ( ) ;
123
+ }
96
124
}
97
- return registered ;
125
+ return registered ;
98
126
}
99
127
100
128
public virtual bool RemoveEndpoint ( EndpointDiscoveryMetadata endpoint )
101
129
{
102
130
var removed = false ;
103
- using ( var locker = @lock . ForReadingUpgradeable ( ) )
131
+
132
+ @lock . EnterUpgradeableReadLock ( ) ;
133
+ try
104
134
{
105
135
policies . ForEach ( policy => removed = removed | policy . RemoveTarget ( endpoint ) ) ;
106
-
107
136
if ( removed )
108
137
{
109
- locker . Upgrade ( ) ;
110
- endpoints . Remove ( endpoint . Address ) ;
138
+ @lock . EnterWriteLock ( ) ;
139
+ try
140
+ {
141
+ endpoints . Remove ( endpoint . Address ) ;
142
+ }
143
+ finally
144
+ {
145
+ @lock . ExitWriteLock ( ) ;
146
+ }
111
147
}
148
+ }
149
+ finally
150
+ {
151
+ @lock . ExitUpgradeableReadLock ( ) ;
152
+ }
153
+
154
+ using ( var locker = Lock . Create ( ) . ForReadingUpgradeable ( ) )
155
+ {
156
+
112
157
}
113
158
return removed ;
114
159
}
115
160
116
161
public virtual EndpointDiscoveryMetadata ResolveEndpoint ( ResolveCriteria resolveCriteria )
117
162
{
118
- using ( @lock . ForReading ( ) )
119
- {
120
- EndpointDiscoveryMetadata endpoint ;
121
- return endpoints . TryGetValue ( resolveCriteria . Address , out endpoint )
122
- ? endpoint
123
- : null ;
124
- }
163
+ @lock . EnterReadLock ( ) ;
164
+ try
165
+ {
166
+ EndpointDiscoveryMetadata endpoint ;
167
+ return endpoints . TryGetValue ( resolveCriteria . Address , out endpoint )
168
+ ? endpoint
169
+ : null ;
170
+ }
171
+ finally
172
+ {
173
+ @lock . ExitReadLock ( ) ;
174
+ }
125
175
}
126
176
127
177
protected virtual bool AcceptEndpoint ( EndpointDiscoveryMetadata endpointDiscoveryMetadata )
0 commit comments