@@ -22,10 +22,7 @@ namespace Castle.MicroKernel.Lifestyle.Scoped
22
22
using System . Runtime . Remoting . Messaging ;
23
23
#endif
24
24
using System . Security ;
25
- #if ! FEATURE_REMOTING
26
25
using System . Threading ;
27
- #endif
28
-
29
26
using Castle . Core ;
30
27
using Castle . Core . Internal ;
31
28
@@ -50,7 +47,7 @@ public class CallContextLifetimeScope : ILifetimeScope
50
47
51
48
private readonly Guid contextId = Guid . NewGuid ( ) ;
52
49
private readonly CallContextLifetimeScope parentScope ;
53
- private readonly Lock @lock = Lock . Create ( ) ;
50
+ private readonly ReaderWriterLockSlim @lock = new ReaderWriterLockSlim ( ) ;
54
51
private ScopeCache cache = new ScopeCache ( ) ;
55
52
56
53
public CallContextLifetimeScope ( )
@@ -66,25 +63,37 @@ public CallContextLifetimeScope()
66
63
[ SecuritySafeCritical ]
67
64
public void Dispose ( )
68
65
{
69
- using ( var token = @lock . ForReadingUpgradeable ( ) )
66
+ @lock . EnterUpgradeableReadLock ( ) ;
67
+ try
70
68
{
71
69
// Dispose the burden cache
72
70
if ( cache == null ) return ;
73
- token . Upgrade ( ) ;
74
- cache . Dispose ( ) ;
75
- cache = null ;
76
-
77
- // Restore the parent scope (if inside one)
78
- if ( parentScope != null )
79
- {
80
- SetCurrentScope ( parentScope ) ;
81
- }
82
- else
71
+ @lock . EnterWriteLock ( ) ;
72
+ try
83
73
{
74
+ cache . Dispose ( ) ;
75
+ cache = null ;
76
+
77
+ // Restore the parent scope (if inside one)
78
+ if ( parentScope != null )
79
+ {
80
+ SetCurrentScope ( parentScope ) ;
81
+ }
82
+ else
83
+ {
84
84
#if FEATURE_REMOTING
85
- CallContext . FreeNamedDataSlot ( callContextKey ) ;
85
+ CallContext . FreeNamedDataSlot ( callContextKey ) ;
86
86
#endif
87
+ }
87
88
}
89
+ finally
90
+ {
91
+ @lock . ExitWriteLock ( ) ;
92
+ }
93
+ }
94
+ finally
95
+ {
96
+ @lock . EnterUpgradeableReadLock ( ) ;
88
97
}
89
98
90
99
CallContextLifetimeScope @this ;
@@ -93,18 +102,29 @@ public void Dispose()
93
102
94
103
public Burden GetCachedInstance ( ComponentModel model , ScopedInstanceActivationCallback createInstance )
95
104
{
96
- using ( var token = @lock . ForReadingUpgradeable ( ) )
105
+ @lock . EnterUpgradeableReadLock ( ) ;
106
+ try
97
107
{
98
108
var burden = cache [ model ] ;
99
109
if ( burden == null )
100
110
{
101
- token . Upgrade ( ) ;
102
-
103
- burden = createInstance ( delegate { } ) ;
104
- cache [ model ] = burden ;
111
+ @lock . EnterWriteLock ( ) ;
112
+ try
113
+ {
114
+ burden = createInstance ( delegate { } ) ;
115
+ cache [ model ] = burden ;
116
+ }
117
+ finally
118
+ {
119
+ @lock . ExitWriteLock ( ) ;
120
+ }
105
121
}
106
122
return burden ;
107
123
}
124
+ finally
125
+ {
126
+ @lock . ExitUpgradeableReadLock ( ) ;
127
+ }
108
128
}
109
129
110
130
[ SecuritySafeCritical ]
0 commit comments