C
C#3mo ago
xyrile

Why does using `await` EF core execute command twice

I am using Blazor server app and add EF Core
public class UserAccountService(AppDBContext context) : ControllerBase, IUserAccountService
{
private readonly AppDBContext _context = context;
[HttpGet]
public async Task<IEnumerable<UserAccountModel>> GetAccounts()
{
return await _context.Accounts.ToListAsync();
}
}
public class UserAccountService(AppDBContext context) : ControllerBase, IUserAccountService
{
private readonly AppDBContext _context = context;
[HttpGet]
public async Task<IEnumerable<UserAccountModel>> GetAccounts()
{
return await _context.Accounts.ToListAsync();
}
}
Dependency
builder.Services.AddScoped<IUserAccountService, UserAccountService>();
builder.Services.AddScoped<IUserAccountService, UserAccountService>();
Here is the code where query executes twice
protected override async Task OnInitializedAsync()
{
var response = await UserAccountService.GetAccounts();
if (response != null)
{
accounts = response.ToList();
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(accounts, Newtonsoft.Json.Formatting.Indented));
}
else
{

}

}
protected override async Task OnInitializedAsync()
{
var response = await UserAccountService.GetAccounts();
if (response != null)
{
accounts = response.ToList();
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(accounts, Newtonsoft.Json.Formatting.Indented));
}
else
{

}

}
10 Replies
Jimmacle
Jimmacle3mo ago
how do you know that it's executing twice and that that's the reason?
xyrile
xyrile3mo ago
i see it in the console
Jimmacle
Jimmacle3mo ago
have you set a breakpoint on that call to make sure you aren't just calling it twice? also, i'd just make that service return a list to avoid the double ToList or just don't tolist it, i don't think that's a requirement for json serialization
xyrile
xyrile3mo ago
No description
xyrile
xyrile3mo ago
this behaviour only occurs in OnInitializedAsync()
D.Mentia
D.Mentia3mo ago
Stack Overflow
Why are Blazor lifecycle methods getting executed twice?
So with a release of asp.net core 3.0 and blazor 1.0 I started doing some actual work with blazor. When splitting Blazor component code into code behind I am using the following public class Logout...
D.Mentia
D.Mentia3mo ago
tldr, prerendering, be careful about which methods you put your logic in
D.Mentia
D.Mentia3mo ago
To prevent developer code in OnInitializedAsync from running twice when prerendering, see the Stateful reconnection after prerendering section. The content in the section focuses on Blazor Web Apps and stateful SignalR reconnection. To preserve state during the execution of initialization code while prerendering, see Prerender ASP.NET Core Razor components.
but tbh I thought it was that it renders once from the server, and then once for the client, even on blazor server
xyrile
xyrile3mo ago
yep this one fix my problem by changting this
<component type="typeof(App)" render-mode="ServerPrerendered" />
<component type="typeof(App)" render-mode="ServerPrerendered" />
to this
<component type="typeof(App)" render-mode="Server" />
<component type="typeof(App)" render-mode="Server" />