Use vs.code development .net code

 

In this article, I will take you step by step through the graphic form to demonstrate how to develop .NET Core program, testing and debugging in Visual Studio Code. Although some of the features of Visual Studio Code yet reached the level of Visual Studio, but it's actually strong enough to meet our daily development. And their lightweight, plug-in technology and cross-platform features VS is not available. And Visual Studio Code can also create a series of extensions to enhance its functionality through the community, and the community has been active enough. We can expect more cool extensions and features to enhance the VS Code, which will make this lightweight, develop .NET Core cross-platform applications editor smoother and more fun. Quickly followed bloggers start with today's article!

Why write this article?

Because the article also said, .NET Core has been fully cross-platform, and we also try to use Linux, but Part CentOS developers ASP.NET Core introductory tutorial  CLI be used in the development of .NET Core words, I feel very suited. After all, we come from excessive .net have become accustomed to using Microsoft's Visual Studio development. Is there a comparable Visual Studio development tool that allows us to efficiently develop on .NET Core Linux system? The answer is yes, because Microsoft has developed a cross-platform called Visual Studio Code and open source text editor. Visual Studio Code is so powerful and amazing as it offers built-in intelligence alerts, debugging, and Git support. And Visual Studio Code provides a powerful plug-in extensions. So that you can find in the library plug-in extensions which meet the needs of your plug-ins. If you do not find it in their extended library, you can also create a plug-in own and use it. Cool, right? It started!

installation

In this section, we will explain how to install Visual Studio Code, configuration for the development of .NET Core

