Daryl
Daryl
CC#
Created by Daryl on 2/12/2024 in #help
Blazor Pages rendering before async methods finish, breaking the page?
The below code is crashing my app because applicationUser is null when it's referenced in the HTML. This is a common issue I keep facing, how are you meant to handle it?
protected override async Task OnInitializedAsync()
{
while(userId == null)
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
userId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
}

transactions = DataService.GetAllTransactions(userId);
mostExpensiveTransactions = transactions.OrderByDescending(t => t.Amount).Take(5).ToList();

while(applicationUser == null)
{
applicationUser = await UserManager.FindByIdAsync(userId);
}
}
protected override async Task OnInitializedAsync()
{
while(userId == null)
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
userId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
}

transactions = DataService.GetAllTransactions(userId);
mostExpensiveTransactions = transactions.OrderByDescending(t => t.Amount).Take(5).ToList();

while(applicationUser == null)
{
applicationUser = await UserManager.FindByIdAsync(userId);
}
}
I thought the while loop would ensure that it can never continue but I guess it continues because it's returning a Task? ChatGPT suggests I added a Delay of 200ms after the command but it doesn't really feel like it would guarantee the issue is solved. Thanks!
153 replies
CC#
Created by Daryl on 6/6/2023 in #help
❔ httpClient.PostAsync() - Program exits and never hits my breakpoint after?
7 replies
CC#
Created by Daryl on 5/30/2023 in #help
✅ Managing JSON which varies in size and/or structure?
I'm working on two hobby projects at the moment, both are meant to accept json input but I'm struggling because C# seems quite strict on datatypes and I'm a noob. In one project the JSON I return is massive and seems like it could be broken into multiple classes. In the second project, the JSON structure seems to vary depending on the data being sent. What's the best way to tackle these sorts of things? I appreciate there is a way to copy JSON into Visual Studio it can generate the class for you but I don't want to be tied to Visual Studio and it's still a lot of admin work. For the varying JSON I captured about 100 entries and put them in a text file but I'm not even sure now how to compare them against each other to see if they're the same structure? Also apparently System.Text doesn't support the "dynamic" data type but Newtonsoft.Json is a thing of the past now? Any advice or tips would be great, thank you!
84 replies
CC#
Created by Daryl on 4/9/2023 in #help
❔ Webhook Package for Microsoft.AspNetCore?
7 replies
CC#
Created by Daryl on 9/10/2022 in #help
Method works when dependent Method never called?
I am following a Pluralsight tutorial on ASP.NET Fundamentals and we've just created this hard-coded data repository to work from:
public class MockPieRepository : IPieRepository
{
private readonly ICategoryRepository _categoryRepository = new MockCategoryRepository();

public IEnumerable<Pie> AllPies =>
new List<Pie>
{
new Pie {PieId = 1, etc},
new Pie {PieId = 2, etc}
};

public IEnumerable<Pie> PiesOfTheWeek
{
get
{
return AllPies.Where(p => p.IsPieOfTheWeek);
}
}
}
public class MockPieRepository : IPieRepository
{
private readonly ICategoryRepository _categoryRepository = new MockCategoryRepository();

public IEnumerable<Pie> AllPies =>
new List<Pie>
{
new Pie {PieId = 1, etc},
new Pie {PieId = 2, etc}
};

public IEnumerable<Pie> PiesOfTheWeek
{
get
{
return AllPies.Where(p => p.IsPieOfTheWeek);
}
}
}
I understand what both of these methods do but I don't understand how the PiesOfTheWeek method would currently work as the AllPies has never been called and not called in any constructor? I would expect PiesOfTheWeek to return Null when it tries it's Where statement. Also, shouldn't AllPies have () when it's used in PiesOfTheWeek? Thank you in advance!
150 replies