Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Razor View NullReferenceException Stacktraces are very unhelpful #1757

Closed
strich opened this issue Nov 7, 2017 · 16 comments
Closed

Razor View NullReferenceException Stacktraces are very unhelpful #1757

strich opened this issue Nov 7, 2017 · 16 comments
Assignees
Labels

Comments

@strich
Copy link

strich commented Nov 7, 2017

This is based upon an issue I originally raised over on the EFCore repo: dotnet/efcore#10221 (comment)

The following line of code had a nullref exception in it that caused the view to fail to present me with a stacktrace view:

@Model.Logins.Count [<a asp-controller="Manage" asp-action="ExternalLogins">Manage</a>]

The Logins object is null.

Stacktrace:

fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HL955T12F4U6", Request id "0HL955T12F4U6:0000000E": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
   at AspNetCore._Views_Manage_Index_cshtml.<ExecuteAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.<RenderPageCoreAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.<RenderPageAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.Mvc.Razor.RazorView.<RenderAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.<ExecuteAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.<ExecuteAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.ViewResult.<ExecuteResultAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResultFilterAsync>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.<Invoke>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.<ProcessRequestsAsync>d__2.MoveNext()

This is a debug enabled build with developer exceptions turned on. This stacktrace was incredibly unhelpful in directing me to the root cause. In my original issue someone noted that there is an open issue to improve the readability of stacktraces (dotnet/extensions#274), but AFAIK it doesn't help actually point to that line of code in question.

@NTaylorMullen
Copy link
Member

@strich what version of Razor/Mvc etc. are you using? I can't reproduce locally using 2.0.

@pranavkm
Copy link
Contributor

pranavkm commented Nov 7, 2017

Does you add the DeveloperExceptionPage (app.UseDeveloperExceptionPage()) in your startup? It should pretty print it highlighting the line in cshtml.

@strich
Copy link
Author

strich commented Nov 7, 2017

@NTaylorMullen I'm on the latest .NET Core 2.0 stuff. What exactly are you trying to repo? Just to be clear - This isn't an issue with the standard template code as I've modified it. This is a issue to raise the lack of helpful stacktracing on trivial nullref's in Razor views.

@pranavkm Ah you might be confused - What I've pasted above is from a server log as I was trying to dig deeper. The very same stacktrace is pretty printed to the client browser. But no line numbers or code line previews. Just that very unhelpful trace.

@benaadams
Copy link
Contributor

Error is in Views/Manage/Index.cshtml you'd need to also copy the .pdbs to the server to get line numbers

@strich
Copy link
Author

strich commented Nov 8, 2017

@benaadams I have confirmed that the pdbs are indeed copied as part of a normal debug publish and the files are sitting there next to the dlls on the server.

@benaadams
Copy link
Contributor

including the NameOfProject.PrecompiledViews.pdb that's created in publish step? Assuming you are using view precompilation e.g. have a NameOfProject.PrecompiledViews.dll

@strich
Copy link
Author

strich commented Nov 8, 2017

@benaadams yep:
image

@benaadams
Copy link
Contributor

Odd no line numbers then... Full framework or .NET Core (2.0 or 1.x)?

Which version of ASP.NET Core (1.x, 2.0, etc)

Think there was an issue with pdbs on Full framework that should have been fixed with ASP.NET Core 2.0 aspnet/Mvc#6442

@strich
Copy link
Author

strich commented Nov 8, 2017

Everything is .NET Core 2.0.
image

@rynowak
Copy link
Member

rynowak commented Dec 17, 2017

@pranavkm can you take a look and see if anything can be done?

@rynowak rynowak added this to Design in Razor 2.1.0 Dec 17, 2017
@pranavkm
Copy link
Contributor

Looks like reading full pdbs, which is what we default to if on a Windows machine with VS \ pdb writer components installed, was broken in 2.0.0. Here's the tracking issue https://github.com/dotnet/corefx/issues/21079. For reasons I haven't dug in to, runtime compiled views have the right line mappings, but precompiled views do not, even though they're producing the same kind of pdb.

I'll see if they have any interest in porting the fix from 2.1.0 (which works fine) back to 2.0.0. Unfortunately, in the absence of a fix from CoreCLR and the missing feature in , there isn't much that can be done to fix this. The only workaround I could suggest is to run the application (dotnet run) as opposed to running the published outputs.

@pranavkm
Copy link
Contributor

Looks like there's already an issue tracking porting the fix for 2.0.x - https://github.com/dotnet/coreclr/issues/15254.

@strich
Copy link
Author

strich commented Dec 19, 2017

For myself, I'm happy to upgrade to v2.1 so I would consider this problem resolved pending confirmation.

@pranavkm
Copy link
Contributor

pranavkm commented Dec 19, 2017

@strich if you're feeling rather adventurous, nightly builds of the 2.2 SDK \ 2.1 runtime are available here - https://github.com/dotnet/cli#installers-and-binaries. 2.2.0-preview1-007796 is the sdk version our builds are currently using.

@rynowak
Copy link
Member

rynowak commented Dec 28, 2017

So it sounds to me like this is a known issue dotnet/coreclr#15254 - and this will start working better in 2.1 - any reason to keep this bug around?

@pranavkm
Copy link
Contributor

Nope. I added a work item to get some tests around this covered in MusicStore so we can catch these regressions early. In addition, I'm going to work on getting aspnet/Mvc#7006 in for 2.1.0 so the pdbs we produce are in line with what the app produces.

@rynowak rynowak removed this from Design in Razor 2.1.0 Jan 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants