6

I'm trying to debug the Antiforgery code in the ASP.NET Core framework.

I have built the code successfully using the instructions in https://github.com/dotnet/aspnetcore/blob/master/docs/BuildFromSource.md using the Debug configuration.

I have added the source code project to my test project and referenced it.

I've checked that the Configuration is set to Debug.

I've made sure that "Suppress JIT Optimizations" has been checked.

"Optimize Code" in Project Properties -> Build, is unchecked for both projects.

"Debug Info" dropdown has been set to "Full" in the Advanced Options (Under Build tab) for both projects.

However when I hit the breakpoint in DefaultAntiforgeryTokenSerializer.Serialize(), it gives me this error when viewing the method's local vars in the Watch window:

Cannot obtain value of local or argument as it is not available at this instruction pointer, possibly because it has been optimized away

enter image description here

How can I force the code to not be optimized?

I'm using Visual Studio Enterprise 2019 v16.8.3.

3
  • 1
    Create a file called Microsoft.AspNetCore.Antiforgery.ini and then put it into the output folder of Microsoft.AspNetCore.Antiforgery project which exists Microsoft.AspNetCore.Antiforgery.dll. Then, add these in that file: [.NET Framework Debugging Control] GenerateTrackingInfo=1 AllowOptimize=0. Also, create a file called AntiforgeryTest.ini for your test project and then do the same steps. The referenced document is here. Commented Dec 14, 2020 at 9:08
  • I think you can just open (ctrl-o) cs file you wan't to debug and it should work? Commented Dec 14, 2020 at 12:37
  • 1
    @onedevteam.com if it was that simple I wouldn't have typed up this question. Commented Dec 14, 2020 at 23:10

2 Answers 2

3

After adding the Antiforgery source code project to my solution, rather than add a project reference to the .csproj file, I had to reference the dll in C:\aspnetcore\artifacts\bin\Microsoft.AspNetCore.Antiforgery\Debug\net5.0.

I can now view the values of local vars in the Watch window.

Sign up to request clarification or add additional context in comments.

4 Comments

Full correct and maintained instructions by Microsoft at learn.microsoft.com/en-us/aspnet/core/test/…
@RickAndMSFT that way is slow as it involves downloading the source code during debug. The way I've discussed in the article works well since the code is part of the VS solution itself.
> slow as it involves downloading the source code during debug But it's a slow one time hit. Once the source is cached, subsequent debug sessions are much faster.
@RickAndMSFT the same can be said about installing the source code, but with the added benefit of super fast debugging, being able to see the values of all variables, and being able to debug line by line (sometimes lines are skipped due to optimizations if you use the symbols server). Also not sure how you can go about opening a specific file if you want to put a breakpoint in there, that's much easier to do if you have the source code locally installed.
1

Full instructions at https://github.com/dotnet/AspNetCore.Docs/issues/4155

  • Un-check Enable Just My Code in Tools -> Options -> Debugging -> General
  • Enable Microsoft Symbol Servers in Tool -> Options -> Debugging -> Symbols

Full maintained instructions by Microsoft at https://learn.microsoft.com/en-us/aspnet/core/test/debug-aspnetcore-source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.