Strange Task Behaviour

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
In a Custom Nopcommerce Task I'm experiencing a strange behaviour.

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
11 years ago
Some updates

While I was  trying to isolate the problem I experienced also this :

The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.

at this line :

categoriesToDiscount[x].AppliedDiscounts.Add(DiscountToApply);

I really don't understand..
11 years ago
uhm... is it possible detach an entity from the ObjectContext ?

For some reasons , when executed as a task by nopcommerce task manager, every service looks like belong to a different ObjectContext....
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.