【问题标题】:"ArgumentNullException: Value cannot be null. Arg_ParamName_Name" out of nowhere in ASP.NET Core“ArgumentNullException:值不能为空。Arg_ParamName_Name”在 ASP.NET Core 中无处不在
【发布时间】:2021-08-11 08:57:24
【问题描述】:

我正在为我正在创建的 Angular 应用程序开发一个 Swagger API,它充当 Riot API 的代理。 我上周最后一次工作没有任何问题。然而,现在我的数据初始化程序在尝试启动它时抛出了一个空异常。

首先,我重新启动了 Visual Studio,然后以管理员权限重新启动,最后重新启动了笔记本电脑,没有骰子。 然后检查是否是我所做的某个更改,回滚到几周前的随机提交。同样的例外。

所以,这个异常似乎不知从何而来。直到今天才看到它发生,我无法让它消失。

异常截图:

数据初始化器(违规行用注释标记,在底部):

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();
            
        }
    }
}

老实说,我不明白代码应该归咎于此。当然,它写得不是很好,而且 Wait() 很臭,但是一切正常,直到它决定不这样做。 据我所知,网上类似的例外情况与这种情况无关。

编辑:忘了提,我还徒劳地更新了 Visual Studio 和所有 NuGet 包。

【问题讨论】:

  • 只是一个提示...看看这个关于如何在应用程序启动之前运行异步代码的答案。那 .Wait 不是最佳的(正如您所指出的)。 stackoverflow.com/a/63323207/1141089

标签: c# asp.net asp.net-core .net-core entity-framework-core


【解决方案1】:

经过更多调试后,发现其中一个 PinnedSummoners 项返回 null,从而导致问题。 根本原因是其中一位召唤师更改了他们的名字,而我未能检查空值。让我失望的是模棱两可的异常消息。

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    相关资源
    最近更新 更多