We are using the uCommerce Stateless API to bulk import products into uCommerce (Umbraco), see: https://docs.ucommerce.net/ucommerce/v9.5/system-integration/bulk-import-from-third-party-systems.html
The Import is taking 12 hours to complete, which is far too slow and we need to introduce parallelism.
Our code uses the async/await pattern in a .Net Framework console app.
I create a list of type Task and then execute in parallel using await Task.WhenAll(tasks);
The Task is returned by the following method.
However, this causes an exception when the method is called for each Task:
- Outer exception: "There was a problem converting an DbDataReader to
NDataReader" - Inner exception: “Can not start another operation while there is an asynchronous operation pending”
This appears to be an underlying constraint of uCommerce/NHibernate. I’m guessing because it’s sharing a DBContext somewhere.
Q. How do we work with the uCommerce Stateless API with parallelism?
Is there a way to get independent instance of the ISessionProvider?
public async Task ProcessProductAsync(dynamic currentProduct, PimApi api)
{
var statelessSession = ObjectFactory.Instance.Resolve<IStatelessSessionProvider>().GetStatelessSession();
string sku = ExpandoHelper.GetListProperty(currentProduct, DynamicConstants.ParentsId);
string variantSku = ExpandoHelper.GetProperty(currentProduct, DynamicConstants.PrimaryId);
if (string.IsNullOrEmpty(sku) || string.IsNullOrEmpty(variantSku))
return;
var existingProduct = await statelessSession
.Query<Product>()
.FetchMany(x => x.ProductDescriptions)
.ThenFetchMany(x => x.ProductDescriptionProperties)
.ThenFetch(x => x.ProductDefinitionField)
.FetchMany(x => x.CategoryProductRelations)
.ThenFetch(x => x.Category)
.FetchMany(x => x.ProductPrices)
.FetchMany(x => x.ProductProperties)
.Fetch(x => x.ProductDefinition)
.ThenFetch(x => x.ProductDefinitionFields)
.SingleOrDefaultAsync(x => x.VariantSku == variantSku);