Null reference in web api call

扶醉桌前 提交于 2019-11-30 22:31:49

The problem is that Dictionary is not thread-safe.

Look at the stack trace, the exception is thrown inside Dictionary's internal Insert method. So cache is definitely not null. id can't be null either, because Guid is a value type. Null values are allowed in a dictionary so it doesn't matter if param is null. The problem is that one thread is probably in the middle of an update that caused the dictionary to reallocate its internal buckets while another thread goes tries to insert another value. Some internal state is inconsistent and it throws. I'd bet this happens once in a while during initialization because that's when caches tend to get filled.

You need to make this class thread-safe which means locking access to the dictionary or (even easier) using a class like ConcurrentDictionary that is designed to be thread-safe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!