【发布时间】:2013-01-28 11:24:41
【问题描述】:
我正在开发一个繁重的 MVC 例程,它会延迟几分钟的处理时间。然后我调用一个 Ajax 请求,如果进程已正确启动,我想从控制器向接口发送一个答案,并继续使用线程执行它。但是,发送返回时,需要访问数据库,并且出现以下错误:对象已释放。
我的代码:
var entidade = this._repositorioDeTabelaDePremiacaoUPL.ObterPorID(dto.ID);
if(entidade.StatusDoServico == ListaDeStatusDoServico.tcProcessando.Id)
return Content("{success:false}");
Thread thread = new Thread(() => this._servicoDeTabelaDePremiacaoUPL.GerarTabela(dto));
thread.Start();
GerenciadorDeUnidadeDeTrabalho.Corrente.Commit();
return Content("{success:true}");
【问题讨论】:
-
如何管理数据库上下文的生命周期?您是自己创建新实例,还是使用一些 DI 容器?
-
@mipe34,我的数据库上下文已在 global.asax 中完全加载,所有实例都已加载。
-
在 global.asax 中完全加载 - 您在某个静态属性中存储每个应用程序生命周期的一个 dbContext?请告诉我设置
this._repositorioDeTabelaDePremiacaoUPL的代码?可以把db上下文初始化的代码贴在global.asax吗? -
@mipe34,我在 global.axax 中的数据库上下文初始化是通过这样的结构映射完成的:
ObjectFactory.Configure(i => { i.For<ContextoBase>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.PerRequest)).Use<ContextoDaAplicacao>(); }我的方法 this._repositorioDeTabelaDePremiacaoUPL 放在这里非常大。 -
请在您的问题中发布更新。
标签: c# asp.net-mvc entity-framework dependency-injection structuremap