我有这个异常,,有时是
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()发布于 2018-05-16 10:15:21
using (var db = _connection)这很糟糕。您为同一连接创建一个新变量,因此在完成使用块之后,您的连接将被释放。你不应该这么做。如果您使用一个容器来创建内容,容器应该释放它不再需要的那些实例。
最有可能的情况是,您的_connection变量已被释放,任何从容器获得连接的类(在作用域情况下)或任何已经拥有该变量并由于它是实例字段而第二次使用该变量的类,都将使用已经释放的连接。
https://stackoverflow.com/questions/50366502
复制相似问题