@@ -21,13 +21,15 @@ public class DefaultHttpContext : HttpContext
21
21
private readonly ConnectionInfo _connection ;
22
22
private readonly AuthenticationManager _authenticationManager ;
23
23
24
- private FeatureReference < IItemsFeature > _items ;
25
- private FeatureReference < IServiceProvidersFeature > _serviceProviders ;
26
- private FeatureReference < IHttpAuthenticationFeature > _authentication ;
27
- private FeatureReference < IHttpRequestLifetimeFeature > _lifetime ;
28
- private FeatureReference < ISessionFeature > _session ;
24
+ private IItemsFeature _items ;
25
+ private IServiceProvidersFeature _serviceProviders ;
26
+ private IHttpAuthenticationFeature _authentication ;
27
+ private IHttpRequestLifetimeFeature _lifetime ;
28
+ private ISessionFeature _session ;
29
29
private WebSocketManager _websockets ;
30
+
30
31
private IFeatureCollection _features ;
32
+ private int _cachedFeaturesRevision = - 1 ;
31
33
32
34
public DefaultHttpContext ( )
33
35
: this ( new FeatureCollection ( ) )
@@ -43,37 +45,122 @@ public DefaultHttpContext(IFeatureCollection features)
43
45
_response = new DefaultHttpResponse ( this , features ) ;
44
46
_connection = new DefaultConnectionInfo ( features ) ;
45
47
_authenticationManager = new DefaultAuthenticationManager ( features ) ;
48
+ }
46
49
47
- _items = FeatureReference < IItemsFeature > . Default ;
48
- _serviceProviders = FeatureReference < IServiceProvidersFeature > . Default ;
49
- _authentication = FeatureReference < IHttpAuthenticationFeature > . Default ;
50
- _lifetime = FeatureReference < IHttpRequestLifetimeFeature > . Default ;
51
- _session = FeatureReference < ISessionFeature > . Default ;
50
+ void CheckFeaturesRevision ( )
51
+ {
52
+ if ( _cachedFeaturesRevision != _features . Revision )
53
+ {
54
+ _items = null ;
55
+ _serviceProviders = null ;
56
+ _authentication = null ;
57
+ _lifetime = null ;
58
+ _session = null ;
59
+ _cachedFeaturesRevision = _features . Revision ;
60
+ }
52
61
}
53
62
54
63
IItemsFeature ItemsFeature
55
64
{
56
- get { return _items . Fetch ( _features ) ?? _items . Update ( _features , new ItemsFeature ( ) ) ; }
65
+ get
66
+ {
67
+ CheckFeaturesRevision ( ) ;
68
+
69
+ var items = _items ;
70
+ if ( items == null )
71
+ {
72
+ items = _features . Get < IItemsFeature > ( ) ;
73
+ if ( items == null )
74
+ {
75
+ items = new ItemsFeature ( ) ;
76
+ _features . Set ( items ) ;
77
+ }
78
+ _items = items ;
79
+ }
80
+ return items ;
81
+ }
57
82
}
58
83
59
84
IServiceProvidersFeature ServiceProvidersFeature
60
85
{
61
- get { return _serviceProviders . Fetch ( _features ) ?? _serviceProviders . Update ( _features , new ServiceProvidersFeature ( ) ) ; }
86
+ get
87
+ {
88
+ CheckFeaturesRevision ( ) ;
89
+
90
+ var serviceProviders = _serviceProviders ;
91
+ if ( serviceProviders == null )
92
+ {
93
+ serviceProviders = _features . Get < IServiceProvidersFeature > ( ) ;
94
+ if ( serviceProviders == null )
95
+ {
96
+ serviceProviders = new ServiceProvidersFeature ( ) ;
97
+ _features . Set ( serviceProviders ) ;
98
+ _serviceProviders = serviceProviders ;
99
+ }
100
+ }
101
+ return serviceProviders ;
102
+ }
62
103
}
63
104
64
105
private IHttpAuthenticationFeature HttpAuthenticationFeature
65
106
{
66
- get { return _authentication . Fetch ( _features ) ?? _authentication . Update ( _features , new HttpAuthenticationFeature ( ) ) ; }
107
+ get
108
+ {
109
+ CheckFeaturesRevision ( ) ;
110
+
111
+ var authentication = _authentication ;
112
+ if ( authentication == null )
113
+ {
114
+ authentication = _features . Get < IHttpAuthenticationFeature > ( ) ;
115
+ if ( authentication == null )
116
+ {
117
+ authentication = new HttpAuthenticationFeature ( ) ;
118
+ _features . Set ( authentication ) ;
119
+ }
120
+ _authentication = authentication ;
121
+ }
122
+ return authentication ;
123
+ }
67
124
}
68
125
69
126
private IHttpRequestLifetimeFeature LifetimeFeature
70
127
{
71
- get { return _lifetime . Fetch ( _features ) ?? _lifetime . Update ( _features , new HttpRequestLifetimeFeature ( ) ) ; }
128
+ get
129
+ {
130
+ CheckFeaturesRevision ( ) ;
131
+
132
+ var lifetime = _lifetime ;
133
+ if ( lifetime == null )
134
+ {
135
+ lifetime = _features . Get < IHttpRequestLifetimeFeature > ( ) ;
136
+ if ( lifetime == null )
137
+ {
138
+ lifetime = new HttpRequestLifetimeFeature ( ) ;
139
+ _features . Set ( lifetime ) ;
140
+ }
141
+ _lifetime = lifetime ;
142
+ }
143
+ return lifetime ;
144
+ }
72
145
}
73
146
74
147
private ISessionFeature SessionFeature
75
148
{
76
- get { return _session . Fetch ( _features ) ; }
149
+ get
150
+ {
151
+ CheckFeaturesRevision ( ) ;
152
+
153
+ if ( _session == null )
154
+ {
155
+ _session = _features . Get < ISessionFeature > ( ) ;
156
+ }
157
+ return _session ;
158
+ }
159
+ set
160
+ {
161
+ _features . Set ( value ) ;
162
+ _session = value ;
163
+ }
77
164
}
78
165
79
166
public override IFeatureCollection Features { get { return _features ; } }
@@ -142,8 +229,8 @@ public override ISession Session
142
229
var feature = SessionFeature ;
143
230
if ( feature == null )
144
231
{
145
- feature = new DefaultSessionFeature ( ) ;
146
- _session . Update ( _features , feature ) ;
232
+ SessionFeature = new DefaultSessionFeature ( ) ;
233
+ feature = SessionFeature ;
147
234
}
148
235
feature . Session = value ;
149
236
}
0 commit comments