Ready to work

  1. Install .NET Core SDK. Specific installation and everyone can click here to view and install it. Because Microsoft's things are more fool, so here is not to demonstrate.

  2. Install Visual Studio Code. You can from here  and then choose to download depending on your operating system, different operating system installation process may vary You can here  see the installation instructions Visual Studio Code. Or because things are more Microsoft fool, so here is not to demonstrate.1541339022729

  3. Installed Visual Studio Code in C # extended to allow Visual Studio Code supports C # development, of course, you can also install the extension in other languages ​​to develop other programming languages, such as python, go and so on. In order to install c # extension, you can Extensions Visual Studio Code icon on the left toolbar or Ctrl + Shift + X to open the Extensions view using keyboard shortcuts. And install extensions from the list in the search box C #. As shown below: 1541339435927

    It should be noted here, the installation is complete, you need to restart the next Visual Studio Code to be able to use C # extended features. 1541339538011
    The following screen will appear after restarting, it said that it has installed the C # Extended1541339685496

    Use Visual Studio Code develop the basic .NET Core program

    Since the environment is ready, so now we have been using Visual Studio Code to develop a .NET Core application now!
  4. A position created on a computer named DotNetCoreSample empty folder, then right-click the folder, select "Use Visual Studio Code Open" from the pop-up menu. This opens the Visual Studio Code, and select the folder as a workspace. Of course, it can be turned by the step shown below this folder, the operation according to your diet like.1541340456205

  5. Use `Ctrl + Shift +` `shortcut to quickly open in Visual Studio Code terminal, as shown below:

    1541590018735

  6. Next we use the dotnet new console --name DotNetCoreSample command to create a foundation in this open terminal inside the console program and restore. As shown below

    1541590248007

  7. Next we open the generated Program.cs file, Visual Studio Code OmniSharp plug-in installed, and then will pop up in the bottom right corner following ask whether you need to generate debug window used to build assets and file shown in Figure, here click "Yes" it will help us generate "launch.json" and "task.json" files, which will help build the code using Visual Studio and debug applications.

    1541590405069

    1541590667259

  8. Let's modify the contents of the Program.cs file, add the following line of code. Then save the file, and move the mouse to the terminal, then the terminal cd to our project directory cd DotNetCoreSample. Input dotnet run and press Enter to see the contents as shown below:

    1541591036238

Use Visual Studio Code in vscode-solution-explorer Solution Explorer plug-in

It may be a lot .neter friends who just started using Visual Studio Code when it is not suited to a variety of command-line command to create dotnet projects and solutions. Fortunately, Visual Studio Code extension provides a solution to prevent similar to Visual Studio plug-resource management to solve this problem. Let's step by step look at how to use this plug-in now!

  1. Open Visual Studio Code extension, and then enter vscode-solution-explorer, and then follows the installation shown in FIG.

    1541591973934

  2. After installing the plug-in, VS Code Explorer in the left column will have a new pane called "SOLUTION EXPLORER" display.

    1541592290514

  3. Next, we use it to create solutions, and add items in the solution of it. We press the shortcut keys Ctrl + Shift + P

    Then select "Create a new empty solution" VS Code you will be prompted for a name solution. We enter a SimpleCalculator as the name of the solution.

    1541592847941

  4. Now, VS Code will use the name we provide create an empty solution. In the background, we installed extension will perform dotnet new sln   command. You can see a blank solution solution "SOLUTION EXPLORER" pane. This extension will then ask if you create a template folders, see below. If allowed, it will .vscode / solution-explorer  add some template directory.

    1541593203311

  5. Now, let's add to the library and console applications that gap solutions. Right-click the solution (in Solution Explorer pane), and then select the Add new project option from the context menu. This will list the available project types provided by the .NET CLI (see the figure). Select the "library" option.

    1541593412253

    1541593430198

  6. You will be asked which language you will use. Select C #, the editor will be prompted to enter a project name. Before we are given like that given MathOperations name. Library has been added to the solution.

  7. Repeat the same steps and add the name " Calculator  Console Application". Remember to select Console Application template from the project.

  8. Now we need to add the class library project in a console application references. Right-click the Console Application project, and then select from the context menu, select "Add Reference" option. Because the solution is only two projects, the extension will automatically add a reference to another project. If there are two or more projects, we need to select an item from the list.

    1541593786278

  9. Navigate to the library catalog MathOperations . The Class1.cs   class file rename MathOperations.cs . Simple way to add a simple addition of two numbers in the class, as follows:

        public static class MathOperation    
        {    
            public static int Add(int num1, int num2) => num1 + num2;
        }  
  10. Navigate to the Calculator program to modify the console and Program.csuse class library file. Here you can use Shift + Alt + Fkeyboard shortcuts to format code. As follows:

    static void Main(string[] args)
            {
                int num1 = 10;
                int num2 = 20;
                int sum = MathOperation.Add(num1, num2); // Method from class library    
                Console.WriteLine($"{num1} + {num2} = {sum}");
                Console.ReadLine();
            }
  11. Now, right-click resolve console application project in Solution Explorer tree, then select "Run" option from the context menu. You can see the .NET CLI application will run in the background. And outputs the result in the Output window, as shown below.

    1541594980618

    1541594999015

Visual Studio Code running test plug-in .NET Core applications

Unit testing is an integral part of software development. Here I do not intend to explain in detail the unit tests, because there are many online resources. I can only tell you how to include unit tests in .NET Core application and can be used to extend the Visual Studio Code running unit tests.

  1. Let the library the way we write down the mathematical operations
 public static class MathOperation
    {
        public static int Add(int num1, int num2) => num1 + num2;
        public static int Subtract(int num1, int num2) => num1 - num2;

        public static int Multiply(int num1, int num2) => num1 * num2;

        public static int Divide(int num1, int num2) => num1 / num2;
    }
  1. Now, we need to add a unit test project in the solution.

We can use .NET CLI or the above-mentioned Solution Explorer extension to add a unit test project. To expand the project by adding Solution Explorer, right-click the solution, and then select "from the context menu to  add a new item  ." Choose from a project template xUnit Test Project  and named  MathOperationTests . After creating a test project, MathOperations add a reference library to test the project.

If you are using the .NET CLI, you need to run the following command.

dotnet new xunit -n MathOperationTests  
dotnet add MathOperationTests\MathOperationTests.csproj reference MathOperations\MathOperations.csproj  
dotnet sln SimpleCalculator.sln add MathOperationTests\MathOperationTests.csproj
  1. The UnitTest1.cs rename OperationTests.cs . We have to change the class name in the code. Now we will add some test library method.

    public class OperationTests
        {
            [Fact]
            public void AddTwoNumbers_ReturnsSum()
            {
                var num1 = 10;
                var num2 = 20;
                var result = MathOperation.Add(num1, num2);
                Assert.Equal(30, result);
            }
    
            [Fact]
            public void SubtractTwoNumbers_ReturnsDifference()
            {
                var num1 = 20;
                var num2 = 10;
                var result = MathOperation.Subtract(num1, num2);
                Assert.Equal(10, result);
            }
    
            [Fact]
            public void MultiplyTwoNumbers_ReturnsProduct()
            {
                var num1 = 10;
                var num2 = 20;
                var result = MathOperation.Multiply(num1, num2);
                Assert.Equal(200, result);
            }
    
            [Fact]
            public void DivideTwoNumbers_ReturnsQuotient()
            {
                var num1 = 20;
                var num2 = 10;
                var result = MathOperation.Divide(num1, num2);
                Assert.Equal(2, result);
            }
        }
  2. Now we need to run the tests we created. We do this using .NET CLI. Open a terminal. Navigate to MathOperationTests directory. Enter dotnet test command. We will get the following output.

    1541596760373

  3. As you can see, less the amount of information output. If we have something similar to the Test Explorer in Visual Studio to execute our unit tests and see the results, it will be good. The good news is there is a called .NET Core Test Explorerthe Visual Studio Code plug-ins. Shown below according to the following install the extension bar code in Visual Studio. But more than illustrate here

    1541597219891

  4. After installing the extension, you can see a beaker icon in the left column of activity. Click on the icon, you will see a test of sidebar panel, which lists the unit test project found. The test items displayed by the tree view in the namespace and class of the packet. You can also see the "Run" "Run all" button and each test button at the top. Click Run All button, you can see all the tests and their results being performed.

    1541597348658

  5. We can see all the tests are passed, and the successful test in the Explorer pane has a green check mark. Now let's make it fail. I will change Add logical way to fail a test.

     public static int Add(int num1, int num2) => num1 - num2;//这里有bug
  6. Now run the test again. We can see our Add test fails methods and test explorer pane red symbols.

    1541597509689

  7. If we navigate to the test the way we write, we can see it now Assert with a red wavy underline methods. If we hover over the wavy line, it will display a message box that displays the actual value and the expected value of the test. The bottom panel VS code (panel terminal is located) of the "  problem" display the same information tab. This can be seen in the following figure.

    1541597599699

  8. Fix the error and run the test again so that all tests are passed, we can see the green flag again.

Visual Studio Code in smooth debug .NET Core Applications

In this section, we will learn how to smooth debugging in Visual Studio Code .NET Core applications. In order to debug .NET Core Applications in Visual Studio Code, we need to install VS Code C # extension. (We have already installed a)

  1. We will add a breakpoint in the Program.cs file Calculator console program. Similarly with Visual Studio, we can left click on the source files from, or place the cursor on a line of code and press F9, in the source code set line breakpoints. Breakpoints the left edge of the editor appears as a red dot.

  2. To start debugging, press F5. This will automatically attach the debugger to our Calculator application to start the application. We can see the execution at the breakpoint we set the stop, which helps us to understand the current state of the program when debugging.

    1541598520046

    Note that here, it is necessary to modify the path and the corresponding item name in the range launch.json Calculator.

    1541598690693

  3. We can see the VS Code of Debug view opens on the left side of the editor. Debug view displays all information related to debugging. We can also note that there is a top of the editor debug toolbar. When debugging, debugging toolbar can be used to code navigation options. Here trying to debug most of the functions with vs2017 about the same, so do not make too much exposition.

to sum up

In this article, I have explained the step by step development, test, and debug .NET Core how to program in Visual Studio Code by graphic tutorial for everyone. Quickly download a try! You will find that you will be more like him!
Reference article: https://www.c-sharpcorner.com/article/create-a-net-core-development-environment-using-visual-studio-code2/

 

Guess you like

Origin blog.csdn.net/weixin_40876986/article/details/90509881