I'm currently trying to migrate from Castle Windsor to Simple Injector.
Within a Service am I trying to inject a HttpSessionStateBase as illustrated on below constructor for the Service:
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;
}
To do this with Windsor can I use a FactoryMethod to inject HttpSessionStateBase as a new 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;
}
This Method will work when being called in the Application_Start method of the ASP.NET MVC 4 project inside the Global.asax.cs file.
I have now tried to convert it to Simple Inject by placing the two Windsor FactoryMethods as below Simple Inject code:
container.Register<HttpRequestBase>(
() => new HttpRequestWrapper(HttpContext.Current.Request),
Lifestyle.Transient);
container.Register<HttpSessionStateBase>(
() => new HttpSessionStateWrapper(HttpContext.Current.Session),
Lifestyle.Transient);
The problem is that when I call this method in the Application_Start method then the HttpContext.Current.Session will be null when Simple Inject call the container verify method.
Any one got a solution for this?