Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityFramework DbContext生命周期+;博士后:“;操作已在进行中。”;_C#_Entity Framework_Nancy_Npgsql_Tinyioc - Fatal编程技术网

C# EntityFramework DbContext生命周期+;博士后:“;操作已在进行中。”;

C# EntityFramework DbContext生命周期+;博士后:“;操作已在进行中。”;,c#,entity-framework,nancy,npgsql,tinyioc,C#,Entity Framework,Nancy,Npgsql,Tinyioc,这几天我一直在搞下面的事情 我有一个Nancy应用程序在Mono上运行,EntityFramework带有存储库模式和UnitOfWork,还有Postgres。Nancy使用TinyIoC作为IoC容器 我有一个web应用程序,它可以在前端对请求进行排队,这样后端就可以一次命中一个请求。这一切都很好 然而,当我运行一个连接到同一后端的iOS应用程序时,问题就开始了,它不会将请求排队到后端,有时几乎同时触发请求 后端会以随机间隔开始抛出此错误: 2016-09-20T13:30:16.12005

这几天我一直在搞下面的事情

我有一个Nancy应用程序在Mono上运行,EntityFramework带有存储库模式和UnitOfWork,还有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
2016-09-20T13:30:16.120057436Z应用程序[web.1]:System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时出错。有关详细信息,请参见内部异常。-->System.InvalidOperationException:操作已在进行中。
2016-09-20T13:30:16.120104535Z应用程序[web.1]:位于Npgsql.NpgsqlConnector.StartUserAction(ConnectorState newState)中:0
2016-09-20T13:30:16.120113254Z应用程序[web.1]:位于:0中的Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior)
2016-09-20T13:30:16.120119308Z应用程序[web.1]:位于:0中的Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior)
2016-09-20T13:30:16.120125313Z应用程序[web.1]:位于:0中的System.Data.Common.DbCommand.ExecuteReader(CommandBehavior)
2016-09-20T13:30:16.120131185Z应用程序[web.1]:at(包装器远程调用检查)System.Data.Common.DbCommand:ExecuteReader(System.Data.CommandBehavior)
2016-09-20T13:30:16.120206045Z应用程序[web.1]:位于System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b_uc(System.Data.Common.DbCommand t,System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext`1 c)中:0
2016-09-20T13:30:16.120220450Z应用程序[web.1]:at System.Data.Entity.Infrastructure.InternalDispatcher`1[TInterceptor].Dispatch[TTarget,TInterceptionContext,TResult](System.Data.Entity.Infrastructure.Interception.t目标,System.Func`3操作,System.Data.Entity.Infrastructure.Interception.TInterceptionContext拦截上下文,System.Action`3正在执行,System.Action`3已执行)位于:0
2016-09-20T13:30:16.120232740Z应用程序[web.1]:位于:0中的System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(System.Data.Common.DbCommand命令,System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext interceptionContext)
2016-09-20T13:30:16.120267802Z应用程序[web.1]:位于0中的System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior)
2016-09-20T13:30:16.120274613Z应用程序[web.1]:位于:0中的System.Data.Common.DbCommand.ExecuteReader(CommandBehavior)
2016-09-20T13:30:16.12031816Z应用程序[web.1]:at(包装器远程调用,带检查)System.Data.Common.DbCommand:ExecuteReader(System.Data.CommandBehavior)
2016-09-20T13:30:16.120326788Z应用程序[web.1]:位于:0中的System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(System.Data.Entity.Core.EntityClient.EntityCommand EntityCommand,CommandBehavior)
2016-09-20T13:30:16.120332587Z应用程序[web.1]:---内部异常堆栈跟踪结束---
2016-09-20T13:30:16.120336995Z应用程序[web.1]:位于:0中的System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(System.Data.Entity.Core.EntityClient.EntityCommand EntityCommand,CommandBehavior)
2016-09-20T13:30:16.120344218Z应用程序[web.1]:在0中的System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.ExecutionType](System.Data.Entity.Core.Objects.ObjectContext,System.Data.Entity.Core.Objects.ObjectParameterCollection参数值)执行[TResultType]
我正在Nancy bootstrapper中注册依赖项:

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...

}
受保护的覆盖无效配置应用程序容器(TinyIoCContainer)
{
base.ConfigureApplicationContainer(容器);
Database.SetInitializer(新的NullDatabaseInitializer());//添加此选项可防止“在创建模型时无法使用上下文”
container.Register();
container.Register().AsSingleton();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();//在端口完成时删除此项
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
container.Register();
等
}
我有一种感觉,这是一个多线程问题,两个单独的请求使用相同的DbContext(或底层连接),这会导致问题爆发

我已经尝试在Nancy bootstrapper的
ConfigureRequestContainer
方法中注册依赖项,但这会引发“Connection is not open”异常

本文对这一问题背后的理论进行了明确解释:

我不清楚以下几点:

  • 我假设这是一个多线程问题,对吗
  • 我需要知道正确的方法来确保每个请求使用自己的DbContext/连接,这样东西就不会发生冲突,最好使用TinyIoC/Nancy来管理DbContext的生命周期
我理解这是一个复杂的问题。如果您需要任何其他信息,请告诉我


谢谢:-)。

将稍微扩展我的评论,以供将来可能有相同错误的人参考。您可能已经知道,实体框架的
DbContext
遵循所谓的“工作单元”模式,这意味着您必须为一个逻辑工作块(单元)使用一个实例。重复使用相同的插件
container.Register<IReflectDbContext, ReflectDbContext> ().AsMultiInstance();