视图中出现获取InvalidOperationException.集合已修改错误



我有一个.Net核心web应用程序。我的代码在调试模式下运行得很好,在服务器上,我没有得到任何可见的错误。但这个错误被反复记录在我的记录器中。

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
at AspNetCore.Views_Home__ItemsList.ExecuteAsync() in [project path]ViewsHome_ItemsList.cshtml:line 56

我不明白为什么会发生这种情况,我不会更改视图中的列表。

IndexDto

public class IndexDto
{
public List<SliderListDto> Sliders { get; set; }
public List<ItemDto> Featured { get; set; }
public ResourceList<ItemDto> Items { get; set; }        
}

资源列表

public class ResourceList<T>
{
public ResourceList() { }
public ResourceList(List<T> items, bool hasMore = false)
{
Items = items;
HasMore = hasMore;
}
public List<T> Items { get; set; }
public bool HasMore { get; set; }
public List<Link> Links { get; set; } = new List<Link>();
}

Index.cshtml

@model IndexDto
<div class="home">
@await Html.PartialAsync("_Sliders", Model.Sliders)
@await Html.PartialAsync("_Featured", Model.Featured)
@await Html.PartialAsync("_ItemsList", Model.Items)
</div>

_ItemsList.cs.html

@if (Model.Items.Any())
{
@*some html here*@
@foreach (var item in Model.Items)
{
@*some html here*@
@if (item.IsOnline)
{
@*some html here*@
}
@*some html here*@
@if (item.OnlineUsersCount > 0)
{
@*some html here*@
}           
}

@if (Model.HasMore)
{
var nextLink = Model.Links.First(l => l.Rel == "next"); //This is where the error happens
if (nextLink != null)
{
@*some html here*@
}
}
@*some html here*@
}

我对Links列表进行修改的唯一一次是在控制器中使用此方法:

public class HomeController : Controller
{
public async Task<IActionResult> Index()
{
var model = await _queryManager.GeMainPage();
SetResourceLinks(model);
return View(model);
}
private void SetResourceLinks(IndexDto result)
{
if (result.Items.HasMore)
{
result.Items.Links.Add(new Link(
"next",
Url.Action(nameof(ItemsController.GetItems), "Items", new { pageNumber = 2}, Request.Scheme),
HttpMethods.Get));
}
}
}

我正在这个应用程序上使用负载平衡。这与此有关吗?

更新

我在_queryManager.GeMainPage((方法中找到了问题的根源。

public async Task<HeyatIndexDto> GetMainPage()
{
var model = await _redisCache.GetOrCreateAsync(CacheKeys.IndexModel, entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5);
return GetMainPageFromDatabase();
});
return model;
}
private async Task<IndexDto> GetMainPageFromDatabase()
{
//....
}

我把数据作为一个对象存储在Redis中,当我检索它时,它并没有创建新的对象,也没有添加到上一个数组的链接。我更改了方法,将数据存储为字符串,然后对其进行反序列化。问题解决了。

public async Task<HeyatIndexDto> GetHeyatMainPage()
{
var serializedData = await _cache.GetOrCreateAsync(CacheKeys.HeyatIndexModel, entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5);
return GetHeyatMainPageFromDatabase();
});
var model = JsonSerializer.Deserialize<HeyatIndexDto>(serializedData);
return model;
}
private async Task<string> GetHeyatMainPageFromDatabase()
{
//....
}

我在项目的另一层找到了问题的根源。

在_queryManager.GeMainPage((方法中,我将数据作为对象存储在Redis中,当我检索到它时,它并没有创建新的对象,也没有添加到上一个数组的链接。我更改了方法,将数据存储为字符串,然后对其进行反序列化。问题解决了。

我在原来的帖子中添加了一个包含GeMainPage代码的更新。

最新更新