结构图实体框架4连接

发布于 2024-09-30 05:32:39 字数 854 浏览 0 评论 0原文

我正在为我的实体框架 4 个实体使用以下 Structuremap 引导代码:

x.For<XEntities>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.PerRequest)).Use(() => new XEntities());

但是当我几乎同时执行两个请求时,我收到以下异常:

EntityException:The underlying provider failed on Open.
{"The connection was not closed. The connection's current state is connecting."}

我正在使用 ASP.NET MVC 2,在我的 Application_Start() 中有以下内容 EndRequest += new EventHandler(MvcApplication_EndRequest);

void MvcApplication_EndRequest(object sender, EventArgs e)
{
    ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}

我可以做什么来解决这个问题?

[编辑] 这种情况发生在包含多个图像的页面上。图像来自数据库,由控制器操作提供服务,控制器操作从数据库读取图像,并将其作为文件结果发送到浏览器。我认为 asp.net 正在破坏我的对象上下文,并在图像请求传入时关闭我的数据库连接,并引发异常。

我现在需要的是一种正确的方法来以良好的方式管理对象上下文的生命周期。

I am using the following Structuremap bootstrapping code for my entity framework 4 entities:

x.For<XEntities>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.PerRequest)).Use(() => new XEntities());

But when I do two nearly simultaneous requests, I get the following exception:

EntityException:The underlying provider failed on Open.
{"The connection was not closed. The connection's current state is connecting."}

I am using ASP.NET MVC 2, en have the following in my Application_Start()
EndRequest += new EventHandler(MvcApplication_EndRequest);

void MvcApplication_EndRequest(object sender, EventArgs e)
{
    ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}

What can I do to fix this?

[edit]
this happens on a page with several images on it. The images come from the database, served by an Controller Action, which reads the image from the database, and sends it as a file result to the browser. I think that asp.net is breaking down my objectcontext, and closing my db connection when the requests for the images come in, and the exception is thrown.

What I need now, is a correct way to manage the lifetime of the object context in the good way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-10-07 05:32:39

为什么要在 Application_Start() 中为 EndRequest 分配委托?

只需直接挂钩事件:

protected void Application_EndRequest()
{
    ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}

另外,我以前从未使用过该语法,这就是我的做法:

For<XEntities>().HybridHttpOrThreadLocalScoped().Use<XEntities>()

另外,您在什么时候更新您的数据上下文?你能展示一些代码吗?

Why are you assigning a delegate for EndRequest in Application_Start()?

Just hook directly into the event:

protected void Application_EndRequest()
{
    ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}

Also, i have never used that syntax before, this is how i do it:

For<XEntities>().HybridHttpOrThreadLocalScoped().Use<XEntities>()

Also, at what point do you new up your Data Context? Can you show some code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文