1

I've decided to pick up blazor as recently enjoyed last UWP project and quite like entiry framework to boot.

I've a very basic component, littlally uses a service using an injected IDbContextFactory<>, and EF core.

The app runs fine but I'm noticing an exponential growth in "requests being sent" or at least break-points being hit multiple times on page load.

I'll set a break point in the page / service.cs and the break point is his multiple times. I extend the EF query to include extra collection and that's another 2/3/4 hits on the break point.

I embed a component and that's yet more hits to the break point.

In _Host.cshtml i've set "render-mode" to Server and it's still hitting multiple times.

I'm just curious is this is expected behaviour because of how blazor pre-rendering works along with signalR or if there's an issue with my very basic code.

I've checked with the blazor example app and that hits breakpoints multiple times too.

Service:

public class PostService
    {
        private readonly IDbContextFactory<PostContext> _db;

        public PostService(IDbContextFactory<PostContext> dBContext)
        {
            _db = dBContext;
        }

 public Board GetPost(string? id)
        {
            using (var db = _db.CreateDbContext())
            {
                var posts = db.Posts.FirstOrDefault(p => string.Equals(p.Id,id));
             }
         }
Program.CS:
builder.Services.AddDbContextFactory<PostContext>();

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<PostService>();

Post.razor
@page "/post/{PostId?}"
@inject NavigationManager navMan
@inject PostService postService


@code {
    [Parameter]
    public string? PostId{ get; set; }

    Post? CurrentPost=> postService.GetPost(PostId);
    
}

All very basic I'm just not used to blazor at all lol

H H
  • 263,252
  • 30
  • 330
  • 514

2 Answers2

1
 Post? CurrentPost=> postService.GetPost(PostId);

Could become very expensive. You now query everytime you use CurrentPost.

Make CurrentPost a property or a field and load it in OnParametersSet[Async]

H H
  • 263,252
  • 30
  • 330
  • 514
  • thanks I've literally just worked that out! and was coming back to update! Thank you though. I really wasn't expecting that behaviour at all, but I'm not used to websockets & blazor. – ConfusedBlazorNewDev Jul 14 '22 at 12:19
  • To be honest this is more about how properties work. – H H Jul 14 '22 at 12:32
0

You code is mighty expensive on Db access. If I read your code correctly: if CurrentPost has 10 properties you are displaying in the Form, you're going to make 10 database calls and create 10 contexts.

Here's some code snippets based on what you've shown in your question that resolve the issue.

I've:

  1. Made the Db access async.
  2. Loaded the Post ONCE in OnInitializedAsync.

Your service:

public async ValueTask<Post> GetPostAsync(string? id)
        {
            using var db = _db.CreateDbContext();
            var post = await db.Posts.FirstOrDefaultAsync(p => string.Equals(p.Id,id));
            // need to deal with nulls here properly
            return post;
             }
         }
private Post? currentPost;

protected async Task OnInitializedAsync()
{
   currentPost = await PostService.GetPostAsync(postId);
}
MrC aka Shaun Curtis
  • 19,075
  • 3
  • 13
  • 31
  • `GetPostAsync` may be called more than once. `GetPostAsync` should query the database only once (depending on the desgin of other code of which I'm not aware...), and cache the result (in the service) for further requests. Voted you up... – enet Jul 15 '22 at 00:20
  • I always have my data in view services, and nearly put that in the answer but with so little code shied away from it. I try not to preach too much in answers!!! For those reading this answer later, look at my answer to this question which demonstrates data in services as suggested by @Enet - https://stackoverflow.com/questions/70713133/blazor-child-component-rendering-and-parameter-changes – MrC aka Shaun Curtis Jul 15 '22 at 07:43