- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这几天我一直在弄乱以下内容。
我有一个在 Mono 上运行的 Nancy 应用程序,使用带有存储库模式和 UnitOfWork 的 EntityFramework,以及 Postgres。 Nancy 使用 TinyIoC 作为 IoC 容器。
我有一个 Web 应用程序,它在前端对请求进行排队,因此后端一次可以处理一个请求。这一切都很好。
但是,当我运行一个连接到同一后端的 iOS 应用程序并且不对后端请求进行排队时,问题就开始了,有时几乎同时触发请求。
后端以随机间隔开始抛出此错误:
2016-09-20T13:30:16.120057436Z app[web.1]: System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.InvalidOperationException: An operation is already in progress.
2016-09-20T13:30:16.120104535Z app[web.1]: at Npgsql.NpgsqlConnector.StartUserAction (ConnectorState newState) <0x41ad0150 + 0x00313> in <filename unknown>:0
2016-09-20T13:30:16.120113254Z app[web.1]: at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal (CommandBehavior behavior) <0x41acfe30 + 0x0002f> in <filename unknown>:0
2016-09-20T13:30:16.120119308Z app[web.1]: at Npgsql.NpgsqlCommand.ExecuteDbDataReader (CommandBehavior behavior) <0x41acfe00 + 0x00013> in <filename unknown>:0
2016-09-20T13:30:16.120125313Z app[web.1]: at System.Data.Common.DbCommand.ExecuteReader (CommandBehavior behavior) <0x41f1a3c0 + 0x00018> in <filename unknown>:0
2016-09-20T13:30:16.120131185Z app[web.1]: at (wrapper remoting-invoke-with-check) System.Data.Common.DbCommand:ExecuteReader (System.Data.CommandBehavior)
2016-09-20T13:30:16.120206045Z app[web.1]: at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c (System.Data.Common.DbCommand t, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext`1 c) <0x41f1ac20 + 0x00027> in <filename unknown>:0
2016-09-20T13:30:16.120220450Z app[web.1]: at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1[TInterceptor].Dispatch[TTarget,TInterceptionContext,TResult] (System.Data.Entity.Infrastructure.Interception.TTarget target, System.Func`3 operation, System.Data.Entity.Infrastructure.Interception.TInterceptionContext interceptionContext, System.Action`3 executing, System.Action`3 executed) <0x41b1d3c0 + 0x0010e> in <filename unknown>:0
2016-09-20T13:30:16.120232740Z app[web.1]: at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader (System.Data.Common.DbCommand command, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext interceptionContext) <0x41f1a880 + 0x00263> in <filename unknown>:0
2016-09-20T13:30:16.120267802Z app[web.1]: at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader (CommandBehavior behavior) <0x41f1a3f0 + 0x000e6> in <filename unknown>:0
2016-09-20T13:30:16.120274613Z app[web.1]: at System.Data.Common.DbCommand.ExecuteReader (CommandBehavior behavior) <0x41f1a3c0 + 0x00018> in <filename unknown>:0
2016-09-20T13:30:16.120318116Z app[web.1]: at (wrapper remoting-invoke-with-check) System.Data.Common.DbCommand:ExecuteReader (System.Data.CommandBehavior)
2016-09-20T13:30:16.120326788Z app[web.1]: at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands (System.Data.Entity.Core.EntityClient.EntityCommand entityCommand, CommandBehavior behavior) <0x41f154c0 + 0x00043> in <filename unknown>:0
2016-09-20T13:30:16.120332587Z app[web.1]: --- End of inner exception stack trace ---
2016-09-20T13:30:16.120336995Z app[web.1]: at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands (System.Data.Entity.Core.EntityClient.EntityCommand entityCommand, CommandBehavior behavior) <0x41f154c0 + 0x000b3> in <filename unknown>:0
2016-09-20T13:30:16.120344218Z app[web.1]: at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType] (System.Data.Entity.Core.Objects.ObjectContext context, System.Data.Entity.Core.Objects.ObjectParameterCollection parameterValues) <0x41f11e50 + 0x000a4> in <filename unknown>:0
我正在 Nancy Bootstrap 中注册依赖项:
protected override void ConfigureApplicationContainer (TinyIoCContainer container)
{
base.ConfigureApplicationContainer (container);
Database.SetInitializer<ReflectDbContext> (new NullDatabaseInitializer<ReflectDbContext> ()); // add this to allow prevent "The context cannot be used while the model is being created"
container.Register<IReflectDbContext, ReflectDbContext> ();
container.Register<ReflectUnitOfWork> ().AsSingleton ();
container.Register<IReflectUserRepository, ReflectUserRepository> ();
container.Register<IUserRepository<ReflectUser>, ReflectUserRepository> ();
container.Register<IReviewRepository, ReviewRepository> ();
container.Register<IReviewSetupRepository, ReviewSetupRepository> ();
container.Register<IRepositoryV2<ReflectUserActivityItem>, EntityFrameworkRepository<ReflectUserActivityItem>> ();
container.Register<IAuthenticationUnitOfWork<ReflectUser, ReflectUserActivityItem>, ReflectUnitOfWork> ();
container.Register<IRepository<ReflectUserActivityItem>, NullRepository<ReflectUserActivityItem>> (); //TODO remove this when port is complete
container.Register<IErrorLogger, SimpleLogLogger> ();
container.Register<IGeoIpDataProvider, TelizeGeoIpDataProvider> ();
container.Register<IRepository<ReviewSetup>, ServiceStackOrmLiteRepository<ReviewSetup>> ();
container.Register<IEmailExporter, MailChimpUserEmailDataExporter> ();
container.Register<IMailer, SmtpMailer> ();
container.Register<IUserManager<ReflectUser>, UserManager<ReflectUser, ReflectUserActivityItem>> ();
container.Register<IUserMessageManager<ReflectUser>, UserMessageManager<ReflectUser>> ();
etc...
}
我觉得这是一个多线程问题,两个单独的请求使用相同的 DbContext(或底层连接),这会导致事情崩溃。
我已经尝试在 Nancy Bootstrap 的 ConfigureRequestContainer
方法中注册依赖项,但这会引发“连接未打开”异常。
这篇文章清楚地解释了这个问题背后的理论:http://mehdi.me/ambient-dbcontext-in-ef6/
我不清楚以下内容:
我知道这是一个复杂的问题。如果您需要任何其他信息,请告诉我。
谢谢 :-).
最佳答案
将稍微扩展我的评论,以供将来可能有相同错误的人引用。您可能已经知道,Entity Framework 的 DbContext
遵循所谓的“工作单元”模式,这意味着您必须为一个逻辑部分(单元)的工作使用一个实例。多个工作单元重复使用同一个实例是不可取的,在某些情况下甚至可能导致失败。与 SQL Server 不同,Postgresql 不支持 MARS(多个事件结果集),这意味着它不支持同时在同一连接上执行多个命令。当您从多个线程重用 DbContext
的单个实例时,它们在执行命令时会重用相同的底层 sql 连接,这会导致上述错误。
如评论中所述,解决问题的方法始终是为每个操作创建新的 DbContext
实例,然后再处理它。这意味着将其注册为
container.Register<IReflectDbContext, ReflectDbContext> ().AsMultiInstance();
并确保您永远不会将 DbConext
实例存储在另一个类的静态字段\单例实例中(例如,您的 ReflectUnitOfWork
是单例,如果您存储 DbContext
在一个字段中 - 又是同样的问题)。
关于c# - EntityFramework DbContext 生命周期 + Postgres : "An operation is already in progress.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39595968/
这对我来说似乎很简单,但我找不到合适的属性。出于错误报告的目的,我想知道我所在的内部过程的名称。 这是最简单的示例: 运行测试。 程序测试。 /* 如何在此处显示过程名称“test”? */ 结束程序
虽然这不是我的主要职责,但我从事 Progress 4GL 已经 8 年了。我更多地使用 C++ 和 Java。当用其他语言编程时,建议声明接近用法。然而,对于 4GL,我看到人们将声明放在文件的顶部
我注意到 Progress 4gl 中的 IF 语句有一个非常奇怪的行为。 我定义了一个格式为“999”的整数,它告诉它有 3 位数字,然后我分配一个小于 100 的值(例如 12),然后当我显示它时
我有下面的代码片段(如之前的 Stack Overflow 回答... Deleting all special characters from a string in progress 4GL 中所
如何将数字和字符串添加到 Progress4GL 上的字符变量中 如下所示(这只是一个展示想法的例子)。 a = 'Code' b = 1 c = a+b 所以c的值为“Code1” 我怎样才能在pr
我有一个缓冲区,其中混合了数据、数字和字符字段。我正在显示字段的值,但由于某种原因,日期字段返回“?”当我尝试将它们添加到字符串时。 我还明白吗?即使我这样做 ASSIGN lvString = lv
有没有办法用特定字符格式化正在进行的字符串? 一个示例显示了前 6 个数字为 x 的 SSN。我已尝试使用 String 函数,但它不支持以格式发送的字母 x。 SSNString = '333224
我正在寻找好的文献来学习 Progress 4GL。这是一个与工作空间相关的项目,无法获得培训资金。我试过文档,但它不准确且相当困惑。 我将不得不在 OE 10.1B 上的 ChUI 中完成大部分工作
我如何计算总数?表中的记录?我想显示数据库中的所有表名以及编号。每个表中的记录数 最佳答案 最快的方法是: proutil dbname -C tabanalys > dbname.tab 这是一个分
我正在创建某种音频播放器,但遇到了障碍。我添加了一个进度条,它会根据正在播放的歌曲进行更新。 但是,我希望进度条可以点击并在点击时跳转到轨道的时间(就像每个普通玩家一样)。
在我的进度应用程序中使用浏览器时,滚动条永远不会正常工作。它会显示我只能向下滚动一点,然后继续前进。这是一个正在进行的错误还是我可以做些什么来解决这个问题? /* Connected Database
我们如何更改 Progress 中的默认锁而不是共享锁? 最佳答案 您可以通过使用 [NO|EXCLUSIVE|SHARE]-LOCK 修饰符将锁定状态添加到查询来在每个单独的查询(FIND、FOR
我正在使用 Material-UI的 默认情况下转换 scaleX() 的组件 的属性在另一个里面 每当值更改以可视化当前进度时。它具有缓动效果,使一切看起来都很流畅,这通常非常好,但是出于我的应用程
我很好奇 PWA 添加到主屏幕后将在后台使用什么浏览器。它是您最初选择“添加到主屏幕”的那个吗?如果是,如果我在我的 Chrome 上将 PWA 添加到主屏幕怎么办手机,然后删除 Chrome(假设现
在我问任何问题之前,让我告诉你我对 Progress Openedge 没有任何经验,但我的公司运行一个用它制作的应用程序,现在我必须通过它进行一些 SOAP 调用。所以我想,为什么不调用用我认为合适
在我提出任何问题之前,让我告诉你我对 Progress Openedge 没有任何经验,但我的公司运行一个用它制作的应用程序,现在我必须通过它进行一些 SOAP 调用。所以我想,为什么不调用用我认为合
进度处理程序已在一些领先的 promise 库(Q、When、Bluebird)中弃用,并且也已从新的 Promises/A+ spec 中删除。虽然我理解取消进度事件背后的原因,但我在重构以下我
如何改变的颜色 progress[value]::-webkit-progress-value { background-color: #00bdf8;
我有一个需要连接的进度数据库。它们与 SQL Server Management Studio 的等价物是什么? 服务器似乎是 Progress OpenEdge 10.1 最佳答案 这在一定程度上取
我在不了解 PWA 的内在含义的情况下开始构建 PWA。当我搜索时,PWA 使用以下技术逐步为 Web 应用程序提供了类似于外观的原生应用程序 舱单 服务人员 设计App shell Web 应用程序
我是一名优秀的程序员,十分优秀!