Printing "Hello World" to the terminal takes a very long time - c#

I am just getting started in C# (and coding in general), following a very basic setup tutorial on YouTube. I am trying to run the simple bit of code generated from clicking Terminal>New Terminal:
using System;
namespace Program1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
From what I understand, this should run just about instantly (it runs perfectly for the tutorial instructor).
I Also get the following problem (CS0579) (now resolved thanks to a comment):
Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute'
The file with this problem looks like this:
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]
The launch.json file has not been altered from the auto-generated file:
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/Program1.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
I am running VSCode 1.47.2 and .NET Core SDK 3.1.302

If you've just started this project, and all it consists of is the above code, I'd recommend closing visual studio, deleting the project folder, and starting over. Seems like something got messed up and you're going to take a lot longer figuring out what it was than just starting over.
I know that sounds like the lazy way to do it, but with just a short 'hello world' program, you shouldn't need to take hours debugging it. Just start over.

Related

How to change path of debugger?

when i want to debug my code in c# I have a problem. I saw that it is most likely because I have a folder named "Michał" and not "Micha". anyone know where I can change the path to this debugger?
PS C:\Code\cs> & 'c:\Users\Micha\.vscode\extensions\ms-dotnettools.csharp-1.24.0\.debugger\vsdbg.exe' '--interpreter=vscode' '--connection=874a5847a90444aab691de8de0e08549'
& : The term 'c:\Users\Micha\.vscode\extensions\ms-dotnettools.csharp-1.24.0\.debugger\vsdbg.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spell
ing of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:4
+ & 'c:\Users\Micha\.vscode\extensions\ms-dotnettools.csharp-1.24.0\.d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (c:\Users\Micha\...ugger\vsdbg.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
content of launch.json file: (generated automaticly)
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"WARNING01": "*********************************************************************************",
"WARNING02": "The C# extension was unable to automatically decode projects in the current",
"WARNING03": "workspace to create a runnable launch.json file. A template launch.json file has",
"WARNING04": "been created as a placeholder.",
"WARNING05": "",
"WARNING06": "If OmniSharp is currently unable to load your project, you can attempt to resolve",
"WARNING07": "this by restoring any missing project dependencies (example: run 'dotnet restore')",
"WARNING08": "and by fixing any reported errors from building the projects in your workspace.",
"WARNING09": "If this allows OmniSharp to now load your project then --",
"WARNING10": " * Delete this file",
"WARNING11": " * Open the Visual Studio Code command palette (View->Command Palette)",
"WARNING12": " * run the command: '.NET: Generate Assets for Build and Debug'.",
"WARNING13": "",
"WARNING14": "If your project requires a more complex launch configuration, you may wish to delete",
"WARNING15": "this configuration and pick a different template using the 'Add Configuration...'",
"WARNING16": "button at the bottom of this file.",
"WARNING17": "*********************************************************************************",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net6.0/cs.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

VSCode tasks.json and launch.json for WinExe dependent on dll

