[英]UnitOfWork struggling with multiple simultaneous connections
Apparently (and quite possibly) there's a flaw in my current UnitOfWork implementation, because I have connection errors when doing many calls at once. 显然(很可能)我当前的UnitOfWork实现存在缺陷,因为我在一次执行多次调用时遇到连接错误。
Exception: 例外:
The underlying provider failed on Open.
底层提供程序在Open上失败。
Inner Exception: 内在例外:
The connection was not closed.
连接没有关闭。 The connection's current state is connecting.
连接的当前状态是连接。
This results in a HTTP 500 response on the client side.
这导致客户端的HTTP 500响应。
UnitOfWork implementation UnitOfWork实现
public class ScopedUnitOfWork : IUnitOfWork
{
public Entities Context { get; set; }
public UnitOfWorkState State { get; set; }
public ScopedUnitOfWork(IEnvironmentInformationProvider environmentInformationProvider)
{
this.Context = new Entities(environmentInformationProvider.ConnectionString);
this.State = UnitOfWorkState.Initialized;
}
public UowScope GetScope()
{
this.State = UnitOfWorkState.Working;
return new UowScope(this);
}
public SaveResult Save()
{
if (this.State != UnitOfWorkState.Working)
throw new InvalidOperationException("Not allowed to save out of Scope. Request an UowScope instance by calling method GetScope().");
this.Context.SaveChanges();
this.State = UnitOfWorkState.Finished;
return new SaveResult(ResultCodes.Ok);
}
}
Working on a single UowScope
would solve the issue but that's not possible given the current circumstance, because each request is completely separate. 使用单个
UowScope
可以解决问题,但鉴于目前的情况,这是不可能的,因为每个请求都是完全独立的。 De facto each request IS using an UoWScope
, but apparently it goes wrong when the UoW receives many calls at once. 事实上,每个请求都使用
UoWScope
,但显然当UoW一次接收到多个呼叫时出错。
The UoW is injected through Unity IoC, so I suppose it's a singleton in effect. UoW是通过Unity IoC注入的,所以我认为它是一个有效的单例。
The question 这个问题
Is there a way to adapt the UoW so that separate high-frequency requests are not an issue? 有没有办法调整UoW,以便单独的高频请求不成问题?
Preferably I'd solve this server side, not client side, any tips? 我最好解决这个服务器端,而不是客户端,任何提示? Thanks!
谢谢!
Disclaimer 放弃
I don't claim I fully understand UoW, so my implementation may need improvement, be gentle :). 我没有声称我完全理解UoW,所以我的实现可能需要改进,温柔:)。 Any improvements on that are certainly welcome!
当然,任何改进都是受欢迎的!
UPDATE UPDATE
I -know- the EF Context is an UoW, I use mine at Domain level to enable transactional processing of data that is functionality related. 我知道 - EF上下文是一个UoW,我在Domain级别使用我的内核来启用与功能相关的数据的事务处理。 And it's also by customer demand, I have no choice.
而且这也是客户的需求,我别无选择。
The issue you have is that the unit of work object is effectively a singleton as your IoC framework is keeping it around for the duration of your application. 您遇到的问题是,工作单元对象实际上是一个单独的单元,因为您的IoC框架在应用程序的持续时间内保持不变。 This means that your context is also being kept as a singleton as it's inside the UoW.
这意味着您的上下文也将保留为UoW内部的单例。 So you will almost certainly get multiple concurrent calls to your context which will throw exceptions.
因此,您几乎肯定会获得对您的上下文的多个并发调用,这会引发异常。
However, I think you are misusing the concept of what a UoW supposed to do. 但是,我认为你滥用UoW应该做的概念。 A UoW is there to provide a container for a group of transactions.
UoW用于为一组事务提供容器。 For example lets say you have an eCommerce platform.
例如,假设您有一个电子商务平台。 When you create an order, you will insert a row in the orders table, then as part of the same transaction you will also insert rows into the order items table, update a users loyalty points etc. So you should do all this inside a single unit of work, commit it, then destroy it.
当您创建订单时,您将在订单表中插入一行,然后作为同一交易的一部分,您还将在订单商品表中插入行,更新用户忠诚度积分等。所以您应该在一个单一内执行所有这些操作工作单位,承诺,然后销毁它。 Let the IoC framework (Unity in this case) create your unit of work for each session.
让IoC框架(在本例中为Unity)为每个会话创建您的工作单元。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.