Skip to content

Get the GetService into HttpContextWrapper #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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