我目前正在尝试从温莎城堡迁移到简单喷油器。从温莎迁移到简单的喷油器,HttpContext.Current.Session为空
内的服务我在试图注入HttpSessionStateBase所示下面的构造函数的服务:
public UserSession(IResourceRepository resourceRepository, IUser user,
IRoleResolver roleResolver, IApproverRepository approverRepository,
IActiveDirectory activeDirectory, HttpSessionStateBase httpSession,
Settings appSettings)
{
_resourceRepository = resourceRepository;
_user = user;
_roleResolver = roleResolver;
_approverRepository = approverRepository;
_activeDirectory = activeDirectory;
_httpSession = httpSession;
_allowedUsernamePostfixes = appSettings.AuthenticationAllowedUsernamePostfixes;
}
要与温莎做到这一点我可以使用一个FactoryMethod注入HttpSessionStateBase作为新HttpSessionStateWrapper。
private static IWindsorContainer BuildWindsorContainer()
{
var container = new WindsorContainer();
container.Kernel.Resolver.AddSubResolver(
new CollectionResolver(container.Kernel, true));
container.Register(
Component.For<IWindsorContainer>().Instance(container)
);
container.Install(FromAssembly.This());
container.Register(
Component.For<HttpRequestBase>()
.UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request))
.LifestyleTransient(),
Component.For<HttpSessionStateBase>()
.UsingFactoryMethod(() => new HttpSessionStateWrapper(HttpContext.Current.Session))
.LifestyleTransient(),
return container;
}
该方法在Global.asax.cs文件中的ASP.NET MVC 4项目的Application_Start方法中被调用时将工作。
我现在已经尝试通过将两个温莎FactoryMethods如下简单的代码注入到其转换为简单的进样:
container.Register<HttpRequestBase>(
() => new HttpRequestWrapper(HttpContext.Current.Request),
Lifestyle.Transient);
container.Register<HttpSessionStateBase>(
() => new HttpSessionStateWrapper(HttpContext.Current.Session),
Lifestyle.Transient);
的问题是,当我把在Application_Start方法这种方法则的HttpContext。 Simple Inject调用容器验证方法时,Current.Session将为null。
任何人都有这个解决方案吗?
嗨,你是正确的代码与使用运行时数据的气味。令人遗憾的是,应用程序的创始人基于使用运行时数据的服务进行身份验证...... 我已经尝试了一些代码,但无法使HttpSessionStateBase工作,因为HttpSessionStateWrapper和HttpSessionState之间没有隐式类型。 – McBoman
对不起,我没有时间测试代码,希望这会给你一些关于如何解决这个问题的指导。 – Steven
当然,感谢您的帮助。 – McBoman