0

I'm developing a Swagger API which acts as a proxy for the Riot API, for an Angular app I'm creating. I last worked on it last week without any problems. Now, however, my data initializer throws a null exception when trying to start it.

At first, I restarted Visual Studio, then I restarted with admin privileges, and lastly restarted the laptop, no dice. Then checked if it was a certain change I made, rolled back to a random commit from a few weeks ago. Same exception.

So, this exception seemingly started appearing from nowhere. Never saw it happen until today, and I can't get it to go away.

Screenshot of the exception: Exception

The data initializer (The offending line is marked with a comment, at the bottom):

public class SummonerDataInitializer
{
    private readonly SummonerContext context;
    private readonly UserManager<User> userManager;
    private readonly SummonerRepository summonerRepository;

    public SummonerDataInitializer(SummonerContext context, UserManager<User> userManager, SummonerRepository summonerRepository)
    {
        this.context = context;
        this.userManager = userManager;
        this.summonerRepository = summonerRepository;
    }

    public async Task InitializeData()
    {
        await context.Database.EnsureDeletedAsync();
        if (await context.Database.EnsureCreatedAsync())
        {
            var dashboard = new Dashboard
            {
                PinnedSummoners = new List<Summoner>
                {
                    await summonerRepository.GetBy("euw", "Jhinstachio"),
                    await summonerRepository.GetBy("euw", "DracoZar"),
                    await summonerRepository.GetBy("euw", "TrueMrCrazy"),
                    await summonerRepository.GetBy("euw", "God of Lunar")
                },
                LiveGameItems = new List<LiveGameItem>
                {
                    new LiveGameItem
                    {
                        Region = "euw",
                        SummonerName = "Don Arts",
                    },
                    new LiveGameItem
                    {
                        Region = "euw",
                        SummonerName = "3250",
                    },
                    new LiveGameItem
                    {
                        Region = "euw",
                        SummonerName = "Cboi1",
                    },
                }
            };

            var user = new User("admin", "admin@admin.com") { EmailConfirmed = true };
            await userManager.CreateAsync(user, "P@ssword1");
            await userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "admin"));
            
            user = await context.Users.SingleAsync(u => u.Email == user.Email);
            user.Dashboard = dashboard; // Removing this line fixes the exception
            context.Users.Update(user);
            await context.SaveChangesAsync();
            
        }
    }
}

I don't see how the code can be to blame here to be honest. Sure, it isn't super well-written, and that Wait() is smelly, but everything worked until it decided not to. Similar exceptions online point to nothing relevant in this situation, as far as I've seen.

EDIT: Forgot to mention, I also updated Visual Studio and all NuGet packages in vain.

  • 1
    Just a tip...have a look at this answer on how to run async code before your app starts. That .Wait is not optimal (as you pointed out). https://stackoverflow.com/a/63323207/1141089 – JOSEFtw Aug 11 '21 at 09:15

1 Answers1

0

After some more debugging, it turns out that one of the PinnedSummoners items was returning null, causing the issue. The root cause is one of those summoners changed their name, and I failed to check for null values. What threw me off was the ambiguous exception message.