Jef*_*ner 12 c# asp.net-mvc nullreferenceexception owin asp.net-mvc-5
AccountController.cs安排AccountController上课.Login该类的方法失败了.特别,
var result = await SignInManager.PasswordSignInAsync(
model.Email, model.Password, model.RememberMe, shouldLockout: true);
Run Code Online (Sandbox Code Playgroud)
在Login方法内投掷System.NullReferenceException.
我已经验证了model.Email,model.Password并且model.RememberMe不是空的.下一步是深入挖掘SignInManager,这是一个AccountController类型的对象ApplicationSignInManager.从上面一行调用的get访问器SignInManager是
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ??
HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
// private set...
}
Run Code Online (Sandbox Code Playgroud)
我改写为
public ApplicationSignInManager SignInManager
{
get
{
var c = HttpContext.GetOwinContext();
var m = c.Get<ApplicationSignInManager>();
return _signInManager ?? m;
}
// private set...
}
Run Code Online (Sandbox Code Playgroud)
调试并确保get访问者没有返回null.它没有返回null.这告诉我里面需要的东西PasswordSignInAsync是null.
我查看了PasswordSignInAsync它的来源,它在防止空引用错误方面做得非常好.我还深入研究了所谓的方法PasswordSignInAsync.我根本看不出什么可能引发空引用错误.
错误的堆栈跟踪非常神秘,我没有看到任何有用的东西.但为了完整起见,这里是:
[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +2112
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +123
Microsoft.AspNet.Identity.Owin.<SignInAsync>d__2.MoveNext() +408
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
Microsoft.AspNet.Identity.CultureAwaiter.GetResult() +63
Microsoft.AspNet.Identity.Owin.<SignInOrTwoFactor>d__23.MoveNext() +2055
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +68
Microsoft.AspNet.Identity.Owin.<PasswordSignInAsync>d__29.MoveNext() +1828
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
ApexRemingtonExternal.Controllers.<Login>d__2.MoveNext() in c:\Users\me\sc\project\Controllers\AccountController.cs:91
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +61
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +114
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +66
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651688
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Run Code Online (Sandbox Code Playgroud)
我很感激任何可能导致这种情况的帮助或指导NullReferenceException.如果我可以添加更多细节(或删除一些(哈哈!)),请告诉我.这是我和我公司的第一个MVC项目,所以我确信我已经破坏了一些东西.
编辑
我应该补充一点,我从项目中剥离了实体框架,转而使用LINQ to SQL,这是我们在所有其他项目中所依赖的.这意味着我必须实现自己的IUserStore版本,IRoleStore等.
1_b*_*bug 11
有时(特别是如果你更新Microsoft.AspNet.Identity.Owin)你可能忘了添加行
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
对方法public void ConfigureAuth(IAppBuilder app)在App_Start\Startup.Auth.cs
包括这应该一切工作之后.
通常我的习惯,问题与症状无关.当然SignInManager.PasswordSignInAsync不会破碎.这是我的代码被打破了.这使得这个问题没有实际意义,这让我想要删除这个问题.
然而,这里有一个值得学习的教训.就我而言,它是as关键字.放弃了EF,如编辑中所提到的,我不得不提供几个接口的实现.我必须提供的一种方法是GetRolesAsync返回Task<IList<String>>(在我的情况下).我从来没有处理过IList.结果as是我需要的.但是我试图把一个转换IQueryable为一个IList,这是行不通的.结果:as简单地返回null.谢谢,as.那很有用.
是啊.我可能会删除这个问题.
| 归档时间: |
|
| 查看次数: |
7367 次 |
| 最近记录: |