Maverick (Shaun)
Maverick (Shaun)
CC#
Created by Maverick (Shaun) on 2/11/2024 in #help
Invoke StateHasChanged() only after asynchronous operation has completed
Ended up taking an alternative approach by navigating to the same page (essentially a reload) using NavigationManager with a forced refresh - thanks for the messages folks
20 replies
CC#
Created by Maverick (Shaun) on 2/11/2024 in #help
Invoke StateHasChanged() only after asynchronous operation has completed
I don't - surely any attempt to reproduce in TryMud would be worthless as I can't replicate EF Core functionality there?
20 replies
CC#
Created by Maverick (Shaun) on 2/11/2024 in #help
Invoke StateHasChanged() only after asynchronous operation has completed
I guess a lack of behaviour would be a better description - nothing changes until I manually refresh the screen. I suppose there's a chance that StateHasChanged isn't actually refreshing the page as I'd expect, but it works everywhere else so I reckon my assumption that StateHasChanged is being invoked before the database is actually updated is more likely
20 replies
CC#
Created by Maverick (Shaun) on 2/11/2024 in #help
Invoke StateHasChanged() only after asynchronous operation has completed
I hadn't noticed I left Blazor out of the title/description, whoops - never done that before
20 replies
CC#
Created by Maverick (Shaun) on 2/11/2024 in #help
Invoke StateHasChanged() only after asynchronous operation has completed
I see. However, I am not seeing a change in behaviour when switching the method to async Task, when calling it with:
<MudButton Variant="Variant.Filled" Color="Color.Info" FullWidth="true" Class="my-4"
OnClick="@PublishDrawWithConfirmation">
Publish Draw
</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Info" FullWidth="true" Class="my-4"
OnClick="@PublishDrawWithConfirmation">
Publish Draw
</MudButton>
As far as I know, Blazor will detect that the method is asynchronous and handle it as such in the event handler above. I'm still not quite sure how awaiting PublishDrawWithConfirmation will affect the call to StateHasChanged within that method, but that's probably due to my lack of experience/in-depth understanding of asynchronicity
20 replies
CC#
Created by Maverick (Shaun) on 2/11/2024 in #help
Invoke StateHasChanged() only after asynchronous operation has completed
What difference would that make? Not disputing it, just not following how that would affect things logically. The call to StateHasChanged is happening inside of this function, so surely just changing the return type wouldn't make a difference? if I move the call to StateHasChanged outside of the function and then await the function and call StateHasChanged, wouldn't I just get the same behaviour as I currently am?
20 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
Made the change, tested that existing functionality was behaving as before and checked if the code that was causing issues with concurrency is now working - everything seems fine, very simple change and can't see any problems yet. Awesome stuff :goodthinking:
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
Awesome, I'll have a go at it - it sounds simple enough and will give me a bit more flexibility at the very least. Thanks for the help
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
Or would it be better to actually create a new context inside of each CRUD method, rather than passing it in?
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
I assume the change to my services (e.g.):
public class DrawService(ApplicationDbContext context, IHttpContextAccessor httpContextAccessor) : IDrawService
{
/* Create Operations */
public async Task AddNewDraw(DrawModel drawModel)
{
context.Draws.Add(drawModel);
await context.SaveChangesAsync();
}
........
public class DrawService(ApplicationDbContext context, IHttpContextAccessor httpContextAccessor) : IDrawService
{
/* Create Operations */
public async Task AddNewDraw(DrawModel drawModel)
{
context.Draws.Add(drawModel);
await context.SaveChangesAsync();
}
........
Would be as simple as just passing the context to the CRUD methods rather than having it in the service constructor?
public class DrawService(IHttpContextAccessor httpContextAccessor) : IDrawService
{
/* Create Operations */
public async Task AddNewDraw(ApplicationDbContext context, DrawModel drawModel)
{
context.Draws.Add(drawModel);
await context.SaveChangesAsync();
}
public class DrawService(IHttpContextAccessor httpContextAccessor) : IDrawService
{
/* Create Operations */
public async Task AddNewDraw(ApplicationDbContext context, DrawModel drawModel)
{
context.Draws.Add(drawModel);
await context.SaveChangesAsync();
}
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
Wow, that' s a lot simpler than I was expecting. I was having a skim read of some StackOverflow threads covering this topic and there seemed to be a lot of try-catch stuff bloating the code... not sure what they were doing to end up with that, my first thought was "that isn't very DRY". Seems it's definitely worthwhile then, I'd of course like to use best practices wherever I can as long as it doesn't add a ton of complexity
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
If it's pretty straightforward, maybe I'll look further into it then - my main concern was that I'd have to rewrite a chunk of logic
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
Just trying to balance between best practices/ease of development/simplicity due to the time constraints. Cutting a few corners where acceptable
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
How much of a problem is that likely to be? This is for a university project and won't actually be running for too long at once. I'm assuming that'd be more of an issue for an application that's running for long periods of time?
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
I've had a think about it, and I think this is the only instance in my application where this problem will occur. Since I'm only needing access to a couple of strings (Title and Description of the relevant model), I've decided just to get the object in the parent component and then pass the Title and Description to the child components directly, avoiding concurrent calls to dbcontext. I think this maybe breaks some "rules" regarding security, but since these strings don't contain any sensitive information, I think it's probably okay. Would you agree? Avoids adding a layer of complexity to the project and gets the same job done
21 replies
CC#
Created by Maverick (Shaun) on 2/10/2024 in #help
Multiple Blazor components accessing database via single DbContext at same time
I'm seeing some recommendations of using a DbContextFactory - would that be the best solution?
21 replies
CC#
Created by Maverick (Shaun) on 2/5/2024 in #help
Blazor Web App (.NET 8) - Logout functionality with IdentityCore
That worked, thank you. Using:
public static void MapAccountServices(this IEndpointRouteBuilder app)
{
app
.MapGet("/Logout", async (HttpContext context, string returnUrl = "/") =>
{
await context.SignOutAsync(IdentityConstants.ApplicationScheme);
context.Response.Redirect(returnUrl);
})
.RequireAuthorization();
}
public static void MapAccountServices(this IEndpointRouteBuilder app)
{
app
.MapGet("/Logout", async (HttpContext context, string returnUrl = "/") =>
{
await context.SignOutAsync(IdentityConstants.ApplicationScheme);
context.Response.Redirect(returnUrl);
})
.RequireAuthorization();
}
and then navigating to the Logout route while forcing a refresh with: NavigationManager.NavigateTo("/Logout", true); Thanks for the help!
9 replies
CC#
Created by Maverick (Shaun) on 2/5/2024 in #help
Blazor Web App (.NET 8) - Logout functionality with IdentityCore
I see - thanks for the tip, I'll have a go at implementing it and see if I can get it working
9 replies
CC#
Created by Maverick (Shaun) on 2/5/2024 in #help
Blazor Web App (.NET 8) - Logout functionality with IdentityCore
I see you're using HttpContext there, but isn't that unavailable when using interactive server?
9 replies
CC#
Created by Maverick (Shaun) on 2/4/2024 in #help
Using IdentityCore with InteractiveServer rendermode (Blazor .NET 8)
(get back soon, watching Band of Brothers)
11 replies