注入的数据库上下文无法在高负载下连接

时间:2018-06-20 09:39:50

标签: entity-framework asp.net-web-api autofac

我在Web api项目中将AutoFac用于依赖项注入器。 我正在解决如下所示的依赖性...

builder.RegisterType<DbContext>().InstancePerRequest().WithParameter("nameOrConnectionString", "name=TestDbContext");
        builder.RegisterType<TestDbContext>().InstancePerRequest();
        builder.RegisterType<TestDbContext>().As<ITestDbContext>().InstancePerRequest();
        builder.RegisterType<TestDbRepository>().As<ITestDbRepository>().InstancePerRequest();

当击中一个api路由时,它工作正常。但是,当我们的QA团队使用jMeter进行性能测试时,一次遇到多个请求,一些请求将失败并获得与数据库相关的错误,例如以下错误...

 The underlying provider failed on Open.
System.InvalidOperationException: The connection was not closed. The connection's current state is connecting.
   at System.Data.ProviderBase.DbConnectionClosedConnecting.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()

但是此错误有时无法通过,但有时会出现。 如果我们需要使用Autofac依赖解析器进行任何修改,请帮助我。

0 个答案:

没有答案