Skip to content

Commit f5eda9b

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

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b
162162
{
163163
return Server;
164164
}
165-
165+
else if (Context.RequestServices != null)
166+
{
167+
return Context.RequestServices.GetService(service);
168+
}
166169
return null;
167170
}
168171

src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextWrapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ 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) => ((IServiceProvider)_context).GetService(serviceType);
8486
}
8587
}

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: 6 additions & 1 deletion
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,8 @@ 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-
156156
Assert.Null(provider.GetService(typeof(HttpContext)));
157+
Assert.Null(provider.GetService<Cache>());
157158
}
158159

159160
[Fact]
@@ -215,6 +216,10 @@ public void CacheFromServices()
215216

216217
// Assert
217218
Assert.Same(cache, result);
219+
220+
var provider = (IServiceProvider)context;
221+
Assert.NotNull(provider.GetService<Cache>());
222+
Assert.Same(cache, provider.GetService<Cache>());
218223
}
219224

220225
[Fact]

0 commit comments

Comments
 (0)