微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nopcommerce 4.4 自定义:等待方法抛出空引用异常

如何解决nopcommerce 4.4 自定义:等待方法抛出空引用异常

我正在将自定义更改从 nopcommerce 4.3 移至 4.4。 nop 4.4 完全是用 async 和 await 方法构建的,我是这种异步等待编码的新手。 当我做简单的方法改变时,我得到了

“未将对象引用设置为对象错误的实例”。

我在剥离其他代码后创建了一个简单的方法显示问题。

请告诉我如何解决此问题。

在这一行收到错误

var products1 = await _productModelFactory.PrepareProductOverviewmodelsAsync(lst1);

enter image description here

public virtual async Task<WishlistModel> PrepareWishlistModelAsync1(WishlistModel model,IList<ShoppingCartItem> cart,bool isEditable = true)
{
    List<Product> lst1 = new List<Product>();
    lst1.Add(new Product { Id = 123,Name = "Product 1" });

    var products1 = await _productModelFactory.PrepareProductOverviewmodelsAsync(lst1);

    return model;
}

public virtual async Task<IEnumerable<ProductOverviewmodel>> PrepareProductOverviewmodelsAsync(IEnumerable<Product> products,bool preparePriceModel = true,bool preparePictureModel = true,int? productThumbPictureSize = null,bool prepareSpecificationAttributes = false,bool forceRedirectionAfteraddingToCart = false)
{
    if (products == null)
        throw new ArgumentNullException(nameof(products));

    var models = new List<ProductOverviewmodel>();
    foreach (var product in products)
    {
        var model = new ProductOverviewmodel
        {
            Id = product.Id,Name = await _localizationService.GetLocalizedAsync(product,x => x.Name),ShortDescription = await _localizationService.GetLocalizedAsync(product,x => x.ShortDescription),FullDescription = await _localizationService.GetLocalizedAsync(product,x => x.FullDescription),SeName = await _urlRecordService.GetSeNameAsync(product),Sku = product.Sku,ProductType = product.ProductType,MarkAsNew = product.MarkAsNew &&
                (!product.MarkAsNewStartDateTimeUtc.HasValue || product.MarkAsNewStartDateTimeUtc.Value < DateTime.UtcNow) &&
                (!product.MarkAsNewEndDateTimeUtc.HasValue || product.MarkAsNewEndDateTimeUtc.Value > DateTime.UtcNow)
        };

        // Customization
        if (product.vendorId > 0)
        {
            var customer = await _customerService.GetCustomerByIdAsync(product.vendorId);

            model.FirstName = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.FirstNameAttribute);
            model.LastName = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.LastNameAttribute);
            model.Phone = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.PhoneAttribute);

            model.Gender = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.GenderAttribute);
            model.Company = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.CompanyAttribute);
            model.CountryId = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.CountryIdAttribute);

            model.StateProvinceId = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.StateProvinceIdAttribute);
            model.LanguageId = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.LanguageIdAttribute);
            model.TimeZoneId = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.TimeZoneIdAttribute);
            model.AvatarPictureId = await _genericAttributeService.GetAttributeAsync<string>(customer,nopCustomerDefaults.AvatarPictureIdAttribute);

            model.CustomerProfileTypeId = model.FirstName;
        }

        //price
        if (preparePriceModel)
        {
            model.ProductPrice = await PrepareProductOverviewPriceModelAsync(product,forceRedirectionAfteraddingToCart);
        }

        //picture
        if (preparePictureModel)
        {
            model.DefaultPictureModel = await PrepareProductOverviewPictureModelAsync(product,productThumbPictureSize);
        }

        //specs
        if (prepareSpecificationAttributes)
        {
            model.ProductSpecificationModel = await PrepareProductSpecificationModelAsync(product);
        }

        //reviews
        model.ReviewOverviewmodel = await PrepareProductReviewOverviewmodelAsync(product);

        models.Add(model);
    }

    return models;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。