我已经在.Net Core中插入了基于连接工厂的LinqToDB服务。
Startup.cs
services.AddScoped<IDatabase, Database>( provider =>
{
IConfigurationSection sqlConfig = Configuration.GetSection("Database");
IDataProvider dataProvider = new SqlServerDataProvider("SqlServer", SqlServerVersion.v2008);
return new Database(dataProvider,
new WorkspaceConnectionFactory(
sqlConfig["clientId"],
sqlConfig["clientSecret"],
sqlConfig["authority"],
sqlConfig["target"],
sqlConfig["connectionString"]).createConnection());
});
使用工厂的这种实现
namespace Workspace.Common.LinqToDB
{
public class WorkspaceConnectionFactory
{
private ClientCredential ClientCredential;
private string Authority;
private string Target;
private string ConnectionString;
public WorkspaceConnectionFactory(string clientId, string clientSecret, string authority, string target, string connectionString)
{
ClientCredential = new ClientCredential(clientId, clientSecret);
Authority = authority;
Target = target;
ConnectionString = connectionString;
}
public IDbConnection createConnection()
{
AuthenticationContext authenticationContext = new AuthenticationContext(Authority);
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(Target, ClientCredential).Result;
SqlConnection WorkspaceDataConnection = new SqlConnection(ConnectionString);
WorkspaceDataConnection.AccessToken = authenticationResult.AccessToken;
return WorkspaceDataConnection;
}
}
}
成功连接并进行一些查询后,出现以下异常:
System.Data.SqlClient.SqlException(0x80131904):资源ID:1.数据库的请求限制为90,并且已达到。请参见“ http://go.microsoft.com/fwlink/?LinkId=267637”以获取帮助。
在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action
1 wrapCloseInAction)
1映射器,表达式表达式,Object [] ps,Int32 queryNumber)+ MoveNext()在C:\ projects \ linq2db \ Source \ LinqToDB \ Linq \ QueryRunner.cs:第320行 在LinqToDB.Linq.Builder.TableBuilder.AssociatedTableContext.SubQueryHelper
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at LinqToDB.Data.DataConnection.ExecuteReader(CommandBehavior commandBehavior) in C:\projects\linq2db\Source\LinqToDB\Data\DataConnection.cs:line 1227 at LinqToDB.Linq.QueryRunner.ExecuteQuery[T](Query query, IDataContext dataContext, Mapper1.ExecuteSubQuery(IQueryRunner queryRunner, Object parentObject, Func
3 queryReader)+ MoveNext()在C:\ projects \ linq2db \ Source \ LinqToDB \ Linq \ Builder \ TableBuilder.AssociatedTableContext.cs:line 460 在System.Collections.Generic.List1.AddEnumerable(IEnumerable
1上可枚举) 在System.Linq.Enumerable.ToList [TSource](IEnumerable1 source) at lambda_method(Closure , IQueryRunner , IDataReader ) at LinqToDB.Linq.QueryRunner.Mapper
1.Map(IQueryRunner queryRunner,IDataReader dataReader)中位于C:\ projects \ linq2db \ Source \ LinqToDB \ Linq \ QueryRunner.cs:line 56 在LinqToDB.Linq.QueryRunner.ExecuteQuery [T](查询查询,IDataContext dataContext,Mapper1 mapper, Expression expression, Object[] ps, Int32 queryNumber)+MoveNext() in C:\projects\linq2db\Source\LinqToDB\Linq\QueryRunner.cs:line 324 at System.Linq.Enumerable.WhereEnumerableIterator
1.MoveNext() 在System.Linq.Enumerable.UnionIterator1.MoveNext() at System.Linq.Enumerable.SelectEnumerableIterator
2.MoveNext() 在System.Linq.Set1.UnionWith(IEnumerable
1个其他位置) 在System.Linq.Enumerable.DistinctIterator1.FillSet() at System.Linq.Enumerable.DistinctIterator
1.ToArray().................................. 在Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper映射器,ObjectMethodExecutor执行器,对象控制器,Object []参数) 在System.Threading.Tasks.ValueTask`1.get_Result() 在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() 在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() 在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext上下文) 在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(状态和下一个,范围和范围,对象和状态,布尔值和isCompleted)处 在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext上下文) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(状态和下一个,范围和范围,对象和状态,布尔值和已完成) 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() 在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() 在Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) 在C:\ TFS \ AT_NCSWorkspace \ WorkSpaceAPI \ src \ API \ Middlewares \ AddUserHeadersMiddleware.cs:line 47中的Workspace.API.Middlewares.AddUserHeadersMiddleware.InvokeAsync(HttpContext上下文,IWorkspaceRepositoryworkspaceRepository)中 在C:\ TFS \ AT_NCSWorkspace \ WorkSpaceAPI \ src \ API \ Middlewares \ ErrorLoggingMiddleware.cs:line 31中的Workspace.API.Middlewares.ErrorLoggingMiddleware.InvokeAsync(HttpContext上下文)中 ClientConnectionId:3c0ec020-5be1-4bda-adc1-205038682b08```
复制步骤
包括一个完整的代码清单(或项目/解决方案),我们可以使用它来重现该问题。
部分代码列表或代码的多个片段会减慢我们的响应速度,或导致我们将问题推送给您,以提供重现该问题的代码。
<code with error and mapping classes>
环境详细信息
答案 0 :(得分:-1)
将工厂更改为静态。这解决了太多的连接问题。