Is this an IIS issue or a Database Issue? Non Concurrent collections must have exclusive access

I am using asp.net core api with EF core and today I got this error message and it confuses me as of why it could have happened.

Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at Mapster.TypeAdapterConfig.Remove(TypeTuple key)
   at Mapster.TypeAdapterConfig.NewConfig[TSource,TDestination]()
   at InventoryService.GetCategories(Int32 inventoryCenterId)
   at Controllers.InventoriesController.GetCategories(Int32 centerId) 

this is the code it is referring to.

      TypeAdapterConfig<Category, CategoryDto>
           .NewConfig()
           .IgnoreIf((src, dest) => string.IsNullOrEmpty(src.MetaTitle), dest => dest.MetaTitle)
          .IgnoreIf((src, dest) => string.IsNullOrEmpty(src.MetaDescription), dest => dest.MetaDescription);

        var categories = dbContext.Centers.Where(x => x.Id == CenterId).SelectMany(x => x.Categories).OrderBy(x => x.Name).ToList();
        return categories.Adapt<List<CategoryDto>>();

It seemed like anyone who tried to access that method go the same error. I did an IIS reset and all was good but I am trying to figure out what happened.

Not sure what else to add.


Solution 1:

.NetCore intentionally throws the InvalidOperationException with mentioned message when a dictionary is modified by multiple threads. The key is to use a thread-safe ConcurrentDictionary.

Please see the GitHub Issue.