I'm trying - for the sake of learning - to write tasks.json & launch.json for VSCode to build this project: ExportableDataGrid
There are two .csproj - one builds a 'Library' and the other a 'WinExe' which depends on the library.
The build is successful, but the Debugger doesn't launch the .exe and has this error:
Error processing 'configurationDone' request. Unknown Error: 0x80131c30
With this cli:
dotnet build Mm.ExportableDataGrid\Mm.ExportableDataGrid.csproj
dotnet build Mm.ExportableDataGrid.Wpf\Mm.ExportableDataGrid.Wpf.csproj
I get these errors:
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [C:\Users\userid\C-Sharp Projects\ExportableDataGrid\mm.exportabledatagrid.wpf\mm.exportabledatagrid.wpf.csf.csproj]
C:\Users\userid\C-Sharp Projects\ExportableDataGrid\mm.exportabledatagrid.wpf\MainWindow.xaml.cs(13,13): error CS0103: The name 'InitializeComponent' does not exist in the current context [C:\Users\userid\C-Sharp Projects\ExportableDataGrid\mm.exportabledatagrid.wpf\mm.exportabledatagrid.wpf.csproj]
C:\Users\userid\C-Sharp Projects\ExportableDataGrid\mm.exportabledatagrid.wpf\MainWindow.xaml.cs(52,13): error CS0103: The name 'dataGrid' does not exist in the current context [C:\Users\userid\C-Sharp Prp Projects\ExportableDataGrid\mm.exportabledatagrid.wpf\mm.exportabledatagrid.wpf.csproj]
Both launch.json and tasks.json are in a root folder containing both folders from the github archive.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Custom .NET Launcher",
"type": "clr",
"request": "launch",
"preLaunchTask": "buildExe",
"program": "${workspaceFolder}/Mm.ExportableDataGrid.Wpf/bin/Debug/Mm.ExportableDataGrid.Wpf.exe",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label":"buildDll",
"type":"shell",
"command":"C:/\"Program Files (x86)/\"Microsoft Visual Studio\"/2019/BuildTools/MSBuild/Current/Bin/MSBuild.exe",
"presentation":{
"clear":true
},
"args":[
"Mm.ExportableDataGrid\\Mm.ExportableDataGrid.csproj",
"/t:Build",
"/p:Configuration=Debug",
"/p:Platform=\"AnyCPU\""
]
},
{
"label":"buildExe",
"type":"shell",
"command":"C:/\"Program Files (x86)/\"Microsoft Visual Studio\"/2019/BuildTools/MSBuild/Current/Bin/MSBuild.exe",
"presentation":{
"clear":true
},
"dependsOn":"buildDll",
"args":[
"Mm.ExportableDataGrid.Wpf\\Mm.ExportableDataGrid.Wpf.csproj",
"/t:Build",
"/p:Configuration=Debug",
"/p:Platform=\"AnyCPU\""
]
}
]
}
Despite much reading I cannot figure out what is going on here.
How do I debug the 0x80131c30 error?
Why do I get the CS5001 error?
Also - I am able to Run Without Debugging, no issues. Seems to be an issue with attaching the debugger.
I just found the issue after weeks having trouble with it.
The fix is to create a "My_Project.runsettings" file specifying "x64" and refer to it in the settings.json of you solution with "omnisharp.testRunSettings":"Path_To_My_File\My_Project.runsettings"
Please see my answer on github for the detailed explanation : https://github.com/OmniSharp/omnisharp-vscode/issues/4361#issuecomment-858540496

How can I configure Visual Studio Code to run/debug .NET (dotnet) Core from the Windows Subsystem for Linux (WSL)?

I've installed .NET Core 2.2 in the Windows Subsystem for Linux (WSL) and created a new project. I've also installed the C# extension for Visual Studio Code and the syntax highlighting and IntelliSense seems to be working.
However, when I try to use the debugger, things stop working. Here's a step by step of what I've tried to do to configure it.
Here's my launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/CodeCore.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/CodeCore.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
And my tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
My directory structure:
But when I click the "Start Debugging" button I get the following error:
launch: program " does not exist
There is a great article on GitHub regarding the subject - Windows Subsystem for Linux.
To cut a long story short, you need to first verify your version after the Windows 10 Creators Update:
~$ cat /etc/os-release | grep -i version
VERSION="16.04.2 LTS (Xenial Xerus)"
VERSION_ID="16.04"
VERSION_CODENAME=xenial
Notice the following:
If you had upgraded to Windows Creators update and already had WSL
installed, you might still have Ubuntu 14 in the WSL. If the version
is 14, run the following commands in a cmd prompt to reinstall and
update WSL.
lxrun /uninstall /full
lxrun /install
Download the debugger:
sudo apt-get install unzip
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg
The debugger will be installed at ~/vsdbg/vsdbg. It is the debuggerPath.
Sample launch.json configuration for launch:
{
"name": ".NET Core WSL Launch",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "publish",
"program": "/mnt/c/temp/dotnetapps/wslApp/bin/publish/wslApp.dll",
"args": [],
"cwd": "/mnt/c/temp/dotnetapps/wslApp",
"stopAtEntry": false,
"console": "internalConsole",
"pipeTransport": {
"pipeCwd": "${workspaceRoot}",
"pipeProgram": "bash.exe",
"pipeArgs": [ "-c" ],
"debuggerPath": "~/vsdbg/vsdbg"
}
}
Please note:
/.vscode/launch.json: This provides an array of different configurations you can use to launch your application. There is a drop down in the Debug view for selecting which configuration is active.
<your-open-folder>/.vscode/tasks.json: This provides an array of different tasks, like building your application, that you can execute. Debug configurations can link to one of these tasks through the preLaunchTask property.
Sample 'publish' task for tasks.json (needed for launching):
{
"version": "2.0.0",
"tasks": [
...,
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/wslApp.csproj",
"-o",
"${workspaceFolder}/bin/publish"
]
}
]
}
Please Note:
preLaunchTask executes dotnet publish, which builds the project on Windows. Since coreclr is cross-platform, the binary can be executed on WSL without any extra work.
pipeProgram is set to bash.exe.
debuggerPath points to vsdbg, the coreclr debugger.
This will not support programs that want to read from the console.
Sample launch.json configuration for attach:
{
"name": ".NET Core WSL Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickRemoteProcess}",
"pipeTransport": {
"pipeCwd": "${workspaceRoot}",
"pipeProgram": "bash.exe",
"pipeArgs": [ "-c" ],
"debuggerPath": "~/vsdbg/vsdbg",
"quoteArgs": true
}
}
Please Note:
"processId": "${command:pickRemoteProcess}" lists the processes running on WSL using the pipe program.
quoteArgs will quote any arguments and debugger commands with spaces if set to true.
Use sourceFileMap to map sources if they are available in a
different location than where they were built. If you build your
project in Linux, make sure to add a map from the /mnt drive
letters. Example: "sourceFileMap": { "/mnt/c/": "c:\\" }
File and paths are case sensitive in Linux.

