From e5a32850900e95f06cc9a50877f5ed175e971af6 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 10 Nov 2015 15:05:29 +0000 Subject: [PATCH] Lazily initialize DefaultHttpContext Lazily initialize sub objects of DefaultHttpContext Resolves #470 --- .../DefaultHttpContext.cs | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNet.Http/DefaultHttpContext.cs b/src/Microsoft.AspNet.Http/DefaultHttpContext.cs index ffc6acc670..0c95f04dc4 100644 --- a/src/Microsoft.AspNet.Http/DefaultHttpContext.cs +++ b/src/Microsoft.AspNet.Http/DefaultHttpContext.cs @@ -18,8 +18,8 @@ namespace Microsoft.AspNet.Http.Internal { private readonly HttpRequest _request; private readonly HttpResponse _response; - private readonly ConnectionInfo _connection; - private readonly AuthenticationManager _authenticationManager; + private ConnectionInfo _connection; + private AuthenticationManager _authenticationManager; private IItemsFeature _items; private IServiceProvidersFeature _serviceProviders; @@ -43,8 +43,6 @@ namespace Microsoft.AspNet.Http.Internal _features = features; _request = new DefaultHttpRequest(this, features); _response = new DefaultHttpResponse(this, features); - _connection = new DefaultConnectionInfo(features); - _authenticationManager = new DefaultAuthenticationManager(features); } void IFeatureCache.CheckFeaturesRevision() @@ -133,9 +131,29 @@ namespace Microsoft.AspNet.Http.Internal public override HttpResponse Response { get { return _response; } } - public override ConnectionInfo Connection { get { return _connection; } } + public override ConnectionInfo Connection + { + get + { + if (_connection == null) + { + _connection = new DefaultConnectionInfo(_features); + } + return _connection; + } + } - public override AuthenticationManager Authentication { get { return _authenticationManager; } } + public override AuthenticationManager Authentication + { + get + { + if (_authenticationManager == null) + { + _authenticationManager = new DefaultAuthenticationManager(_features); + } + return _authenticationManager; + } + } public override ClaimsPrincipal User {