Set IHttpContextAccessor only if DI provides it
This commit is contained in:
parent
4efc40d8b1
commit
681533e06c
|
|
@ -9,6 +9,10 @@ namespace Microsoft.AspNet.Http.Internal
|
|||
{
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public HttpContextFactory() : this(httpContextAccessor: null)
|
||||
{
|
||||
}
|
||||
|
||||
public HttpContextFactory(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
|
|
@ -17,13 +21,19 @@ namespace Microsoft.AspNet.Http.Internal
|
|||
public HttpContext Create(IFeatureCollection featureCollection)
|
||||
{
|
||||
var httpContext = new DefaultHttpContext(featureCollection);
|
||||
_httpContextAccessor.HttpContext = httpContext;
|
||||
if (_httpContextAccessor != null)
|
||||
{
|
||||
_httpContextAccessor.HttpContext = httpContext;
|
||||
}
|
||||
return httpContext;
|
||||
}
|
||||
|
||||
public void Dispose(HttpContext httpContext)
|
||||
{
|
||||
_httpContextAccessor.HttpContext = null;
|
||||
if (_httpContextAccessor != null)
|
||||
{
|
||||
_httpContextAccessor.HttpContext = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,5 +21,16 @@ namespace Microsoft.AspNet.Http.Internal
|
|||
// Assert
|
||||
Assert.True(ReferenceEquals(context, accessor.HttpContext));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllowsCreatingContextWithoutSettingAccessor()
|
||||
{
|
||||
// Arrange
|
||||
var contextFactory = new HttpContextFactory();
|
||||
|
||||
// Act & Assert
|
||||
var context = contextFactory.Create(new FeatureCollection());
|
||||
contextFactory.Dispose(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue