c# - 依赖注入 IDbConnection SqlConnection
问题描述
我有时会提出这个异常:
System.InvalidOperationException: 'The ConnectionString property has not been initialized.'
我使用内置的依赖注入:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IDbConnection>(db => new SqlConnection(
Configuration.GetConnectionString("AppConnectionString")));
services.AddScoped<IAppConfigurationRepository, AppConfigurationRepository>();
services.AddScoped<IHomeworkingRequestRepository, HomeworkingRequestRepository>();
services.AddScoped<IEmployeeRepository, EmployeeRepository>();
services.AddScoped<IEmployeeService, EmployeeService>();
services.AddScoped<IHomeworkingRequestService, HomeworkingRequestService>();
services.AddMvc();
}
之前,我已经有这个错误。services.AddScoped<IDbConnection>
我改成的代码services.AddTransient<IDbConnection>
解决了这个问题。但现在我又遇到了错误。
编辑
发生异常时请查找代码:
public class EmployeeRepository : IEmployeeRepository
{
private readonly IDbConnection _connection;
public EmployeeRepository(IDbConnection connection)
{
_connection = connection;
}
public IEnumerable<Employee> GetAllActiveEmployees()
{
string query = @"
SELECT
FirstName
,LastName
,BusinessUnit
FROM Employees";
using (var db = _connection)
{
_connection.Open(); // <-- The exception is thrown here
return db.Query<Employee>(query);
}
}
}
另请查找完整的堆栈跟踪:
at System.Data.SqlClient.SqlConnection.PermissionDemand()
at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Homeworking.Dal.EmployeeRepository.GetAllActiveEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Repository\EmployeeRepository.cs:line 42
at Homeworking.Service.EmployeeService.GetAllEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Service\EmployeeService.cs:line 22
at Homeworking.Service.HomeworkingRequestService.GetAllEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Service\HomeworkingRequestService.cs:line 23
at Homeworking.Web.Controllers.AppController.Index() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Web\Controllers\AppController.cs:line 22
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
解决方案
using (var db = _connection)
这是不好的。您为同一连接创建一个新变量,因此在 using 块完成后,您的连接将被释放。你不应该这样做。如果你使用容器来创建东西,容器应该处理那些它不再需要的实例。
最有可能发生的事情是您的_connection
变量已被释放,并且任何从容器获取连接的类(在 Scoped 的情况下)或任何已经具有此变量并第二次使用它的类,因为它是一个实例字段,将使用已经部署的连接。
推荐阅读
- python - 如何在 psycopg2 连接方法中指定 Schema?
- centos - 如何更改 MariaDB 的数据目录?
- http - Flutter中从api获取后如何拆分文本数据
- c++ - 如果输入由单个空格分隔,输入运算符 (>>) 如何知道在哪里停止并获取下一个输入?
- javascript - 获取 page.evaluate 不是函数错误
- qt - 无法构建简单的 QT5 项目
- angular - 当通过 Visual Studio 2019 for Angular 从 Package.json 恢复包时,显示以下错误,我使用的是 DOT NET 而不是 DOT NET CORE
- python - 使用包含波斯记录的数据集
- google-apps-script - 使用标准 Google Cloud Platform (GCP) 脚本读取 Gmail POST 通知
- ios - APNS 连接失败