Skip to content

Commit

Permalink
improve inject HttpContextAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
304NotModified committed May 30, 2019
1 parent 69f11a7 commit 2ebf1c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/NLog.Web/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("74d5915b-bea9-404c-b4d0-b663164def37")]
[assembly: InternalsVisibleTo("NLog.Web.Tests,PublicKey=0024000004800000940000000602000000240000525341310004000001000100772391e63c104728adcf18e2390474262559fa7f34a4215848f43288cde875dcc92a06222e9be0592b211ff74adbb5d21a7aab5522b540b1735f2f03279221056fedbe7e534073dabee9db48f8ecebcf1dc98a95576e45cbeff5fe7c4842859451ab2dae7a8370f1b2f7a529d2ca210e3e844d973523d73d193df6c17f1314a6")]
32 changes: 9 additions & 23 deletions src/Shared/LayoutRenderers/AspNetLayoutRendererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,12 @@ namespace NLog.Web.LayoutRenderers
/// </summary>
public abstract class AspNetLayoutRendererBase : LayoutRenderer
{
/// <summary>
/// Initializes the <see cref="AspNetLayoutRendererBase" />.
/// </summary>
protected AspNetLayoutRendererBase()
{
#if !ASP_NET_CORE
HttpContextAccessor = RetrieveHttpContextAccessor();
#endif
}


#if !ASP_NET_CORE
private static IHttpContextAccessor RetrieveHttpContextAccessor() => new DefaultHttpContextAccessor();
#endif

#if ASP_NET_CORE

/// <summary>
/// Context for DI
/// </summary>
private IHttpContextAccessor _httpContextAccessor;


/// <summary>
/// Provides access to the current request HttpContext.
/// </summary>
Expand All @@ -51,6 +35,13 @@ public IHttpContextAccessor HttpContextAccessor
set => _httpContextAccessor = value;
}

#if !ASP_NET_CORE

internal static IHttpContextAccessor DefaultHttpContextAccessor { get; set; } = new DefaultHttpContextAccessor();

private static IHttpContextAccessor RetrieveHttpContextAccessor() => DefaultHttpContextAccessor;
#else

private static IHttpContextAccessor RetrieveHttpContextAccessor()
{
var serviceProvider = ServiceLocator.ServiceProvider;
Expand All @@ -77,12 +68,7 @@ private static IHttpContextAccessor RetrieveHttpContextAccessor()
}
}

#else
/// <summary>
/// Provides access to the current request HttpContext.
/// </summary>
[NLog.Config.NLogConfigurationIgnorePropertyAttribute]
public IHttpContextAccessor HttpContextAccessor { get; set; }


#endif

Expand Down
15 changes: 13 additions & 2 deletions tests/Shared/RegisterCustomLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ namespace NLog.Web.AspNetCore.Tests
{
public class RegisterCustomLayoutRenderer : TestBase
{
#if !ASP_NET_CORE
~RegisterCustomLayoutRenderer()
{
AspNetLayoutRendererBase.DefaultHttpContextAccessor = new DefaultHttpContextAccessor();
}
#endif

[Fact]
public void RegisterLayoutRendererTest()
{
Expand All @@ -29,6 +36,7 @@ public void RegisterLayoutRendererTest()
httpcontext.Connection.LocalPort.Returns(123);
#else
httpcontext.Request.RawUrl.Returns("123");

#endif

// Act
Expand Down Expand Up @@ -56,14 +64,17 @@ private static
SetupHttpAccessorWithHttpContext()
{
var httpContextAccessorMock = Substitute.For<IHttpContextAccessor>();
var serviceProviderMock = Substitute.For<IServiceProvider>();
serviceProviderMock.GetService(typeof(IHttpContextAccessor)).Returns(httpContextAccessorMock);


#if ASP_NET_CORE
var serviceProviderMock = Substitute.For<IServiceProvider>();
serviceProviderMock.GetService(typeof(IHttpContextAccessor)).Returns(httpContextAccessorMock);
var httpcontext = Substitute.For<HttpContext>();
ServiceLocator.ServiceProvider = serviceProviderMock;
#else
var httpcontext = Substitute.For<HttpContextBase>();
httpContextAccessorMock.HttpContext.Returns(httpcontext);
AspNetLayoutRendererBase.DefaultHttpContextAccessor = httpContextAccessorMock;
#endif


Expand Down

0 comments on commit 2ebf1c5

Please sign in to comment.