Debug .NET Core source (Visual Studio 2019)

2019-05-31 06:41发布

I use Visual Studio 2019 and .NET Core 3 web application. I'd like to debug .NET Core source code, so when an error occurs I can dive into the code and examine it in more detail. As I couldn't find an exact description on how to do it (even on Microsoft documentation site), I'll try to summarize here what I know so far.

I have the following options in Visual Studio:

      1. Enable just my code
      2. Enable .NET framework source stepping
      3. Enable source server support
      4. Enable source link support

The first two options are mutually exclusive. I suppose I should select the second option, although I don't understand weather ".NET framework" in this options includes .NET Core?

Now as .NET Core is open source and available on GitHub, I don't understand 3rd vs 4th option. The 3rd should allow me to pull source from Microsoft Symbol Servers (do symbols include source?) while the 4th should allow me to pull source from GitHub? Do I need one or the other?

First, I put a breakpoint on CreateHostBuilder in Main method, select the 3rd option and run the application. After the breakpoint is hit I keep stepping into code (F11) until a message appears saying "GenericHostBuilderExtensions.cs is not found". It offers me to browse for it, but I have no idea where could I find it.

Let's try all over again, this time selecting the 4th option (and deselection the 3rd). Now, after the breakpoint is hit, VS offers me to download the before mentioned file by Source Link. A accept, but the result is the message "Source Link Error: Azure DevOps: Authentication failed for all accounts. Use 'File -> Account Settings...' to add a new account or refresh credentials." Well, I'm logged in with my Microsoft account, what else should I do?

I repeat it all again, this time selecting both 3rd and 4th options. Result is the same.

Now I select another Source Link option: "Fall back to Git Credential manager..." and start all over again. Same result.

Of course I extensively Googled and spent quite some time trying to find a solution, but I'm missing something. I'd appreciate help on what exactly the mentioned options do and how to debug by stepping into .NET Core source code.

1条回答
一夜七次
2楼-- · 2019-05-31 07:01

Maybe this issue can help solve your puzzle.

Just like what vancem describes in that issue, all you need to do for debugging .net core source is:

1.Uncheck the (Tools -> Options -> Debugging -> Just My Code) checkbox.

2.Ensure that the (Tools -> Options -> Debugging -> Symbol Settings -> Microsoft Symbol Servers) checkbox is set

3.Ensure that the (Tools -> Options -> Debugging -> Enable Source Link support) checkbox is checked

"GenericHostBuilderExtensions.cs is not found".

As for why you fail here. I think it's similar to the issue I provides above. For .net core 3.0 preview, some of its libraries currently don't have source links in github so the vs can't fetch them. (Like .net core 2.0 in 2017, now it has been fixed)

Actually, if you follow options above, you will find you can debug well .net core 2.x web-app, but may fail for not finding source links for .net core 3.0.

As a workaround:

1.You can try getting the source manually from git, and go solution -> Properties -> Debug Source Files, then add your directory to the 'Directories Containing Source Code' to check if it helps for some situation. (Which is describe in details by vancem, thanks to him)

2.And the source of GenericHostBuilderExtensions.cs of the GenericHostBuilderExtensions.cs can be found here.I reproduced same issue, and resolve it by download the xx.cs and find it during debugging.

3.But I have to say this way takes some time. Since not only this file has no source file, we will meet similar issues and have to resolve it by getting corresponding source from git unless all the source in .net core3.0 has its source links. But it may takes some time, sorry for this inconvenience. And I suggest you can post it on github and share link here. Members interested in it will help vote it.

Hope all above helps.

查看更多
登录 后发表回答