I'm using the Unity.MVC3.dll from codeplex and am attempting to register an instance of a NHibernate ISession per request. As per the Unity MVC3 docs I should just have to use the HierarchicalLifetimeManager to accomplish this:
var container = new UnityContainer();
container.RegisterInstance<ISession>(SessionFactory.OpenSession(), new HierarchicalLifetimeManager());
container.RegisterControllers();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
SessionFactory is a static object in my Global.asax. When I add ISession
to one of my controllers I receive the following error message:
The current type, NHibernate.ISession, is an interface and cannot be constructed. Are you missing a type mapping?
I thought I didn't need to RegisterType
when just registering an instance?
In my controller I have:
[Dependency]
public ISession session { get; set; }
I also tried adding ISession as an argument to one of my ActionResult functions
I'll add that the issue is not on NHibernate's end. The configuration is successful and I can use it to query info from my database.
UPDATE
When I do not specify the lifetime manager it works, however, this isn't going to get me my "one instance per request" that I need.
I might have to explore Ninject :p