在异步方法期间调用ProductService.Insert会导致NopCommerce崩溃



我已经编写了一个产品导入插件,它将从一个制表符分隔的文件中导入50000个产品。有必要做一个插件,因为我必须下载图像,解析类别,插入制造商,等等。

我的插件工作得很好。它可以在大约2分钟内完成100个产品。这意味着制作50000个产品大约需要17个小时。我想通过使我的产品插入方法Async来加快速度。当我使用ProductService的Insert方法时,NopCommerce崩溃。它崩溃的文件是WebHelper和方法GetCurrentIPAddress。由于某种原因,此方法是在dbcontext之后立即调用的。SaveChanges方法。这真是个奇怪的问题。这里有一些示例代码,也许了解源代码的人知道发生了什么或如何解决这个问题。

我的目标是一次从文本文件中解析和添加几个产品,而不是一次只做一个。

控制器方法:

[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");
    }
}

我的服务类方法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 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);
}

我怀疑问题是由于控制器中缺少await造成的。如果没有这一点,任何async方法都将在ASP.NET请求上下文中为已经完成的请求恢复:

[HttpPost]
[AdminAuthorize]
public async Task<ActionResult> ImportProducts(ConfigurationModel model)
{
    try
    {
        string fileName = Server.MapPath(model.Location);
        if (System.IO.File.Exists(fileName))
        {
            await _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");
    }
}

最新更新