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;
|
private IHttpContextAccessor _httpContextAccessor;
|
||||||
|
|
||||||
|
public HttpContextFactory() : this(httpContextAccessor: null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public HttpContextFactory(IHttpContextAccessor httpContextAccessor)
|
public HttpContextFactory(IHttpContextAccessor httpContextAccessor)
|
||||||
{
|
{
|
||||||
_httpContextAccessor = httpContextAccessor;
|
_httpContextAccessor = httpContextAccessor;
|
||||||
|
|
@ -17,13 +21,19 @@ namespace Microsoft.AspNet.Http.Internal
|
||||||
public HttpContext Create(IFeatureCollection featureCollection)
|
public HttpContext Create(IFeatureCollection featureCollection)
|
||||||
{
|
{
|
||||||
var httpContext = new DefaultHttpContext(featureCollection);
|
var httpContext = new DefaultHttpContext(featureCollection);
|
||||||
_httpContextAccessor.HttpContext = httpContext;
|
if (_httpContextAccessor != null)
|
||||||
|
{
|
||||||
|
_httpContextAccessor.HttpContext = httpContext;
|
||||||
|
}
|
||||||
return httpContext;
|
return httpContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose(HttpContext 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
|
||||||
Assert.True(ReferenceEquals(context, accessor.HttpContext));
|
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