VS Code: debug project .NET Core project that resides in a subfolder

After moving my .NET Core project into a subfolder in the solution I am no longer able to Debug it using VS Code.
If I move tasks.json and launch.json into the project folder and open VS Code there I am able to debug, but I would like to be able to open the entire solution in VS Code.
The error message I am getting is this:
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
The error seems to state that my task runner can no longer find my project, so I tried adding this to tasks.json:
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [ ],
"isBuildCommand": true,
"showOutput": "silent",
"problemMatcher": "$msCompile"
}
],
// My addition
"options": {
"cwd": "${workspaceRoot}/myProjectFolder"
}
}
Doing this I am able to start the task, but I then get the following error:
Exception thrown: 'System.IO.FileNotFoundException' in
Microsoft.Extensions.Configuration.FileExtensions.dll
How do I configure the .NET Core debugger to be able to run a project that resides in a sub folder?
The problem is your tasks.json, you should copy your subfolder .vscode/tasks.json in the parent directory.
When you create your tasks.json from the parent directory, it make a tasks.json like as your tasks.json example.
But if you copy the tasks.json created in the subfolder It should be some like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/<change to your subfolder>/<your project>.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
So there is the .csproj file, it say in the error.
The reason for the FileNotFoundException pointed by mrtedweb, the program not finding the appsettings.json can be solved without changing the program code.
Just edit launch.json, modifying the cwd configuration to point to the project subfolder instead of the root workspace which is the default, from
cwd: ${workspaceRoot} to, ${workspaceRoot}/TheProject.
// C:\Solution\.vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/TheProject/bin/Debug/netcoreapp1.1/TheProject.dll",
"args": [],
"cwd": "${workspaceRoot}/TheProject",
"stopAtEntry": false,
"console": "internalConsole",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:5050"
}
}
]
}
With this launch.json, it's possible to run the application from VS Code opening it from a root solution folder with the project inside a subfolder.
C:\Solution>code .
And it can also be run from the commandline without changing the paths in the program from the project subfolder.
C:\Solution\TheProject>dotnet run TheProject.csproj
It can no longer locate the appsettings.json file since the project was moved to a subfolder. This can be easily fixed in the StartUp.cs file by adding the following variable to the constructor:
var filePath = Path.Combine(env.ContentRootPath, "<project folder name>", "appsettings.json");
Note: You will need to specify the value for <project folder name>.
Then update the line that reads
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
to read:
.AddJsonFile(filePath, optional: false, reloadOnChange: true)
Also, verify the launch.json file is targeting the proper program file.
"program": "${workspaceRoot}/<project name>/bin/Debug/netcoreapp1.1/<program name>.dll"
Note: You will need to specify the values for <project name>, and <program name>.

