Handling Multiple Concurrent Requests in ASP.NET Core with C# - Unexpected Behavior and Performance Issues
I'm collaborating on a project where I'm currently working on an ASP.NET Core 6.0 web application, and I'm running into some issues when trying to handle multiple concurrent requests. I'm using a singleton service to manage state, but it seems like I'm experiencing race conditions that lead to inconsistent results. Here's a simplified version of my singleton service: ```csharp public class DataService { private List<string> _data = new List<string>(); private readonly object _lock = new object(); public void AddData(string item) { lock (_lock) { _data.Add(item); } } public List<string> GetData() { lock (_lock) { return new List<string>(_data); } } } ``` In my controller, I'm calling this service like so: ```csharp [ApiController] [Route("api/[controller]")] public class DataController : ControllerBase { private readonly DataService _dataService; public DataController(DataService dataService) { _dataService = dataService; } [HttpPost] public IActionResult Post(string item) { _dataService.AddData(item); return Ok(); } [HttpGet] public ActionResult<List<string>> Get() { return _dataService.GetData(); } } ``` When I run load tests simulating multiple users adding data at the same time, I notice that sometimes items are missed or duplicated when retrieving the list. I also get intermittent exceptions related to accessing the list, although the lock should prevent concurrent modifications: ``` System.InvalidOperationException: Collection was modified; enumeration operation may not execute. ``` I've tried increasing the lock granularity by changing the way I handle the list, but that doesn't seem to help. Is there a better design pattern or approach to manage concurrent access to shared state in this scenario? Should I be considering other ways to make this more robust, like using Concurrent Collections or a different state management approach? Any insights would be greatly appreciated! I'd be grateful for any help. Am I missing something obvious?