使用 HttpContextScoped() 时 StructureMap 不会处理数据上下文
我的目标是在 ASP.NET MVC 中为每个 HTTP 请求提供一个数据上下文 (MainDbContext
),并在请求结束时处理该数据上下文。
我正在使用以下 StructureMap 配置:
public static class ContainerConfigurer
{
public static void Configure()
{
ObjectFactory.Initialize(x =>
{
x.For<MainDbContext>().HttpContextScoped();
});
}
}
每当我需要 MainDbContext
时,我都会使用以下代码:
var dbContext = ObjectFactory.GetInstance<MainDbContext>();
这按预期工作:每个 HTTP 请求仅创建一个数据上下文。问题是,MainDbContext
没有在请求结束时被释放。
如何配置 ObjectFactory 以在 HTTP 请求完成时处理数据上下文?或者这只是我需要使用 Global.asax 中的 Application_EndRequest()
手动执行的操作。
更新
我刚刚尝试将以下代码添加到 Global.asax:
protected virtual void Application_EndRequest()
{
ObjectFactory.GetInstance<MainDbContext>().Dispose();
}
正如预期的那样,这解决了问题。然而,我仍然想知道是否有任何方法可以使用 StructureMap 自动执行此操作。
My goal is to have one data context (MainDbContext
) per HTTP request in ASP.NET MVC and dispose the data context when the request ends.
I'm using the following StructureMap configuration:
public static class ContainerConfigurer
{
public static void Configure()
{
ObjectFactory.Initialize(x =>
{
x.For<MainDbContext>().HttpContextScoped();
});
}
}
Whenever I need a MainDbContext
, I'm using this code:
var dbContext = ObjectFactory.GetInstance<MainDbContext>();
This is working as expected: only one data context is being created per HTTP request. The problem is, MainDbContext
is not being disposed at the end of the request.
How can I configure my ObjectFactory to dispose the data context when the HTTP request finishes? Or is this just something I need to do manually using Application_EndRequest()
in Global.asax.
Update
I just tried adding the following code to Global.asax:
protected virtual void Application_EndRequest()
{
ObjectFactory.GetInstance<MainDbContext>().Dispose();
}
As expected, this solves the problem. I'm still wondering if there's any way to do this automatically with StructureMap, however.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
而不是:尝试:
通常情况下,存储库类需要数据库上下文。因此,您的存储库不需要使用
ObjectFactory.GetInstance();
来获取一些接口数据库上下文,并配置 StructureMap 将MainDbContext
注入其中。然后使 StructureMap 将存储库注入控制器,...在
Application_EndRequest
中:Instead of:Try:
Also normally it's repository classes that need a db context. So instead of
ObjectFactory.GetInstance<MainDbContext>();
have your repositories take some interface db context and configure StructureMap to inject theMainDbContext
into them. Then make StructureMap inject repositories into controllers, ...In
Application_EndRequest
:使用嵌套容器是唯一的方法获取结构图来自动处理对象。如果您不使用该技术,唯一的方法是使用OP描述的方式自行处置对象(从容器中拉出对象并处置它;请参阅此NHibernate 示例 了解一种方法)或将对象范围限定为 HttpRequest 并调用正如 Darin 所描述的那样,ReleaseAndDisposeAllHttpScopedObjects。
Using a nested container is the only way to get Structure Map to automatically dispose objects. If you're not using that technique the only way is to dispose the objects yourself using either the way the OP described (pulling the object from the container and disposing it; see this NHibernate example for one way to do it) or to scope the object to HttpRequest and call ReleaseAndDisposeAllHttpScopedObjects as Darin described.