I am new to using dependency injection and had come a long way in my blazor project before I really started using it.
I added DBContext as Transient service(as mentioned in many answers here) and injected it into DataAccessLayer like so:
public class DataAccessLayer
{
public DataAccessLayer(GlobalVariables s,DataContext d)
{
GlobalVariables = s;
context = d;
}
private readonly GlobalVariables GlobalVariables;
private readonly DataContext context;
`/other code here`
Now as I am using razor components in my blazor project when I open one page, around 5 components start to render. All of these components are fetching data from DataAccessLayer.
I run getting these two errors:
System.InvalidOperationException: Invalid attempt to call ReadAsync when reader is closed.
System.InvalidOperationException: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with DbContext
When i remove the dependency and add (using Datacontext), the errors go away and it runs fine. Can anybody suggest me how to properly inject it?
P.S: I have already checked that all my async methods have await and configureawait(true)
The method throwing the first exception of readasync is this :
public async Task<List<MasterCustomer>> getCustomersBinding()
{
List<MasterCustomer> customersList = new List<MasterCustomer>();
{
customersList = await (from table in context.MasterCustomer where (table.IsActive == true) select (new MasterCustomer { Code = table.Code, Name = table.Name })).ToListAsync().ConfigureAwait(true); ;
}
return customersList;
}
ReadAsync
? Could you show the code of that method – Vencovsky