From f5eda9b4606f2999ce1d7aba36e178319c5b0efb Mon Sep 17 00:00:00 2001 From: Biroj Nayak Date: Fri, 8 Dec 2023 19:33:34 +0000 Subject: [PATCH 1/3] Get the GetService into HttpContextWrapper --- .../Generated/Ref.Standard.cs | 1 + src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs | 5 ++++- .../HttpContextWrapper.cs | 2 ++ .../CacheTests.cs | 8 ++++++++ .../HttpContextTests.cs | 7 ++++++- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs index f16d75461..84ec1ff5a 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs @@ -208,6 +208,7 @@ public partial class HttpContextWrapper : System.Web.HttpContextBase public override System.Security.Principal.IPrincipal User { 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 override void AddError(System.Exception ex) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} public override void ClearError() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} + public override object GetService(System.Type serviceType) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} public override void RewritePath(string path) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} public override void RewritePath(string path, bool rebaseClientPath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} public override void RewritePath(string filePath, string pathInfo, string queryString) { 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 b6e29a072..2edbbdd0a 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs @@ -162,7 +162,10 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b { return Server; } - + else if (Context.RequestServices != null) + { + return Context.RequestServices.GetService(service); + } return null; } diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextWrapper.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextWrapper.cs index 5a7388547..99e0d89c5 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextWrapper.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextWrapper.cs @@ -81,5 +81,7 @@ public override IPrincipal User public override void RewritePath(string filePath, string pathInfo, string? queryString, bool setClientFilePath) => _context.RewritePath(filePath, pathInfo, queryString, setClientFilePath); public override void SetSessionStateBehavior(SessionStateBehavior sessionStateBehavior) => _context.SetSessionStateBehavior(sessionStateBehavior); + + public override object? GetService(Type serviceType) => ((IServiceProvider)_context).GetService(serviceType); } } diff --git a/test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/CacheTests.cs b/test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/CacheTests.cs index bc93811a0..f5dc410d4 100644 --- a/test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/CacheTests.cs +++ b/test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/CacheTests.cs @@ -32,6 +32,10 @@ public void CacheFromHttpContext() // Assert Assert.Same(cache, result); + + //Act via GetService + var cacheFromService = context.GetService(); + Assert.Same(cache, cacheFromService); } [Fact] @@ -54,6 +58,10 @@ public void CacheFromHttpContextWrapper() // Assert Assert.Same(cache, result); + + //Act via GetService + var cacheFromService = contextWrapper.GetService(); + Assert.Same(cache, cacheFromService); } } } diff --git a/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs b/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs index 74170b177..6dbe10ae2 100644 --- a/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs +++ b/test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.SystemWebAdapters.Features; using Microsoft.AspNetCore.SystemWebAdapters.SessionState; +using Microsoft.Extensions.DependencyInjection; using Moq; using Xunit; @@ -152,8 +153,8 @@ public void GetServiceReturnsExpected() Assert.Same(context.Response, provider.GetService(typeof(HttpResponse))); Assert.Same(context.Server, provider.GetService(typeof(HttpServerUtility))); Assert.Same(context.Session, provider.GetService(typeof(HttpSessionState))); - Assert.Null(provider.GetService(typeof(HttpContext))); + Assert.Null(provider.GetService()); } [Fact] @@ -215,6 +216,10 @@ public void CacheFromServices() // Assert Assert.Same(cache, result); + + var provider = (IServiceProvider)context; + Assert.NotNull(provider.GetService()); + Assert.Same(cache, provider.GetService()); } [Fact] From 749fcb0ca9da2ebd2d8ab0d9c9fe6137e5106d8f Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Thu, 14 Dec 2023 14:21:23 -0800 Subject: [PATCH 2/3] Update src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs --- src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs index 2edbbdd0a..90c11fee2 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs @@ -162,11 +162,7 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b { return Server; } - else if (Context.RequestServices != null) - { - return Context.RequestServices.GetService(service); - } - return null; + return Context.RequestServices.GetService(service); } public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target) From 70dbc1214f00169254c00ff28796024bb5242079 Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Thu, 14 Dec 2023 15:10:18 -0800 Subject: [PATCH 3/3] add null check --- src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs index 90c11fee2..81fa01dad 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs @@ -162,7 +162,8 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b { return Server; } - return Context.RequestServices.GetService(service); + + return Context.RequestServices?.GetService(service); } public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target)