C# 依赖注入IDbConnection SqlConnection
我有时会提出这个例外:C# 依赖注入IDbConnection SqlConnection,c#,asp.net-core,dependency-injection,C#,Asp.net Core,Dependency Injection,我有时会提出这个例外: System.InvalidOperationException: 'The ConnectionString property has not been initialized.' 我使用内置的依赖项注入: public void ConfigureServices(IServiceCollection services) { services.AddTransient<IDbConnection>(db => new SqlConnecti
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();
}
这很糟糕。您为同一连接创建了一个新变量,因此在使用块完成后,您的连接将被释放。你不应该这样做。如果您使用容器来创建内容,容器应该处理那些不再需要的实例
最有可能发生的情况是,您的
\u连接
变量已被释放,任何从容器获取连接的类(如果是作用域的)或任何已经具有此变量并再次使用它的类,因为它是一个实例字段,将使用已释放的连接。是否确定AppConnectionString
连接字符串存在?哪一行引发异常?请将完整堆栈跟踪添加到ConfigureServices
中。Pro提示:将配置文件的读取从注册lambda中移出(即db=>…
)。这样,您可以在应用程序启动时读取和验证配置值,并让应用程序在该点失败,而不是在解析对象图时失败在需要时创建连接。使用像EF Core这样的ORM要容易得多though@Florian你在使用Dapper吗?使用DI连接更糟糕。这意味着单个连接将在很长时间内保持活动状态,积累锁并阻塞其他连接。只要ADO.NET驱动程序提供连接,就没有理由对连接使用DIpooling@PanagiotisKanavos比什么更糟?它已经和DI一起使用了。我只是说如果他有使用块,他使用DI是错误的。我不能说最好的解决方案是什么,或者是使用块的,或者是DI,但两者不应该以那种方式结合在一起。更好的解决方案是按照ADO.NET的意图使用工厂。不要使用.NET1.1接口,而是注入并使用.NET2@PanagiotisKanavos。虽然我大体上同意,但我倾向于回答问题。问题源于处理容器创建的东西。不管对象是什么,这都是不好的。
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)