我有一个C#.Net Web服务。我正在调用一个使用nHibernate连接到我的数据库的dll(C#.Net)。当我调用dll时,它会执行对数据库的查询并加载父对象“任务”。然而,当DLL试图访问子对象“Task.SubTasks”,它引发以下错误:Web服务nHibernate SessionFactory问题
NHibernate.HibernateException failed to lazily initialize a collection of role: SubTasks no session or session was closed
我是新来NHibernate的所以不知道什么我失踪一段代码。
在调用dll之前,我需要在Web服务中启动Factory会话吗?如果是这样,我该怎么做?
编辑:添加了Web服务代码和CreateContainer()方法代码。此代码被调用的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;
}
编辑:添加DLL的代码和库代码...
DLL的代码
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
}
}
库任务
/// <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;
}
子任务知识库
/// <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;
}
感谢您的意见。但是,我认为我的问题处于更高层次。我没有任何nHibernate会话代码。我使用温莎打开一个容器,但不是一个会话。引发错误的代码是在dll中。 Web服务初始化容器,然后调用dll。 dll从db获取对象并将它们作为byte []返回给Web服务,但在它到达之前它正在进行barfing。 – MikeTWebb 2012-04-27 17:05:17
但在某个地方有人正在打开和关闭NHibernate会话。由于错误指出NHibernate抱怨一个封闭的会话,它一定是由某人打开和关闭的。温莎的哪些部分在使用? ActiveRecord,WCF工具,NHibernate集成? – 2012-04-27 17:11:40
表示同意......它在某处“自动”发生。这是nHibernate集成。 hibernate.cfg有一个部分。我已经在上面添加了CreateContainer代码 –
MikeTWebb
2012-04-27 17:27:37