From ffcd176dc0bcb24d0f333b2c86f889f1af60ac46 Mon Sep 17 00:00:00 2001 From: Afshin Mehrabani Date: Mon, 9 Oct 2023 20:15:11 +0100 Subject: [PATCH 1/3] Add a setter for HttpContext.Current --- .../HttpContext.cs | 6 +++- .../HttpContextTests.cs | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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..14e802fae4 100644 --- a/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs +++ b/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs @@ -471,5 +471,35 @@ 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); + } } } From 187c24edb6c59e460aa1b700d8de0de5ac929111 Mon Sep 17 00:00:00 2001 From: Afshin Mehrabani Date: Mon, 9 Oct 2023 20:27:42 +0100 Subject: [PATCH 2/3] Update types for HttpContext.Current --- .../Generated/Ref.Standard.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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");} } From 98026b11fbc0b9d81b7a8d8207fd9f577dc9bd3c Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Mon, 9 Oct 2023 12:39:26 -0700 Subject: [PATCH 3/3] Update test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs --- .../HttpContextTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs b/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs index 14e802fae4..4c19b01002 100644 --- a/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs +++ b/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs @@ -486,7 +486,6 @@ public void SetHttpContext() Assert.IsType(HttpContext.Current); } - [Fact] public void SetHttpContextToNull() {