ASP.NET Core Identity在使用PostgreSQL时重定向出现问题?

huangapple go评论64阅读模式
英文:

ASP.NET Core Identity redirection breaks when using postgres?

问题

我有一个小的Blazor Server应用程序,通过EntityFramework 7运行Asp.net Core Identity。我之前一直在使用SQL Server,直到今天我尝试切换托管提供程序。当我尝试使用Entity Framework的NpgSql数据连接器来执行完全相同的操作时,应用程序表现得非常奇怪。当我登录时,结果是成功的,但然后从MVC重定向回Blazor应用程序时超时。如果我然后刷新页面,我仍然在登录页面上,但现在已经登录,可以正常使用应用程序。这可能是我的问题是什么?

我想分享更多的代码,但唯一的区别是从以下内容切换:

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("<string>"));

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql("<string>"));

我的第一个想法是EfCore的某些部分仍然试图使用SQL Server数据提供程序,但为什么刷新后我能正确登录呢?

编辑:刚刚发现另一个症状-显然,所有Blazor功能(状态更改,从方法重定向)现在都无法正常工作。就像Blazor的server.js文件没有被加载一样-但它确实被正确加载,没有JS错误发生。感觉自己快要发疯了。

英文:

I've got a small Blazor Server app running with Asp.net Core Identity through EntityFramework 7. I had been using a SQL server, until today when I tried to switch hosting providers. When I try to do the exact same thing with the NpgSql data connector for Entity Framework, the application behaves very strangely. When I log in, the result is a success, but then the redirect from MVC back to the Blazor app times out. If I then refresh the page, I'm still on the Login page, but now signed in, and can use the application as normal. What could be my issue here?

I would share more of my code, but the only difference is going from

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("<string>"));

to

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql("<string>"));

My first thought was that there's some part of EfCore that's still trying to use Sql server data providers, but then why would I be correctly signed in on refresh?

EDIT: Just discovered another symptom- apparently, all blazor functions (state change, redirects from methods) are now nonfunctional. It's like if the blazor.server.js file isn't being loaded- but it is being loaded correctly, and no JS errors are happening. Feel like I'm going crazy here.

答案1

得分: 0

事实证明,我在程序的其他地方存在线程问题。我在这里使用了这段代码

currentUser = UserManager.FindByNameAsync(authState.User.Identity.Name).Result;

我在这里使用了.Result()而不是等待该方法,以为这会同步运行-我现在意识到这是不良实践,因为以这种方式使用.Result()会导致死锁。不知何故,当我使用Entity Framework的SQL Server数据提供程序时,这段代码是正常的-我猜测SQL Server的冷启动时间可能比我现在使用的Postgres服务器快了一点,因此该方法需要更长的时间-然后陷入了死锁。

我重构了代码以正确等待该方法,现在一切都正常工作。

英文:

EDIT

Turns out I was having a threading issue elsewhere in my program. I was using this code

currentUser = UserManager.FindByNameAsync(authState.User.Identity.Name).Result;

I had used .Result() here instead of awaiting the method in thinking this would run synchronously- I now realize this is bad practice, as using .Result() in this way is a deadlock waiting to happen. Somehow, this code was fine when using the SQL Server data provider for Entity Framework- My guess is that the cold start time for the SQL server was a bit faster than the Postgres server I'm now using, and because of this the method was taking a fraction of a second longer- then running into a deadlock.

I refactored the code to properly await the method, and now everything works perfectly.

huangapple
  • 本文由 发表于 2023年6月6日 06:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定