Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 2753a75

Browse files
committed
Flow through FeatureReference removal
1 parent 50ad2b2 commit 2753a75

9 files changed

+440
-52
lines changed

src/Microsoft.AspNet.Http/Authentication/DefaultAuthenticationManager.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,55 @@ namespace Microsoft.AspNet.Http.Authentication.Internal
1515
public class DefaultAuthenticationManager : AuthenticationManager
1616
{
1717
private readonly IFeatureCollection _features;
18-
private FeatureReference<IHttpAuthenticationFeature> _authentication = FeatureReference<IHttpAuthenticationFeature>.Default;
19-
private FeatureReference<IHttpResponseFeature> _response = FeatureReference<IHttpResponseFeature>.Default;
18+
private int _cachedFeaturesRevision = -1;
19+
20+
private IHttpAuthenticationFeature _authentication;
21+
private IHttpResponseFeature _response;
2022

2123
public DefaultAuthenticationManager(IFeatureCollection features)
2224
{
2325
_features = features;
2426
}
2527

28+
void CheckFeaturesRevision()
29+
{
30+
if (_cachedFeaturesRevision != _features.Revision)
31+
{
32+
_authentication = null;
33+
_response = null;
34+
_cachedFeaturesRevision = _features.Revision;
35+
}
36+
}
37+
2638
private IHttpAuthenticationFeature HttpAuthenticationFeature
2739
{
28-
get { return _authentication.Fetch(_features) ?? _authentication.Update(_features, new HttpAuthenticationFeature()); }
40+
get
41+
{
42+
CheckFeaturesRevision();
43+
if (_authentication == null)
44+
{
45+
_authentication = _features.Get<IHttpAuthenticationFeature>();
46+
if (_authentication == null)
47+
{
48+
_authentication = new HttpAuthenticationFeature();
49+
_features.Set(_authentication);
50+
}
51+
}
52+
return _authentication;
53+
}
2954
}
3055

3156
private IHttpResponseFeature HttpResponseFeature
3257
{
33-
get { return _response.Fetch(_features); }
58+
get
59+
{
60+
CheckFeaturesRevision();
61+
if (_response == null)
62+
{
63+
_response = _features.Get<IHttpResponseFeature>();
64+
}
65+
return _response;
66+
}
3467
}
3568

3669
public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes()

src/Microsoft.AspNet.Http/DefaultConnectionInfo.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,66 @@ namespace Microsoft.AspNet.Http.Internal
1313
public class DefaultConnectionInfo : ConnectionInfo
1414
{
1515
private readonly IFeatureCollection _features;
16+
private int _cachedFeaturesRevision = -1;
1617

17-
private FeatureReference<IHttpConnectionFeature> _connection = FeatureReference<IHttpConnectionFeature>.Default;
18-
private FeatureReference<ITlsConnectionFeature> _tlsConnection = FeatureReference<ITlsConnectionFeature>.Default;
18+
private IHttpConnectionFeature _connection;
19+
private ITlsConnectionFeature _tlsConnection;
1920

2021
public DefaultConnectionInfo(IFeatureCollection features)
2122
{
2223
_features = features;
2324
}
2425

26+
void CheckFeaturesRevision()
27+
{
28+
if (_cachedFeaturesRevision != _features.Revision)
29+
{
30+
_connection = null;
31+
_tlsConnection = null;
32+
_cachedFeaturesRevision = _features.Revision;
33+
}
34+
}
35+
2536
private IHttpConnectionFeature HttpConnectionFeature
2637
{
27-
get { return _connection.Fetch(_features) ?? _connection.Update(_features, new HttpConnectionFeature()); }
38+
get
39+
{
40+
CheckFeaturesRevision();
41+
42+
var connection = _connection;
43+
if (connection == null)
44+
{
45+
connection = _features.Get<IHttpConnectionFeature>();
46+
if (connection == null)
47+
{
48+
connection = new HttpConnectionFeature();
49+
_features.Set(connection);
50+
}
51+
_connection = connection;
52+
}
53+
return connection;
54+
}
2855
}
2956

3057
private ITlsConnectionFeature TlsConnectionFeature
3158
{
32-
get { return _tlsConnection.Fetch(_features) ?? _tlsConnection.Update(_features, new TlsConnectionFeature()); }
59+
get
60+
{
61+
CheckFeaturesRevision();
62+
63+
var tlsConnection = _tlsConnection;
64+
if (tlsConnection == null)
65+
{
66+
tlsConnection = _features.Get<ITlsConnectionFeature>();
67+
if (tlsConnection == null)
68+
{
69+
tlsConnection = new TlsConnectionFeature();
70+
_features.Set(tlsConnection);
71+
}
72+
_tlsConnection = tlsConnection;
73+
}
74+
return tlsConnection;
75+
}
3376
}
3477

3578
public override IPAddress RemoteIpAddress

src/Microsoft.AspNet.Http/DefaultHttpContext.cs

Lines changed: 104 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public class DefaultHttpContext : HttpContext
2121
private readonly ConnectionInfo _connection;
2222
private readonly AuthenticationManager _authenticationManager;
2323

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;
2929
private WebSocketManager _websockets;
30+
3031
private IFeatureCollection _features;
32+
private int _cachedFeaturesRevision = -1;
3133

3234
public DefaultHttpContext()
3335
: this(new FeatureCollection())
@@ -43,37 +45,122 @@ public DefaultHttpContext(IFeatureCollection features)
4345
_response = new DefaultHttpResponse(this, features);
4446
_connection = new DefaultConnectionInfo(features);
4547
_authenticationManager = new DefaultAuthenticationManager(features);
48+
}
4649

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+
}
5261
}
5362

5463
IItemsFeature ItemsFeature
5564
{
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+
}
5782
}
5883

5984
IServiceProvidersFeature ServiceProvidersFeature
6085
{
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+
}
62103
}
63104

64105
private IHttpAuthenticationFeature HttpAuthenticationFeature
65106
{
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+
}
67124
}
68125

69126
private IHttpRequestLifetimeFeature LifetimeFeature
70127
{
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+
}
72145
}
73146

74147
private ISessionFeature SessionFeature
75148
{
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+
}
77164
}
78165

79166
public override IFeatureCollection Features { get { return _features; } }
@@ -142,8 +229,8 @@ public override ISession Session
142229
var feature = SessionFeature;
143230
if (feature == null)
144231
{
145-
feature = new DefaultSessionFeature();
146-
_session.Update(_features, feature);
232+
SessionFeature = new DefaultSessionFeature();
233+
feature = SessionFeature;
147234
}
148235
feature.Session = value;
149236
}

0 commit comments

Comments
 (0)