No1*_*ver 5 c# asp.net asp.net-membership asp.net-identity
我使用此代码(其中上下文是OAuthGrantCustomExtensionContext):
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser appUser1 = await userManager.FindByIdAsync("My user id"); // 31 seconds to execute
ApplicationUser appUser2 = await userManager.FindByIdAsync("My user id"); // 1 milisecond to execute
Run Code Online (Sandbox Code Playgroud)
第一次执行FindByIdAsync函数需要31秒.在第二次执行时,它只需要<1 MS.我还通过运行"SELECT ... WHERE ..."查询来检查我的数据库有多快 - 而且速度非常快 - <1 MS.
我也在"输出窗口"里面的输出中注意到:
抛出异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException':System.Data.dll中的'System.Data.SqlClient.SqlException'异常抛出:System中的'System.Data.SqlClient.SqlException'抛出.Data.dll异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException':抛出System.Data.dll Exception中的'System.Data.SqlClient.SqlException':'System.Data.SqlClient.抛出System.Data.dll Exception中的SqlException:System.Data.dll中的'System.Data.SqlClient.SqlException'抛出异常:抛出System.Data.dll Exception中的'System.Data.SqlClient.SqlException':'System.抛出System.Data.dll Exception中的Data.SqlClient.SqlException:System.Data.dll中的'System.Data.SqlClient.SqlException'抛出异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException'异常:System.Data.dll中的'System.Data.SqlClient.SqlException'线程0x1598已退出,代码为0(0x0).抛出异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException':System.Data.dll中的'System.Data.SqlClient.SqlException'异常抛出:System中的'System.Data.SqlClient.SqlException'抛出.Data.dll异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException':抛出System.Data.dll Exception中的'System.Data.SqlClient.SqlException':'System.Data.SqlClient.抛出System.Data.dll Exception中的SqlException:System.Data.dll中的'System.Data.SqlClient.SqlException'抛出异常:抛出System.Data.dll Exception中的'System.Data.SqlClient.SqlException':'System.抛出System.Data.dll Exception中的Data.SqlClient.SqlException:System.Data.dll中的'System.Data.SqlClient.SqlException'抛出异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException'异常:System.Data.dll Exception中的'System.Data.SqlClient.SqlException':EntityFramework.dll'iisexp中的'System.Data.Entity.Infrastructure.RetryLimitExceededException' ress.exe'(CLR v4.0.30319:/ LM/W3SVC/6/ROOT-1-131126169736320764):已加载'EntityFrameworkDynamicProxies-EntityFramework'.抛出异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException':System.Data.dll中的'System.Data.SqlClient.SqlException'异常抛出:System中的'System.Data.SqlClient.SqlException'抛出.Data.dll异常:抛出System.Data.dll异常中的'System.Data.SqlClient.SqlException':抛出EntityFramework.dll异常中的'System.Data.Entity.Core.EntityCommandExecutionException':'System.Data.Entity.抛出EntityFramework.dll Exception中的Core.EntityCommandExecutionException:EntityFramework.dll中的'System.Data.Entity.Core.EntityCommandExecutionException'抛出异常:EntityFramework.dll'iisexpress.exe'中的'System.Data.Entity.Core.EntityCommandExecutionException'( CLR v4.0.30319:/ LM/W3SVC/6/ROOT-1-131126169736320764):已加载的'EntityFrameworkDynamicProxies-ObjectModelLayer'.'iisexpress.exe'(CLR v4.0.30319:/ LM/W3SVC/6/ROOT-1-131126169736320764):已加载'EntityFrameworkDynamicProxies-Microsoft.AspNet.Identity.EntityFramework'.
上面的输出是从我的机器执行此行的时间收集的:
ApplicationUser appUser1 = await userManager.FindByIdAsync("My user id");
Run Code Online (Sandbox Code Playgroud)
在其他代码行中,输出窗口为空.(这可以解释为什么他们这么快).
正如您所看到的,SqlException在此代码行中抛出了很多内容.在我看来,像一个"重试"机制,尝试agian和agian执行一些逻辑,但它失败了.我想知道这是否是为什么这么长时间?
我在寻找:
SqlException什么?也许我会解决这个问题,这段代码运行得更快.谢谢你的支持!
我找到了一种有用的方法来捕获第一次机会异常.使用此代码:
AppDomain.CurrentDomain.FirstChanceException +=
(object source, FirstChanceExceptionEventArgs e) =>
{
Console.WriteLine("FirstChanceException event raised in {0}: {1}", AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
};
Run Code Online (Sandbox Code Playgroud)
所以,我运行代码并收集抛出的异常.得到以下例外18次:
服务器主体"XXX"无法访问当前安全上下文下的数据库"master".服务器'XXX.net'上的数据库'master'目前不可用.请稍后重试连接.如果问题仍然存在,请联系客户支持,并为其提供会话跟踪ID"XXXXXXX-68E9-4FDD-9E3D-92BE27438835".用户'XXX'登录失败.
我的用户实际上不允许访问"主"表.所以,这是有道理的.我将我的数据库连接字符串更改为具有访问"主"表的权限的另一个用户 - 现在它正在快速运行!(不到3秒).
在给出正确的权限后,我仍然在日志中得到此异常(7次):
无效的对象名称'dbo.EdmMetadata'.
问题:
FindByIdAsync需要访问主表???6 年后,我仍然在最新的 Identity 和 Entity-Framework 库中遇到类似的行为。第一次调用 userManager.Get[Something] 大约需要 5 秒;随后的调用时间为 3-10 毫秒。Identity 是否会在第一次调用时尝试检查/重新创建底层数据库?
| 归档时间: |
|
| 查看次数: |
366 次 |
| 最近记录: |