cjones26
cjones26

Reputation: 3784

ServiceStack & NHibernate Integration creating two sessions?

I'm attempting to follow the blog post here: http://www.philliphaydon.com/2012/06/using-nhibernate-with-servicestack/ though I'm running into an issue with my code. Basically, I'm setting up the SessionFactory just like Phillip does and my service looks identical to Phillip's.

The issue I'm running into is when attempting to grab data from the DB without stepping through the function--it looks like the session is killed before it can be accessed, and the code then seems to simply just break down:

NHProf ServiceStack Issues https://i.sstatic.net/hw1jW.png

The odd thing is that when I attempt to debug the function by stepping through it, I am returned my result fine and NHProf agrees:

NH Prof ServiceStack Issues2 https://i.sstatic.net/DfNSi.png

Kind of odd, if anyone had any answer on why this was occurring it would be greatly appreciated!

Upvotes: 1

Views: 335

Answers (1)

Michael D. Kirkpatrick
Michael D. Kirkpatrick

Reputation: 488

What happens if you dispose the session on EndRequest? That should allow you to not have to worry about disposing your session ever time you make a database call.

I use the following in my web.config to allow me to attach to the Init event within my FluentSessionManager.cs:

<httpModules>
        <add name="MyFramework.FluentSessionManager" type="MyFramework.FluentSessionManager" />
</httpModules>

Within my FluentSessionManager.cs that lives at namespace MyFramework:

public void Init(HttpApplication context)
    {
        context.EndRequest += Application_EndRequest;
    }

public static void Application_EndRequest()
    {
        // Perform the disposing of your session here.  Commit, close, etc....
    }

This should allow you to close and dispose your session upon the EndRequest event. This also prevents you from having memory leaks and zombie connections to your SQL server.

Hopefully this helps.

Upvotes: 3

Related Questions