In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to write and debug. Net projects in vscode. I hope you will get something after reading this article. Let's discuss it together.
Install the plug-in
Using VSCode to write dotnet core projects in addition to its default features, I recommend installing some very distinctive and useful extensions. It is precisely because of the plug-in mechanism of VSCode that it becomes more powerful and meets our various needs.
1. C # language extension
This is necessary to write C # code in VSCode, and the debugger will be downloaded automatically when the .cs file is opened by default after installation.
2. [C # XML Notes]
This plugin can help you quickly add comments, choose to install it.
3 、 [C# Extensions]
This plugin, highly recommended, can help you initialize the contents of the file, including the corresponding namespace, when creating the file.
There are some other helper classes, such as EditorConfig,Guildes,One Dark Theme,Project Manager, Setting Sync and so on.
New multi-project solution
Open the command line tool and enter in the command line tool:
$: > dotnet new sln-o vscode_tutorial / / create a name named vscode_tutorial in the current directory
The above command uses dotnet sdk to create a new solution file, which you can create manually without the command line, but it is more convenient to create dotnet core-related projects using dotnet new, as shown in the following figure:
After building the solution, we are going to build the project, including a console project, a class library project, and a unit test project.
First set up a common class library project to store our business methods (suppose we are working on a real project) (note that it is already cd into the sln directory)
$: > dotnet new classlib-o VSCodeTutorial.Common/ / create a new class library project under the current directory VSCodeTutorial.Common$: > dotnet sln add VSCodeTutorial.Common/VSCodeTutorial.Common.csproj / / add the project to the solution
In the same way, we set up the console project and the unit test project
$: > dotnet new console-o VSCodeTutorial.ConsoleApp$: > dotnet sln add VSCodeTutorial.ConsoleApp/VSCodeTutorial.ConsoleApp.csproj$: > dotnet new xunit-o VSCodeTutorial.UnitTest$: > dotnet sln add VSCodeTutorail.UnitTest/VSCodeTutorial.UnitTest.csproj
Note here that the template name of the control is console, while we use xunit for unit tests.
At this point, our project structure has been established. Let's use VsCode to open the current directory to see the completed project structure, as shown in the following figure:
Add dependencies between projects
Open the project file VSCodeTutorial.ConsoleApp.csproj using VsCode and add a reference to the Common project
Exe netcoreapp1.1
Also open the VSCodeTutorial.UnitTest.csproj project file and add a reference to the Common project
Netcoreapp1.1
Unlike the above project, there are some additional dependencies, which you can just understand here. If you add package dependencies in nuget, just use PackageReference and fill in the library name and version number as above.
After adding the dependencies, we initialize them with dotnet restore in the root directory, or we can try to compile them with the dotnet build command.
The project dependency is shown in figure 2:
Start writing code.
The overall requirements of this project: I need to open a console program, the runtime requires the user to enter an integer less than 50, the console receives this number and calculates the factorial of this number, and outputs the results to the console.
After a simple thought, I decided to put the factorial implementation into the Common project and unit test it, and the tested code into the UnitTest project.
First of all, let's delete the files that are not needed in the previously generated project to delete the Class1.cs in VsCodeTutorial.Common and the UnitTest1.cs in VSCodeTutorial.UnitTest, of course, you can also keep it.
In the first step, we create a new file MathHelper.cs in the VsCodeTutorial.Common project and add the following code to the file to achieve our factorial. The code is relatively simple and will not be detailed.
Namespace VSCodeTutorial.Common {public class MathHelper {/ factorial, the overflow problem is not considered in this example. Factorial (n) = n * (n Mel 1) * (n Mel 2). * 1; / input parameter n / public static int Factorial (int n) {if (n MathHelper.Factorial (zero)) Int one = 1; var oneResult = MathHelper.Factorial (one); Assert.Equal (1, oneResult); / / check the normal situation again int five = 5; var fiveResult = MathHelper.Factorial (five); Assert.Equal (5 / 4 / 3 / 2 / 1, fiveResult); int ten = 10 Var tenResult = MathHelper.Factorial (ten); Assert.Equal (10'9'8'7'6'5'4'3'2'1, tenResult);}
Run a unit test using the command line
Before using the configuration VSCode, I suggest that you use the command line to run the unit test, which will help you better understand the configuration content.
Enter the command: dotnet test. / VSCodeTutorial.UnitTest/VSCodeTutorial.UnitTest.csproj in the root directory to view the running result:
Very bad, there will be a coding error, and this error has not been solved for the time being.. But I guess the unit test passed, and I'm sure this problem will be solved in subsequent releases. In fact, the output garbled problem can be solved in Console projects. But the good news is that there is no garbled problem in running unit tests in VSCode.
Run unit tests using VSCode
First of all, when you open the project, VSCode may have advised you to configure the relevant content, as shown in the following figure:
Selecting Yes will help you create a new directory and two files. Luanch.json is used to perform the configuration of the debugger, while tasks.json is used to configure various tasks, of which running unit tests is a task.
First, we open tasks.json, and a task has been added by default, as shown below:
{"version": "0.1.0", "command": "dotnet", / / global command, that is, all tasks use this command You can also set "isShellCommand": true, "args": [], "tasks": [{"taskName": "build" in each task / / Task name when the main command is set, the taskName will also be used as a command parameter "args": ["${workspaceRoot}\\ VSCodeTutorial.ConsoleApp\\ VSCodeTutorial.ConsoleApp.csproj"], "isBuildCommand": true, / / only one compilation task can be set for a solution It is useless to set too much, of course, it can also be executed, but you cannot use the shortcut to run "problemMatcher": "$msCompile" / / problemMatcher of the C# project}]}
The global command line is used by default, so you can omit the configuration of dotnet commands in the task, but if your solution includes multiple projects that require different command-line compilation methods, if the front-end website uses grunt to package resources, then the top should be left blank and command should be configured in each subtask.
And if there are multiple compiled projects (such as when the client and server are in a solution), you should also configure command in the subtask and set personalized taskName to distinguish, so I recommend setting command in the task. Let's modify the above code and add a person who runs the unit test.
{"version": "0.1.0", "isShellCommand": true, "args": [], "tasks": [{"taskName": "build_console", "command": "dotnet"args": ["build", / / make up dotnet build / / set the project to be compiled If there are multiple startup projects that can be set as solution files (.sln), there is only one project, so setting the running project can also be "${workspaceRoot}\\ VSCodeTutorial.ConsoleApp\\ VSCodeTutorial.ConsoleApp.csproj"], "isBuildCommand": true, / / set whether to compile the project "problemMatcher": "$msCompile"} {"taskName": "UnitTest", "command": "dotnet", "args": ["test", / / composition dotnet test command "${workspaceRoot}\\ VSCodeTutorial.UnitTest\\ VSCodeTutorial.UnitTest.csproj"], "isTestCommand": true / / set to unit test project "problemMatcher": "$msCompile"}]}
In the above code, I moved the command command to the task and gave each task a good recognizable name. Now there are two tasks here.
The first task, the build_console runtime, compiles the VSCodeTutorial.ConsoleApp project and its dependent projects.
The second task, UnitTest, is a unit test project. Run the dotnet test command. There is a special setting called "isTestCommand": after true is identified as a test project, you can run the command through a shortcut.
The task is built, let's run the task, windows press ctrl+shift+p, and type: task filter command in the pop-up dialog box to get the following options
Select the task: run the test task to run the unit test project we wrote earlier, and you can see the success of the run, as shown in the following figure:
The Chinese display here is normal and there is no garbled code, but I don't know why.. It's just that amazing.
For frequently executed tasks, you can easily call by setting keyboard shortcuts, you can see that I respectively set up the ctrl+shift+t run test task ctrl+shift+b run compilation task, ctrl+shift+r startup selection task, you can set up according to your own preferences.
Start writing console code
Open the Program.cs file in the VSCodeTutorial.ConsoleApp project and modify the code, as follows:
Using System;using VSCodeTutorial.Common;namespace VSCodeTutorial.ConsoleApp {class Program {static void Main (string [] args) {while (true) {Console.WriteLine ("Please enter a number less than 10, enter:"); string input_str = Console.ReadLine () If (int.TryParse (input_str, out var input_int)) {if (input_int > 0 & & input_int dotnet run-p. / VSCodeTutorial.ConsoleApp/VSCodeTutorial.ConsoleApp.csproj)
You can also run the dotnet run command directly in the VSCodeTutorial.ConsoleApp directory.
The result is still in garbled code, but this time we have a solution. We can add a line to the console code to onsole.OutputEncoding = Encoding.UTF8.
Using System;using System.Text;using VSCodeTutorial.Common;namespace VSCodeTutorial.ConsoleApp {class Program {static void Main (string [] args) {Console.OutputEncoding = Encoding.UTF8; / / set console coding while (true) {Console.WriteLine ("Please enter a number less than 10, press enter:") String input_str = Console.ReadLine (); if (int.TryParse (input_str, out var input_int)) {if (input_int > 0 & & input_int
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.