开发者

structuremap entity framework 4 connection

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.Releas开发者_运维技巧eAndDisposeAllHttpScopedObjects();
}

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