Asp.net mvc 在异步方法期间调用ProductService.Insert会导致商业崩溃

Asp.net mvc 在异步方法期间调用ProductService.Insert会导致商业崩溃,asp.net-mvc,async-await,nopcommerce,Asp.net Mvc,Async Await,Nopcommerce,我已经编写了一个产品导入插件,它将从一个制表符分隔的文件导入50000个产品。有必要做一个插件,因为我必须下载图片、解析类别、插入制造商等等 我的插件工作得很好。它可以在大约2分钟内生产100种产品。这意味着生产50000种产品大约需要17个小时。我想通过使我的产品插入方法异步来加快速度。当我使用ProductService的插入方法时,NopCommerce崩溃。它崩溃的文件是WebHelper和方法GetCurrentIPAddress。出于某种原因,在dbcontext.SaveChang

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

我的插件工作得很好。它可以在大约2分钟内生产100种产品。这意味着生产50000种产品大约需要17个小时。我想通过使我的产品插入方法异步来加快速度。当我使用ProductService的插入方法时,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));
            }

        }
    }
}
私有方法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);

}

我怀疑问题是由于控制器中缺少
wait
。如果没有此选项,对于已完成的请求,任何
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");
    }
}
[HttpPost]
[行政授权]
公共异步任务导入产品(配置模型)
{
尝试
{
字符串文件名=Server.MapPath(model.Location);
if(System.IO.File.Exists(fileName))
{
wait _caImportService.ImportProducts(文件名);
}
其他的
{
错误通知(“文件不存在”);
返回重定向操作(“导入产品”);
}
SuccessNotification(_localizationService.GetResource(“Admin.Catalog.Products.Imported”);
返回重定向操作(“导入产品”);
}
捕获(异常exc)
{
错误通知(exc);
返回重定向操作(“导入产品”);
}
}

这很有效。从常规到异步,我的性能没有提高though@DavidGood:你是如何衡量绩效的async只能通过减少线程池的压力来提高可伸缩性;它不会神奇地使单个请求更快。我想我误解了异步的作用。我认为它可以让我一次处理好几行csv文件,而不是一次只处理一行。