By default, when you want access to Session object from http handler you have beautiful exceptions thrown.
This is probably from performance reason. But there is very simple solution to fix it.
Everything to do is to implement a IRequiresSessionState marker interface.
I wrote in current project typical funcion to count the number of elements in db for specific criteria using NHibernate HQL: IQuery query = Session.CreateQuery(@" select count(t.ID) from Post p join p.Tags t where t.Name=:tagName and p.Blog.Name=:blogName ") .SetParameter("tagName", tagName) .SetParameter("blogName", blogName); int result=query.UniqueResult&l... I was very surpriced to see that I stuck with System.InvalidCastException: Specified cast is not valid. Solution for this was to ......