Blazor服务器列出页面加载中的行为



我有一个Blazor Server项目。NET 5,它在一个特定的页面上给了我一些令人难以置信的奇怪行为。

我有一个名为Products的页面,其中包含一个自定义组件ProductSettings,它包含两个列表——List<Location>List<Product>。该页面继承自BasePage,CCD_5进行一些初步查找(业务信息等(

<ProductSettings Products="@products" Locations="@locations"></ProductSettings>

我对页面和组件都使用OnParametersSet方法。页面方法如下所示:

protected List<Product> products { get; set; }
protected List<string> locations { get; set; }
protected override void OnParametersSet()
{
log.LogTrace("Products.ParametersSet start");
products = new List<Product>();
locations = locationRepo.GetLocationsByBusiness(business!.BusinessId).Select(x => x.Handle).ToList();

var l1 = productsRepo.GetProducts(locations[0], business!.BusinessId);

log.LogTrace(@"{Loc0} location ids:",locations[0]);
foreach (var l in l1)
{
log.LogTrace(l.LocationId);
}
var l2 = productsRepo.GetProducts(locations[1], business!.BusinessId);
log.LogTrace(@"{Loc0} location ids:",locations[1]);
foreach (var l in l2)
{
log.LogTrace(l.LocationId);
}
products.AddRange(l1);
products.AddRange(l2);

log.LogTrace("main array location ids:");
foreach (var p in products)
{
log.LogTrace(p.LocationId);
}
log.LogTrace("Products page params set");
base.OnParametersSet();
}

locationRepo.GetLocationsByBusinessproductsRepo.GetProducts方法是同步的,并从Azure表存储中提取数据。

我得到的错误是,尽管我的循环成功地完成了每个LocationProduct数据(我有两个位置,每个位置有4个产品(,但我最终得到了一个8个产品的列表,这些产品都与第二组产品相匹配。换句话说,在上面的代码中,我得到了这个跟踪输出:

09:38:28 trce: AdminSite.Components.BasePage[0] business-1 location ids:
09:38:28 trce: AdminSite.Components.BasePage[0] business-1
09:38:28 trce: AdminSite.Components.BasePage[0] business-1
09:38:28 trce: AdminSite.Components.BasePage[0] business-1
09:38:28 trce: AdminSite.Components.BasePage[0] business-1
09:38:28 trce: AdminSite.Components.BasePage[0] business-2 location ids:
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] main array location ids:
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2
09:38:28 trce: AdminSite.Components.BasePage[0] business-2

就我的一生而言,我无法理解它为什么要这样做。它似乎完全覆盖了列表中的前四条记录。我有其他页面使用类似的方法,我没有遇到任何类似的问题。起初,我认为这与我的repos中的其他逻辑部分有关,我已经研究了List<T>的文档,但我找不到任何东西表明对象会像这样合并。我还有单元测试来验证和健全性检查AddRange和我的其他逻辑,并且我的库中的一切都是一致的——只有在这个Blazor页面中,我才会感到奇怪。

ProductsRepo中有逻辑,它将在其中查找Location的产品,如果它们不存在,它将返回Business的产品列表(用作默认值(。我能得出的唯一合理的结论是,这样做不知怎么搞砸了对象,考虑到它在反复测试不同场景后通过了单元测试,这很奇怪。

这个故事的寓意可能是对这样的事情做出单独的呼吁,而我一开始就应该这样做。

最新更新