Could not find the preLaunch task 'build'

To configure Visual Studio Code to debug C# scripts on OSX, I followed through all the steps listed in the article below:
Debugging C# on OSX with Visual Studio Code
When I tried to debug the sample C# script, Visual Studio Code reported this error:
Could not find the preLaunch task 'build'
As a consequence, I could not inspect the variables defined in the script.
This is a copy of the launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch console application",
"type": "mono",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/Program.exe",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false
}
]
}
This is a copy of the tasks.json file:
{
"version": "0.1.0",
"command": "mcs",
"args": [
"-debug",
"Program.cs"
],
"showOutput": "silent",
"taskSelector": "/t:",
"tasks": [
{
"taskName": "exe",
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
How do I resolve this?
You can use the Visual Studio Code to solve it.
When you see the error message, click on the steps below
Configure Task
Create tasks.json file from template
NET Core Executes .NET Core build commands
The VSCode will create a file like it:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
It's finished. The VSCode will build the project before run.
The error occurs because Visual Studio Code cannot find any task in the tasks.json with the taskName value set to 'build'.
The preLaunchTask property of the launch.json file defines the task that should be executed before the script is launched. From the question, Visual Studio Code has been configured to run the task build before launching the script:
preLaunchTask: 'build'
But there's no task named 'build' in the tasks.json file.
To fix this, you should change the value of the preLaunchTask property to 'exe', which is the build task that has been defined in the tasks.json file.
It seems like this will be different for every scenario.
For me what #Jeferson Tenorio worked, but it needed a few more steps so let's add them:
Click on Configure Task:
Create tasks.json file from template
.NET Core Executes .NET Core build commands
Go to your launch.json file, and under configurations/program you will find this:
${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll
Simply replace <insert-target-framework-here> and <insert-project-name-here> with your target framework, in my case that would be netcoreapp2.0 and then your project name (if you haven't changed anything your project name should be the same as the folder where you created your project), it should look something like this:
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/MyProject.dll"
I hope this helps.
Visual Studio Code: Could not find the preLaunchTask 'build'?
As suggested in the above answer, you need to have the launch items defined in launch.json file and not in blah.code-workspace file.
The latter doesn't read tasks defined in tasks.json but only those defined in the same .code-workspace file.
Bug reports:
https://github.com/microsoft/vscode/issues/136187
https://github.com/microsoft/vscode/issues/93149
On Linux, to get the build command to work, I needed to change the tasks.json file from:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"args": [
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
to:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build"
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
the reason for this is the fact that Linux will treat the task generated by VSC as running command "dotnet build" instead of "dotnet" with the parameter of "build". Without the change you will receive "dotnet build: command not found" with exit code 127
For Ubuntu Linux 20.04 LTS (but may well be the same on other OS's) what got preLaunchTask working for me, was using both a local tasks.json and launch.json
So my folder structure (pre-build) is:
.vscode/launch.json
.vscode/tasks.json
dist
src/index.ts
package.json
tsconfig.json
My launch.json contains:
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "TS preLaunchTask-build",
"program": "${file}",
"preLaunchTask": "tsc: build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"skipFiles": [
"<node_internals>/**", "node_modules",
]
},
]
}
My tasks.json contains:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"command": "echo hello yes working!",
"problemMatcher": [],
"label": "myTask"
},
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": ["$tsc"],
"group": "build",
"label": "tsc: build"
},
]
}
And my tsconfig.json contains:
{
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true,
"target": "es5",
"module": "commonjs"
},
"include": [
"src/**/*"
]
}
Usage: whilst within the index.ts just hit F5 with a breakpoint set
I have also included another task, "myTask" you can change the preLaunchTask line in your launch.json to: "preLaunchTask": "myTask", (where it will output some text to console to verify preLaunchTask is now working)
That way, if you still have issues, you can see if the issue is in your tsconfig setup, or if it's a preTaskLaunch setup issue.
(I would have thought it should have resolved this itself, but apparently not at the current time of writing anyway - but does force the advantage (for me) of committing debug config to the repo on project basis rather than global config)

Categories