The Task is written to synchronize customers , discount and products of an ERP with a nopcommerce shop.
The Task class correctly implements ITask and I call my own method from the Execute method in this way:
#region ITask
public void Execute()
{
Synchronize(DateTime.Now, 1, pageSize);
}
public void Synchronize(DateTime StartSynchronizeFrom, int index, int pageSize)
{
(do a lot of stuff... )
}
The problem is the following snippet of code.
Inside the Synchronize method, when I create the discounts rules , the task insert two discounts record inside the database. One of them with all the categories assigned, the other without any category assigned.
_discountService.InsertDiscount(DiscountToApply);
//apply the discount to all the categories
IList<Category> categoriesToDiscount = new List<Category>();
categoriesToDiscount = _categoryService.GetAllCategories();
for (int x = 0; x < categoriesToDiscount.Count; x++)
{
if (categoriesToDiscount[x].AppliedDiscounts.Where(d => d.Id == DiscountToApply.Id).Count() == 0)
{
categoriesToDiscount[x].AppliedDiscounts.Add(DiscountToApply);
_categoryService.UpdateCategory(categoriesToDiscount[x]);
_categoryService.UpdateHasDiscountsApplied(categoriesToDiscount[x]);
}
}
If I manually call Synchronize in background with
ThreadPool.QueueUserWorkItem(plugin.Synchronize, StartSynchronizeFrom);
from a controller, everything works nicely, so the logic inside the Synchronize method should be correct.
I'm missing something ?
Thanks for any help.
Tommaso