Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");}
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public void CacheFromHttpContext()

// Assert
Assert.Same(cache, result);

//Act via GetService
var cacheFromService = context.GetService<Cache>();
Assert.Same(cache, cacheFromService);
}

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

// Assert
Assert.Same(cache, result);

//Act via GetService
var cacheFromService = contextWrapper.GetService<Cache>();
Assert.Same(cache, cacheFromService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Cache>());
}

[Fact]
Expand Down Expand Up @@ -215,6 +216,10 @@ public void CacheFromServices()

// Assert
Assert.Same(cache, result);

var provider = (IServiceProvider)context;
Assert.NotNull(provider.GetService<Cache>());
Assert.Same(cache, provider.GetService<Cache>());
}

[Fact]
Expand Down