Visual Studio Code cannot debug program - c#

I am trying to use the C# extension in VS code to debug a program, however when I try to run without debugging I get the message "Cannot launch program setting the 'outFiles' attribute might help".
My launch.json file
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}"
}
]
}
My C# code
using System;
// Class declaration
class Geeks {
// Main Method
static void Main(string[] args) {
}
}

As noted in comments you are using the launch.json for a Node project.
The Visual Studio Code C# extension can generate the assets you need to build and debug.
You can generate them through the Command Palette (View > Command Palette) by typing '.NET', and running .NET: Generate Assets for Build and Debug. This command will generate the necessary launch.json and tasks.json configuration files (under the .vscode folder).
See docs and debugging

Related

How to debug dotnet core source code using Visual Studio Code

I would like to explore the source code of dotnet when debugging my application, for example. I would like to hit F11 on the Where Linq method and see step by step what is happening at runtime.
I am aware that this is possible using Visual Studio (https://github.com/dotnet/core/issues/897), and in Rider it is enabled by default, and stepping into it is as easy as debugging my own code.
What it the easiest way to set this up in visual studio code and is it even possible?
Please note, this is NOT a duplicate of Is there anyway to debugging .NET Core source code by Visual Studio Code? as that is regarding simple debugging in VSCode, not stepping into the BCL.
Yes, it's possible.
Two links that were already mentioned by you are valuable but there is one more link https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
First, check 'launch.json' configuration file
{
"version": "0.2.0",
"configurations": [
{
"name": "example name",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/app/bin/Debug/net6.0/app.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"justMyCode": false, // should be false, as we want to debug 3rd party source code
"requireExactSource": false, // https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#require-exact-source
"suppressJITOptimizations": true, // it's better to set true for local debugging
"enableStepFiltering": false, // to step into properties
"symbolOptions": {
"searchMicrosoftSymbolServer": true, // get pdb files from ms symbol server
"searchNuGetOrgSymbolServer": true,
"moduleFilter": {
"mode": "loadAllButExcluded",
"excludedModules": []
}
},
"logging": { // you can delete it if all is ok
"moduleLoad": true,
"engineLogging": true,
"trace": true
}
}
]
}
After you start debugging the 'DEBUG CONSOLE' will contain log like this
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.10\System.Private.CoreLib.dll'. Symbols loaded.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.10\System.Console.dll'. Symbols loaded.
If there was a problem with loading some pdbs you can use dotPeek, as local symbol server.
After that you can use 'Step Into (F11)' button in debug panel at vscode to debug .net source code.

C# - VS Code - launch:program ... does not exist

I am trying to debug a simple "Hello world" application in VS Code, however, when I press Ctrl + F5, it gives me the following error:
If I manually change the path in launch.json from:
${workspaceFolder}/bin/Debug/insert-target-framework-here/insert-project-name-here.dll
To:
"${workspaceFolder}/bin/Debug/netcoreapp2.1/test.dll"
It does work, however before it was working fine without me manually typing the path. Also, I have noticed that VS Code no longer asks to rebuild assets like it did before:
So far I have tried the following:
Uninstalled VS Code, then .NET Core 2.1, deleted the VS Code extension folder from %USER%\.vscode\ , re-installed VS Code, then .NET Core 2.1, and then the C# extension (C# for Visual Studio Code (powered by OmniSharp)).
When the VS Code starts, it does download the "OmniSharp" package successfully, but still, no prompt to rebuild assets when I open a C# file. Debugging gives the same issue as before.
Here is the launch.json:
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
}
And the tasks.json:
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
I found a solution that worked for me.
My VS Code was giving me the same error message, and what I did to fix it was:
- Press the combination Ctrl + Shift + P
- Restart Omnisharp
- Then it asks if you want to add missing files for build.
- Click Yes.
After this I was able to debug my app.
Hope it works for you!
Visit your \bin\Debug\netcoreapp3.1 in your project folder (That you open in VS)
Go to launch.json file in VS:
Replace:
"program": "${workspaceFolder}/bin/Debug//.dll",
With:
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/CSharp.dll",
In may case, the project is called CSharp. Watch out.
I had a the same error. The debugger was looking for the .dll file in ${workspaceFolder}/bin/Debug/netcoreapp3.1/myApp.dll but the file was located in ${workspaceFolder}/bin/MCD/Debug/netcoreapp3.1/tradeAppl.dll
After changing the launch.json file to read
...
"program": "${workspaceFolder}/bin/MCD/Debug/netcoreapp3.1/tradeAppl.dll",
...
I was able to debug the application without any problems.
Since you have :".dll" taged with "<" and ">", it means that you have give it a value.
The easiest way to do it is to open the project in VSCode and use find&replace to replace the:
with your project name which I do believe its: "test" as per the .dll name
Configure your Launch.json like this Gist
And there is no need to Tasks.json , you can Press F5 to build or configure it your self to which command should be run in default shell when you press F5
// "program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/csharp_multi_threads.dll",
You need to change the program value as it reminded
I switched to MS Visual Studio, since I did not find any other solutions.
-Right Click on Project Name And Click on Reveal in Explorer
-Copy Url In Explorer After Folder Name
For Example My Folder Address is
D:\IOT\Projects\LWSIOT_WebApiWM2\LWSIOT_WebApiWM2\LWSIOT_WebApiWM2\bin\Debug\netcoreapp3.1\LWSIOT_WebApiWM2.dll
And In Launch.json :
${workspaceFolder}/LWSIOT_WebApiWM2/LWSIOT_WebApiWM2/bin/Debug/netcoreapp3.1/LWSIOT_WebApiWM2.dll
-Paste in Launch.json on "Program":"<Your_Address>"
-Click On Debug And Its OK
In launch.json , replace :
"program": "${workspaceFolder}/Api/bin/Debug/netcoreapp3.1/Api.dll",
with:
"program": "${workspaceFolder}/Api/bin/linux/Debug/netcoreapp3.1/Api.dll"
Since I was running code in Linux machine so I should have provided Linux Folder' path
Must be an issue with the path to the dll in the program property of the launch.json file.
In my case, it still contained the old .net core framework version after the version upgrade.

