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..81fa01dad 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs @@ -163,7 +163,7 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b return Server; } - return null; + return Context.RequestServices?.GetService(service); } public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target) 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]