diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs index 205d1e022f..c4e79b8abe 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs @@ -138,7 +138,7 @@ internal HttpContext() { } public System.Web.HttpApplicationState Application { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } public System.Web.HttpApplication ApplicationInstance { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } public System.Web.Caching.Cache Cache { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } - public static System.Web.HttpContext Current { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } + public static System.Web.HttpContext Current { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} set { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } public System.Web.RequestNotification CurrentNotification { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } public System.Exception Error { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } public bool IsDebuggingEnabled { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} } diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs index fd2f9e111d..1706b9add4 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs @@ -28,7 +28,11 @@ public class HttpContext : IServiceProvider private IDictionary? _items; private TraceContext? _trace; - public static HttpContext? Current => _accessor.HttpContext; + public static HttpContext? Current + { + get => _accessor.HttpContext; + set => _accessor.HttpContext = value; + } internal HttpContext(HttpContextCore context) { diff --git a/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs b/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs index e5fd5b0172..4c19b01002 100644 --- a/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs +++ b/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs @@ -471,5 +471,34 @@ public void RewritePathWithPathInfo(bool rebase) // Assert feature.Verify(f => f.Rewrite(filePath, pathInfo, query, rebase), Times.Once); } + + [Fact] + public void SetHttpContext() + { + // Arrange + var coreContext = new DefaultHttpContext(); + var context = new HttpContext(coreContext); + + // Act + HttpContext.Current = context; + + // Assert + Assert.IsType(HttpContext.Current); + } + + [Fact] + public void SetHttpContextToNull() + { + // Arrange + var coreContext = new DefaultHttpContext(); + var context = new HttpContext(coreContext); + HttpContext.Current = context; + + // Act + HttpContext.Current = null; + + // Assert + Assert.Null(HttpContext.Current); + } } }