I'm using the Nhibernate persistence facility from Windor's tutorial:
Kernel.Register(
Component.For<ISessionFactory>()
.UsingFactoryMethod(config.BuildSessionFactory)
.LifeStyle.Singleton,
Component.For<ISession>()
.UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
.LifeStyle.PerWebRequest);
Sometimes my call to config.BuildSessionFactory will fail (maybe my mappings are wrong, or my connection string is invalid or whatever). In the debugger, I can see the Nhibernate exception being thrown. Now Windsor can no longer resolve my ISession
either because the factory itself couldn't be instantiated.
The problem is that it doesn't seem to complain about it. Without the debugger, the exception is silently thrown away and the only symptom I have in my app is that all my ISession
dependencies are suddenly null
. What's the right way to deal with exceptions in UsingFactoryMethod
? Is there some way I can tell Windsor to bubble up this exception to my app?
The only why I can see Castle eating the exception is if the session is being injected as a property, which makes Castle consider it optional.
Here's how I fixed it... I created an activator that throws an exception when it fails to set a property's value:
And then I configured most of my components with
.Activator<StrictComponentActivator>()