【发布时间】:2015-11-27 13:45:40
【问题描述】:
我正在尝试在 vNext 中重新创建 HttpContext.Current,但我遇到了问题。
到目前为止的代码:
public class CurrentContext
{
public IHttpContextAccessor ctx { get; private set; }
public CurrentContext(IHttpContextAccessor accessor)
{
ctx = accessor;
}
}
services.AddTransient<CurrentContext>();
我想使用:
public static class UserFactory
{
public static void SetCurrentUser(User u)
{
//tryed to get the context but GetService returns null
IHttpContextAccessor _context = ((CurrentContext)CallContextServiceLocator.Locator.ServiceProvider.GetService(serviceType: typeof(CurrentContext))).ctx;
//add the user to the Session
}
}
我现在如何在项目中的任何位置获取CurrentContext 的实例?
【问题讨论】:
标签: c# dependency-injection asp.net-core asp.net-core-mvc