Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7849339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:29:13+00:00 2026-06-02T18:29:13+00:00

I have a C# .Net Web Service. I am calling a dll (C# .Net)

  • 0

I have a C# .Net Web Service. I am calling a dll (C# .Net) that uses nHibernate to connect to my database. When I call the dll, it executes a query to the db and loads the parent Object “Task”. However, when the dll tries to access the child objects “Task.SubTasks”, it throws the following error:

NHibernate.HibernateException failed to lazily initialize a collection of role:  SubTasks no session or session was closed

I’m new to nHibernate so not sure what piece of code I’m missing.

Do I need to start a Factory session in my web service before calling the dll? If so, how do I do that?

EDIT: Added the web service code and the CreateContainer() method code. This code gets called just prior to calling the dll

    [WebMethod]
    public byte[] GetTaskSubtask (string subtaskId)
    {
        var container = CreateContainer(windsorPath);

        IoC.Initialize(container);
        //DLL CALL
        byte[] theDoc = CommonExport.GetSubtaskDocument(subtaskId);

        return theDoc;

    }

/// <summary>
/// Register the IoC container.
/// </summary>
/// <param name="aWindsorConfig">The path to the windsor configuration 
/// file.</param>
/// <returns>An initialized container.</returns>
protected override IWindsorContainer CreateContainer(
   string aWindsorConfig)
{
    //This method is a workaround.  This method should not be overridden.  
    //This method is overridden because the CreateContainer(string) method 
    //in UnitOfWorkApplication instantiates a RhinoContainer instance that 
    //has a dependency on Binsor.  At the time of writing this the Mammoth 
    //application did not have the libraries needed to resolve the Binsor 
    //dependency.

    IWindsorContainer container = new RhinoContainer();

    container.Register(
       Component.For<IUnitOfWorkFactory>().ImplementedBy
          <NHibernateUnitOfWorkFactory>());
    return container;
}

EDIT: Adding DLL code and repository code…

DLL Code

public static byte[] GetSubtaskDocument(string subtaskId)
{
    BOESubtask task = taskRepo.FindBOESubtaskById(Guid.Parse(subtaskId));

    foreach(subtask st in task.Subtasks) <--this is the line that throws the error
    {
    //do some work
    }


}

Repository for task

/// <summary>
/// Queries the database for the Subtasks whose ID matches the 
/// passed in ID.
/// </summary>
/// <param name="aTaskId">The ID to find matching Subtasks 
/// for.</param>
/// <returns>The Subtasks whose ID matches the passed in 
/// ID (or null).</returns>
public Task FindTaskById(Guid aTaskId)
{ 
    var task = new Task();
    using (UnitOfWork.Start())
    {
        task =  FindOne(DetachedCriteria.For<Task>()
                    .Add(Restrictions.Eq("Id", aTaskId)));
        UnitOfWork.Current.Flush();
    }
    return task;
}

Repository for subtask

/// <summary>
/// Queries the database for the Subtasks whose ID matches the 
/// passed in ID.
/// </summary>
/// <param name="aBOESubtaskId">The ID to find matching Subtasks 
/// for.</param>
/// <returns>The Subtasks whose ID matches the passed in 
/// ID (or null).</returns>
public Subtask FindBOESubtaskById(Guid aSubtaskId)
{ 
    var subtask = new Subtask();
    using (UnitOfWork.Start())
    {
        subtask =  FindOne(DetachedCriteria.For<Subtask>()
                    .Add(Restrictions.Eq("Id", aSubtaskId)));
        UnitOfWork.Current.Flush();
    }
    return subtask;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:29:16+00:00Added an answer on June 2, 2026 at 6:29 pm

    You have apparently mapped a collection in one of your NHibernate data classes with lazy loading enabled (or better: not disabled, as it is the default behavior). NHibernate loads the entity and creates a proxy for the mapped collections. As soon as they are accessed, NHibernate attempts to load the items for that collection. But if you close your NHibernate session before that happens, the error you received will occur. You are probably exposing your data object through your web service to the web service client. During the serialization process, the XmlSerializer tries to serialize the collection which prompts NHibernate to populate it. As the session is closed, the error occurs.

    Two ways to prevent this:

    • close the session after the response has been sent

    or

    • disable lazy loading for your collections so that they are loaded instantly

    Addition after the above edits:

    in your repository, you start UnitsOfWork within a using-statement. They are being disposed as soon as the code is completed. I don’t know the implementation of UnitOfWork but i assume it controls the lifetime of the NHibernate session. By disposing the UnitOfWork, your are probalby closing the NHibernate session. As your mapping initializes collections lazy loaded, these collections are not yet populated and the error occurs. NHibernate needs the exact instance of the session that loaded an entity to populate lazily initialized collections.

    You will run into problems like this if you use lazy loading and have a repository that closes the session before the response is complete. One option would be to initialize the UnitOfWork at the start of the request and close it after the response is complete (for instance in Application_BeginRequest, Application_EndRequest in Global.asax.cs). That would of course mean a close integration of your repository into the web service.

    In any case, creating a Session for a single request in combination with lazy loading is a bad idea and is very likely to create similar problems in the future. If you can’t change the repository implementation you might probably have to disable lazy loading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .aspx page calling a .asmx page's web service. In that .net
I have a .NET Web Service running in VS2005 and a client that consumes
I have a .NET web service (using asmx...have not upgraded to WCF yet) that
i have an ASP.NET web service that returning a custom entity object (Staff): [WebMethod]
I have an ASP.net web service that I'm using for a web application which
I have an ASP.NET web service running that accepts both HTTP POST and SOAP
I have an ASP.NET web service that I can access via a windows program
I have a windows forms client that consumes an ASP.net web service. It's my
I have a .NET Windows service and a .NET Web Application that I would
I have a requirement to call a dll (unmanaged c) from a .NET web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.