Checking whether an object exists in StructureMap container
- by Kevin Pang
I'm using StructureMap to handle the creation of NHibernate's
ISessionFactory and ISession. I've scoped ISessionFactory as a
singleton so that it's only created once for my web app and I've
scoped ISession as a hybrid so that it will only be opened once per
web request.
I want to make sure that at the end of each web request, I properly
dispose of ISession if it was created for that web request. I figured
I could put some code in my Application_EndRequest routine to first
check if an ISession was created, and if so, call ISession.Dispose.
My current workaround is to just open up an ISession on
Application_BeginRequest then dispose of it on Application_EndRequest,
but that seems somewhat wasteful in that static file requests for
images and css files and whatnot will create an ISession without ever
using it. I know that the overall performance hit is negligable since
ISessions are very lightweight, but it's getting annoying seeing all
those ISessions being created inside NHProf.