Some file not served when run via Visual Studio Code

I have a 'Hello World' Kestrel server generated by Yeoman (as described here).
yo aspnet
When I run the site via the command line everything work well:
dotnet run
If it's run through Visual Studio Code though bootstrap.css, jquery.js and bootstrap.js don't load (404).
Looking at the F12 tools, they are actually coming from different locations. When run via dotnet run they come from https://ajax.aspnetcdn.com/..., but when run through Visual Studio Code the browser is looking for them in a local folder ~/lib/....
a. Why are the files being sourced differently depending on how I run the site?
b. How do I fix this?
Thanks
When you run it from command line, it runs in production mode (no ASPNETCORE_ENVIRONMENT variable set. When you run it from Visual Studio it sets the ASPNETCORE_ENVIRONMENT to Development.
Inside your Razor files, you have a <environments> section which controls which files are served in which production mode. Depending on your environment/OS, you need to set the variable differently. i.e. in Linux you'd need to run ASPNETCORE_ENVIRONMENT=Development dotnet run.
As for the reason why you get 404 when running in development mode, you probably need to copy over the wwwroot folder to your output directory, with this entry in your project.json.
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"copyToOutput":
[
"wwwroot",
"Views",
"appsettings.json",
"web.config"
]
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config"
]
},
Though usually it should be necessary to add wwwroot to copyToOutput

Asp.net Core 1.0 project.json prepublish scripts

Hey this is a part of my project.json file:
"scripts": {
"prepublish": [
"node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js",
"node node_modules/webpack/bin/webpack.js"
],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
},
I want to invoke prepublish scripts every time I publish the application even if it's a development environment and I am running it through visual studio or dotnet run. Prepublish doesn't seem to work in these cases
When you run through VS or using 'dotnet run', you only build and run the application from source, without publishing. Publishing scripts will be triggered, if you call dotnet publish.
The dotnet run command provides a convenient option to run your application from the source code with one command. It compiles source code, generates an output program and then runs that program.
Right now only the following types of script are supported:
precompile
postcompile
prepublish
postpublish
and what you want is more like "beforerun"/"postrun". As a workaround you may try to call your scripts directly from code in your application entry point.

Can't get C# host app to launch from Chrome Extension

I'm following this post to try to setup a C# native messaging host.
C# native host with Chrome Native Messaging
I've got my sample running where I an launch Calc.exe from a bat file, like this:
host.json
{
"name": "tsetools",
"description": "TSE tools for Chrome",
"path": "tse-host.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://ddemkjpofammommjpcmkhpajoccdjcdg/"
]
}
tse-host.bat
#echo off
CD C:\Windows\System32
start calc.exe
The above works fine & launches Calc.exe
But if I change host.json to the following:
{
"name": "tsetools",
"description": "TSE tools for Chrome",
"path": "TSEChromeHost.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://jahdheagjlkdjcoeibobaphodgpgpacc/"
]
}
I never see "TSEChromeHost.exe" get launched in the task manager. I've built the C# code from the post above without any errors & can lunch it manually. I have "TSEChromeHost.exe" in the same folder as my "host.json" file.
Any ideas as what I'm doing wrong?
I think issue is that TSEChromeHost.exe can't be located.
Try this:
You need to change tse-host.bat file like this ( you need to change your working directory to dir where TSEChromeHost.exe is located )
#echo off
Pushd D:\SomeDir
start TSEChromeHost.exe
Change D:\SomeDir to driectory where TSEChromeHost.exe file is located.
And Also Set path key in host.json file like this: ( as in first example )
"path": "tse-host.bat"
Try to specify full path to your TSEChromeHost.exe. On Linux and OSX the path must be absolute. On Windows it can be relative to the directory in which the manifest file is located. (See this documentation)
"path": "Full_path_to_TSEChromeHost.exe"
eg
"path": "C:\\TestFolder\\TSEChromeHost.exe"

Categories