Recently, I created a Web Request application to retrieve news articles on a website based on a date given. I created a collection of dates i.e. [06-07-2021, 06-08-2021, 06-01-2021, etc.], and have a for loop that runs on the collection and returns the news article for that date. In order to speed up this process, I created a Parallel.ForEach loop that runs on the collection and it stores the news articles in a Dictionary where the key is the date and the value is the article(s). Inside the Parallel loop, I receive a *System.InvalidOperationException when I attempt to add the key-value pair into the Dictionary. It doesn't happen all the time, so I can typically resolve the issue by restarting the program. Another exception I receive is the **System.NullReferenceException which I think is strange because every variable is assigned a value even my methods return a value, albeit an empty string if there is a problem with the request. This exception is also resolved by restarting the application.
My question is why is there an inconsistency in exceptions when running this application?
*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.
**Object reference not set to an instance of an object.