Skip to content

Commit 6f5b309

Browse files
author
Biroj Nayak
committed
Get the GetService into HttpContextWrapper
1 parent 1a0c991 commit 6f5b309

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public partial class HttpContextWrapper : System.Web.HttpContextBase
208208
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");} }
209209
public override void AddError(System.Exception ex) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
210210
public override void ClearError() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
211+
public override object GetService(System.Type serviceType) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
211212
public override void RewritePath(string path) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
212213
public override void RewritePath(string path, bool rebaseClientPath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
213214
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");}

src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b
161161
else if (service == typeof(HttpServerUtility))
162162
{
163163
return Server;
164+
}else
165+
{
166+
return Context.RequestServices.GetService(service);
164167
}
165-
166-
return null;
167168
}
168169

169170
public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target)

src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextWrapper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,10 @@ public override IPrincipal User
8181
public override void RewritePath(string filePath, string pathInfo, string? queryString, bool setClientFilePath) => _context.RewritePath(filePath, pathInfo, queryString, setClientFilePath);
8282

8383
public override void SetSessionStateBehavior(SessionStateBehavior sessionStateBehavior) => _context.SetSessionStateBehavior(sessionStateBehavior);
84+
85+
public override object? GetService(Type serviceType)
86+
{
87+
return ((IServiceProvider)_context).GetService(serviceType);
88+
}
8489
}
8590
}

test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/CacheTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public void CacheFromHttpContext()
3232

3333
// Assert
3434
Assert.Same(cache, result);
35+
36+
//Act via GetService
37+
var cacheFromService = context.GetService<Cache>();
38+
Assert.Same(cache, cacheFromService);
3539
}
3640

3741
[Fact]
@@ -54,6 +58,10 @@ public void CacheFromHttpContextWrapper()
5458

5559
// Assert
5660
Assert.Same(cache, result);
61+
62+
//Act via GetService
63+
var cacheFromService = contextWrapper.GetService<Cache>();
64+
Assert.Same(cache, cacheFromService);
5765
}
5866
}
5967
}

test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpContextTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.Http;
1313
using Microsoft.AspNetCore.SystemWebAdapters.Features;
1414
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;
15+
using Microsoft.Extensions.DependencyInjection;
1516
using Moq;
1617
using Xunit;
1718

@@ -152,8 +153,6 @@ public void GetServiceReturnsExpected()
152153
Assert.Same(context.Response, provider.GetService(typeof(HttpResponse)));
153154
Assert.Same(context.Server, provider.GetService(typeof(HttpServerUtility)));
154155
Assert.Same(context.Session, provider.GetService(typeof(HttpSessionState)));
155-
156-
Assert.Null(provider.GetService(typeof(HttpContext)));
157156
}
158157

159158
[Fact]

0 commit comments

Comments
 (0)