Hi,
I am displaying SQL DB table column value change in Angular UI using Signal R and EF Core. When there is a value change in database table i am getting below error message.
System.InvalidOperationException: 'A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext.
Code snippet:
await Clients.Clients(this.Context.ConnectionId).SendAsync("marketPriceChangeResponse", marketPrice.ToList());
Gowtham CpPosted Aug 26, 2024, 2:52 AM
You're getting that error because multiple threads are using the same `DbContext` instance. To fix it:
1. Use a new `DbContext` instance for each SignalR connection by injecting it with a scoped lifetime.
2. Always use async methods like `ToListAsync()` and await them properly.
This will ensure that each connection handles database operations independently.
Hope it helps!