The question is why do you need to run these in parallel? Many code is not meant to be thread safe (i.e. to run in parallel)
If you aim to improve performance, maybe try to profile to see where the bottleneck is?
Hi Quan, thanks for your reply.
We need this because some products may have more than 100 options and mapping them all one by one will take a lot of time, which is not acceptable for our client.
We have complex logic for variant mapping, including calculating the displayed price depending on applied and available discounts, custom calculation of available quantities and other business logic. Yes, perhaps we can review the code in the mapper and gain some time, but in any case, without parallel processing of variants, this process may take much longer.
If you mean that we can't use parallel processing (and therefore methods like "Task.WhenAll") if we want to use "ContentRepository" or methods from "CustomerContext.Current", then of course we will have to look for another solution. But before that, I would like to understand if there is any way to make these methods work.
Another example of what for we use "Parallel.ForEach" is API that can accept thousands of product data and save them in catalog. We process them in parallel and save them using "IContentRepository.Save" method. This method throws different types of errors: "The transaction operation cannot be performed because there are pending requests working on this transaction" , "'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", null reference errors.
You can look at IContentRepository.Publish which can publish multiple content at once. it was added to improve performance, without having to use multiple threads.
Thanks, I'll take a look and see if this method helps with saving content. But I still want to understand if I can do something to fix the first issue.
I also want to note that the same code works fine in the previous version. What has changed so that now the same code fails with an error?
Hello,
We recently migrated the project to the ASP.NET Core. After this, we faced a bug with code that runs several methods in parallel.
We have a product mapper class that gets prepared variant models using this code:
Inside "_variantMapper.Map" we use "PromotionEngine.Run()" method and "CustomerContext.Current.GetContactById(customerId)" method. And when loading the product page, one of these methods throws an error every time. Here are examples of these errors:
I found this article https://world.optimizely.com/blogs/Johan-Bjornfot/Dates1/2023/8/parallel-tasks-and-backgroundcontext/ and tried to use "IBackgroundContextFactory.Create()" inside the lambda method in "Parallel.ForEach". Here's the updated code:
Thanks to this code, the number of errors has decreased. Only one type of error occurs: "Object reference not set to an instance of an object" inside the "CustomerContext.Current.GetContactById(Guid contactId)" (it seems that "PromotionEngine.Run()" use this method too). And I only got one of these errors the first time I loaded the page, and the next time I loaded the product page it opened (at least the error didn't occur every time I loaded the page). But if I reload the page in the browser several times right after it loads, I can still catch one of these errors.
I'm not sure what we can do from our code side to get rid of this error. Can you help me how to properly run several methods in parallel so that a this exception doesn't occur?
Thanks!