dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.7k stars 1.06k forks source link

Debugging .net core 3.0 from hosted application #3663

Open giordi91 opened 5 years ago

giordi91 commented 5 years ago

I am hosting a .net core 3.0 context into my c++ application. Unluckily no matter what I try I am not able to debug the c# code. Is there any way to do so?

I am loading an entry point assembly using hostfxr.h as shown in the dotnet/samples. Then from inside c# I am loading all the other c# dlls using the documentation from here: https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support

Everything works fine, I am mostly missing the ability to debug the c# code. That is the main reason I picked c# over other languages for the integration.

I have enabled native code debugging in my c# application, but that did not help. Do I need to load the pbd files manually somehow?

giordi91 commented 5 years ago

Looks lile I did not properly enabled mixed debugging, when I do so, it complains that .Net Framework was loaded not .net coreCLR as this: image

The only way I found is to set the coreCLR debugger when attaching as this: image Of course this is a more work and not as nice of a iterative debugging experience. Is there any way to set this setting directly in the solution such that I can debug running F5?

Best regards

livarcocc commented 5 years ago

I don't believe this is the right repo for this issue.

I would instead file a vs feedback item in visual studio and this will be routed to the appropriate team.

Short of that, @@tommcdon is this something that you could help with?

tommcdon commented 5 years ago

Is there any way to set this setting directly in the solution such that I can debug running F5?

@giordi91 the debug engine is controlled by the VS project system. What type of project are you using to debug the application? Note it is possible to open an EXE as a project (this is known as the "EXE" project system) and the debug engine can be set using project properties. An EXE project can also be created using the "/DebugExe" command-line arg on devenv.exe - https://docs.microsoft.com/en-us/visualstudio/ide/reference/debugexe-devenv-exe?view=vs-2019.

Note that there is a known limitation where coreclr mixed mode debugging requires coreclr.dll to be loaded in the process. This is because the CLR Debugger Services libraries are loaded into the debugger once the coreclr.dll module is loaded into the debuggee. The CLR Debugger Services libraries required for both native and managed debugging.

Also please post a link to the VS feedback item once it is posted and we will help route it to the appropriate team.

giordi91 commented 5 years ago

@livarcocc Apologies, I am really new to the whole .net core ecosystem so I might be misplacing issues. I will do my best to redirect them properly. At the beginning I thought was a .net core issue but then boiled down to be a possible VS issue?

@tommcdon I am debugging my engine, which is a few c++ solution and now some c# dlls. I have a test suite which is c++ using catch exe that is what the debugger runs, the exe uses a c++ dll, which is the one that hosts .net core context. The one that use hostfxr to load the main c# assembly. Then once the c# assembly is loaded and initialized I use that DLL to load all the other c# dlls, which in this case would be the "user scripting dlls" that needs to be dynamically loaded and optionally recompiled.

Isn't coreclr.dll automatically loaded when hostfxr is loaded resolved and the context initialized? Do I need to open it manually in the c++ executable?

I am running 90% of the same code as this sample: https://github.com/dotnet/samples/tree/master/core/hosting/HostWithHostFxr The only difference is that that code is wrapped in a library that then another executable uses.

As mentioned I am able to debug if I do attach and explicitly and ask to use the managed coreCLR debugger. I can't do the same if I start debugging with F5.

I hope I was not too confusing! Should I submit the VS feedback anyway? Please let me know

iamsunny commented 4 years ago

Running "npm i -g azure-functions-core-tools@3 --unsafe-perm true" did help. Though I'm using 16.4.5 already, I was having the same error. Running the command and restarting the Visual Studio resolved the concern

https://stackoverflow.com/a/60082110/1057667

ForNeVeR commented 4 years ago

FYI, I've encountered the same problem while debugging PSReadLine project. It turns out VS will implicitly use project's target framework to choose a debugger type for a project. So, after I've replaced

<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>

with

<TargetFrameworks>netcoreapp3.1</TargetFrameworks>

then the debugger started successfully.

(the process in question was a PowerShell Core instance started using launchSettings.json)