.net 已经有一个与此命令关联的打开的DataReader,必须首先关闭该命令并调用actionfilter数据库

.net 已经有一个与此命令关联的打开的DataReader,必须首先关闭该命令并调用actionfilter数据库,.net,sql-server,asp.net-mvc,session,asp.net-web-api,.net,Sql Server,Asp.net Mvc,Session,Asp.net Web Api,这起作用了 我有一个非常奇怪的问题。我收到错误已经有一个与此命令关联的打开的DataReader,必须先关闭。来自API调用。但是,此错误仅发生在这台服务器上,而不是也包含数据库的主服务器上 代码是精确的,web.config是相同的,除了在主服务器上使用localhost的连接字符串 此API调用使用会话cookie属性来验证用户。这是asp.net成员资格的唯一独特之处 唯一不同的是服务器错误关闭了TLS1.0,但主服务器没有。我也不想启用MultipleActiveResultSets,因

这起作用了

我有一个非常奇怪的问题。我收到错误
已经有一个与此命令关联的打开的DataReader,必须先关闭。
来自API调用。但是,此错误仅发生在这台服务器上,而不是也包含数据库的主服务器上

代码是精确的,web.config是相同的,除了在主服务器上使用localhost的连接字符串

此API调用使用会话cookie属性来验证用户。这是asp.net成员资格的唯一独特之处

唯一不同的是服务器错误关闭了TLS1.0,但主服务器没有。我也不想启用
MultipleActiveResultSets
,因为这以前一直有效。任何帮助都会很好

https://example.com/api/v1/test/help?testid=109178&_COOKIE_=<cookie>
StackTrace

Stack Trace :   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at Tournaments.Data.Repositories.RepositoryBase`1.Get(Expression`1[] includes, Expression`1 where) in D:\Projects\Tournaments.Data\Repositories\RepositoryBase.cs:line 110
   at Tournaments.Services.Api.ApiAuthenticationService.ValidUser(Guid userId) in D:Api\ApiAuthenticationService.cs:line 116

这是一个并发问题;
ActionFilterAttribute
被视为一个单实例,因此单个实例处理所有请求。
因此,多个并发请求共享相同的
\u apiAuthenticationService
实例,该实例包含
DbContext
。这会导致在共享的
DbContext
实例上同时执行查询/命令的可能性,该实例会因给定的异常而失败

出现此错误的时间和频率取决于Web请求的流量以及
DbContext
/数据库执行查询/命令的速度。
(该TLS错误可能已经导致一些延迟。)


解决方案:不要在
ActionFilter
中使用共享状态;为每个请求分别解析/实例化依赖项。

如果看不到创建错误的代码,我们将无能为力。
GlobalConfiguration.Configuration.Filters.Add(新的APIValidatesCoresFilter(container.GetInstance())在Global.asax中完成。我想在这个actionfilter中执行一些数据库代码,那么在使用DI时,如何解决这个共享状态问题呢?我也很高兴我找到了一个正确的答案。你能注入一个工厂方法,通过它每次解析/返回一个新实例吗?另外,我看到框架在这方面取得了相当大的进展,但这取决于您所使用的MVC/WebAPI版本;例如,不幸的是,我还没有使用ASP.NET内核,并且使用MVC 5,SimpleInjactor作为DI。
SimpleInjactor
允许注入工厂方法/委托。在您的
ActionFilter
中,您通过该工厂方法解析了一个新的
apauthenticationservice
实例。它通过直接调用该服务来工作,这不完全是我想要的,但目前没有时间查看任何其他内容。你能把这个放在你的答案
DependencyResolver.Current.GetService().ValidUser(新的Guid(user.ProviderUserKey.ToString())中吗
Stack Trace :   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at Tournaments.Data.Repositories.RepositoryBase`1.Get(Expression`1[] includes, Expression`1 where) in D:\Projects\Tournaments.Data\Repositories\RepositoryBase.cs:line 110
   at Tournaments.Services.Api.ApiAuthenticationService.ValidUser(Guid userId) in D:Api\ApiAuthenticationService.cs:line 116
 Message :There is already an open DataReader associated with this Command which must be closed first.  Source :System.Data  Stack Trace :   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)     at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)     at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)     at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)     at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)     at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)     at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)     at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)  TargetSite :Void ValidateConnectionForExecute(System.Data.SqlClient.SqlCommand)