升级到EF 5.0.1/DbContextFactory后,网站未显示数据



我正在编写一个网络应用程序来提交视频进行投票。因为我遇到了线程和数据库的问题,我从核心3.0.1升级到了5.0.1

在我使用这些服务之前。AddDbContext,但不起作用。现在我使用这个:

services.AddDbContextFactory<LogicDbContext>(
dbContextOptions => dbContextOptions
.UseMySql(serverVersion: ServerVersion.AutoDetect(connectionString), connectionString: connectionString)
.EnableSensitiveDataLogging()
.EnableDetailedErrors());

我的前端:

@page "/VideoPage"
@using WebProject.Data
@using Google.Apis.YouTube.v3.Data
@using Microsoft.AspNetCore.Identity
@using Microsoft.EntityFrameworkCore.Diagnostics
@using System.Security.Claims
@using Microsoft.AspNetCore.Identity.EntityFrameworkCore
@using Microsoft.EntityFrameworkCore
@inject YoutubeApiService YoutubeApiService;
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
@inject VideoManager VideoManager;
@inject VoteManager VoteManager;
@inject ApplicationDbContext _identityContext
<h1>Videos</h1>
<div style="visibility: @(busy?"visible":"hidden")">
<text>
Working...
</text>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>Thumbnail</th>
<th>Title</th>
<th>Channel</th>
<th>Date</th>
<th>Submitter</th>
<th>Votes</th>
</tr>
</thead>
<tbody>
@foreach (var video in VideosList)
{
<tr>
<td><img src="@video.Thumbnail" alt="Thumbnail not found" width="200" /></td>
<td><a href="https://www.youtube.com/watch?v=@video.VideoId">@video.Title</a></td>
<td>@video.ChannelTitle</td>
<td>@video.PublicationDate?.ToString("dd.MM.yyyy")</td>
<td>@_identityContext.Users.FirstOrDefault(u => u.Id == video.Submitter)?.UserName</td>
<td>
@(VoteManager.GetVotesAsync(video).Result)
</td>
</tr>
}
</tbody>
</table>
@code
{
public List<Videos> VideosList = new List<Videos>();
public bool busy;
protected override async void OnInitialized()
{
await RefreshList();
}
public async Task RefreshList()
{
busy = true;
VideosList = await VideoManager.GetVideosAsync();
busy = false;
this.StateHasChanged();
}
}

VoteManager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebProject.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace WebProject.Data
{
public class VideoManager
{
private readonly IDbContextFactory<LogicDbContext> _contextFactory;
private readonly YoutubeApiService _youtubeApiService;
public VideoManager(IDbContextFactory<LogicDbContext> contextFactory, YoutubeApiService youtubeApiService)
{
_contextFactory = contextFactory;
_youtubeApiService = youtubeApiService;
}
public async Task<List<Videos>> GetVideosAsync()
{

var videos = new List<Videos>();
await using (var context = _contextFactory.CreateDbContext())
{
videos = await context.Videos.ToListAsync();
}
return videos;
}

}
}

在更改为新版本并使用工厂之前,代码可以获得视频列表并显示它们。

现在,在我进入页面之后;正在工作"显示消息,但不再发生任何事情。当在VS中调试时(我不使用VS代码(,它告诉我VideoList实际上已经填充。也忙设置为false。使用this.StateHasChanged();没有任何作用。有时整个网站都冻结了,我甚至无法点击链接。

有什么我没看到的吗?

有问题

<td>
@(VoteManager.GetVotesAsync(video).Result)
</td>

你不能在Blazor(Razor(代码中使用await,而.Result是罪魁祸首,它可能会阻塞主线程,导致死锁。这符合你的症状。

规则是在异步代码中始终避免.Result.Wait()

作为一种解决方案,当您获得视频时,只需.Include()即可获得投票。执行一个大查询也比许多小查询提供更好的性能。

相关内容

  • 没有找到相关文章

最新更新