Wiki Link: [discussion:81873]
Issue with Find.Items and NHibernate Sessions.  

Tags: Developer Forum, User Forum

Jan 24 at 10:31 PM

I'll start off by admitting my codebase is a little out of date (the latest Get was probably 12th May 2009), so the answer to this might be "Get the latest, it all works" in which case I'll be busy...

However, I've got a version of N2 up and running on ASP.NET MVC using the /Examples/MVC project as my starting point: http://www.holytrinitybeckenham.org.uk/

I'd like to automate a few more areas of this for the people that administer it, so wanted to make the link to the letter automatically pick up the latest letter.

I added the following code to my HomePage model:

        private readonly NewsPage m_LatestNews = null;

        public HomePage()
        {
            var news = N2.Find.Items.Where.Type.Eq(typeof(NewsPage))
                .OrderBy.Published.Desc.MaxResults(1).Select<NewsPage>();

            if (null != news)
            {
                m_LatestNews = news[0];
            }
        }

And had the following in my (strongly typed) view:

    <% if (null != Model.LatestNews) {%>
    <p><a href="<%=Model.LatestNews.Url %>">Letter from our vicar Nick Reed and his wife Cathryn</a></p>
    <% } %>

This all appeared to work fine, until I clicked on the link in the browser, and received the following exception:

failed to lazily initialize a collection, no session or session was closed

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: NHibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed

Source Error:

Line 30:             where T : ContentItem
Line 31: {
Line 32: foreach (T item in items)
Line 33: if (Match(item))
Line 34: yield return item;


Source File: c:\Users\Ben\Documents\Visual Studio 2008\Projects\n2_Source\src\N2\Collections\ItemFilter.cs    Line: 32

Any recommendations as to a suitable workaround would be gratefully received.

Thanks


Jan 26 at 12:18 PM

Can't test with Visual Studio as I'm just answering on my iPod right now but maybe something like:

var news = N2.Find.Items.Where.Type.Eq(typeof(NewsPage))
                .OrderBy.Published.Desc.MaxResults(1).Select<NewsPage>().ToList();

will solve this.


Jan 26 at 8:42 PM

Hi jronaldi,

Thanks for the suggestion, it certainly has merit - thinking about it after you'd suggested it I've used a similar trick in the past with linq to force the collection to instantiate - but in this case it didn't help I still get the error when I click through to the link.

I don't know if it helps, but it only seems to affect the NewsContainer or NewsPages (i.e. those that interact with the postings whose type I was searching on). I have another listing page that works in the same way as the news pages, but uses a different type (EventContainer and EventPage) and this still works fine so it's clearly something to do with the way the Finder is working?


Coordinator
Jan 26 at 10:18 PM

N2 opens a nh session on first usage and closes it on end request. Maybe this session is disposed somehow. Hard to tell exactly where without debugging the code.


Jan 26 at 11:10 PM

Hi Libardo,

You got me thinking, and whilst I've not had a chance to see exactly what's causing it, I have worked around the issue - so thanks.

I've moved the Find out of the models constructor, and into a new controller that I've created for the homepage (rather than relying on the default ContentController, which populates an auto property on the model:

[Controls(typeof(HomePage))]
public class HomePageController : ContentController<HomePage>
{
    //
    // GET: /HomePage/
    public override ActionResult Index()
    {
        var vd = CurrentItem;

        var news =
              N2.Find.Items.Where.Type.Eq(typeof(NewsPage))
              .OrderBy.Published.Desc.MaxResults(1)
              .Select<NewsPage>().ToList();

        if (news.Any())
        {
            vd.LatestNews = news[0];
        }

        return View("Index", vd);
    }
}
Which seems to work a treat.

Thanks once again for the pointers :)


Updating...
© 2006-2010 Microsoft | Get Help | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2010.7.20.17027