Home > Software design >  noob to blazor and confused about page life cycle and an ever increase number of requests
noob to blazor and confused about page life cycle and an ever increase number of requests

Time:07-15

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.

thanks.

Service:

    {
        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

CodePudding user response:

 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]

  • Related