Calling ProductService.Insert during an Async method crashes NopCommerce

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 years ago
I have written a product import plugin that will import 50,000 products from a tab delimited file. It's necessary to do a plugin because I have to download images, parse categories, insert manufacturers, etc.

My plugin works great. It can do 100 products in about 2 minutes. This means it will take about 17 hours to do 50,000 products. I wanted to speed this up by making my product insert method Async. When I go to use the ProductService's Insert method NopCommerce crashes. The file it crashes under is the WebHelper and method GetCurrentIPAddress. For some reason this method is being called right after the dbcontext.SaveChanges method. This is a really weird problem. Here is some sample code and maybe someone that knows the source has an idea of what is going on or how to fix this problem.

Controller Method:

[HttpPost]
[AdminAuthorize]
public ActionResult ImportProducts(ConfigurationModel model)
{
  try
  {
    string fileName = Server.MapPath(model.Location);
    if (System.IO.File.Exists(fileName))
    {
      _caImportService.ImportProducts(fileName);
    }
    else
    {
      ErrorNotification("File does not exist");
      return RedirectToAction("ImportProducts");
    }
    SuccessNotification(_localizationService.GetResource("Admin.Catalog.Products.Imported"));
    return RedirectToAction("ImportProducts");
  }
  catch (Exception exc)
  {
    ErrorNotification(exc);
    return RedirectToAction("ImportProducts");
  }
}

My Service Class method CAImportService.ImportProducts:

public virtual async Task ImportProducts(string fileName)
{
  //Open file and start reading in line by line
  using(var file = new StreamReader(fileName))
  {
    var csv = new CsvReader(file);

    while (csv.Read())
    {
      if (csv.GetField("Blocked").ToLower() == "false")
      {
        //Set up general product properties and add product
        //Omitted for brevity
        await Task.Run(() => ImportProduct(name, shortDescription, description, pleaseNote, sku, manufacturerPartNumber,
        stockQuantity, price, oldPrice, weight, length, width, height, closeout, warranty,
        brand, tags, categories, pictures));
      }
      
    }
  }
}


Private Method InsertProduct:

private void ImportProduct(string name, string shortDescription, string description, string pleaseNote,
  string sku, string manufacturerPartNumber, int stockQuantity, decimal price, decimal oldPrice,
  decimal weight, decimal length, decimal width, decimal height, string closeout, string warranty,
  string brand, string tags, string categories, string pictures)
{

    var product = new Product();

    // Omitted Set Up Product Properties

    // Crashes after calling this line upon calling dbcontext.SaveChanges
    _productService.InsertProduct(product);

}
9 years ago
My goal is to parse and add several products from the text file at once rather than just doing them one at a time.
9 years ago
I actually had to make my controller method Async also

public async Task<ActionResult> ImportProducts(ConfigurationModel model)

That fixed it but there is no performance increase going from normal to async
9 years ago
Look into SqlBulkCopy
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.