Create a .NET Core Development Environment using Visual Studio Code - Part 2

Create a .NET Core Development Environment using Visual Studio Code - Part 2

In my previous article, I talked about setting up a .NET Core development environment using Visual Studio Code. It covered how to set up VS Code to run .NET Core Applications with the help of .NET CLI. Even though we were able to develop an application with VS Code, we were missing many things that most of the .NET developers are accustomed to in their standard IDE - Visual Studio. One such feature is the Solution Explorer.

When .NET projects are being built, they are usually contained in solutions. Solutions are containers used by Visual Studio to organize one or more related projects, for example, a class library and a corresponding test project. When we open a solution in Visual Studio, it automatically loads all the projects that the solution contains.

Solution Explorer is the Visual Studio feature that helps to visualize our project solution structure in a tree view. This helps in the easy navigation of the project files.

solution-explorer-visual-studio.png

In this article, I will explain how to bring the solution explorer functionality to our source code editor - Visual Studio Code.

.NET Core Solutions - The .NET CLI way

  • We shall create a simple calculator application that shall do the basic math operations. The application shall contain a C# class library project and a console application.

  • Create a new folder named SimpleCalculator and open that folder in Visual Studio Code.

  • Open the terminal in VS Code and enter the command dotnet new sln --name SimpleCalculator. This command will create an empty solution in the directory.

  • Let us create a class library first. Enter the command dotnet new classlib --name MathOperations in the terminal. This will create a class library project named MathOperations in our directory.

  • Now we need to add the class library project we created to our solution. For this, we need to run the dotnet sln <SOLUTION_NAME> add <PROJECT> command where <SOLUTION NAME> is the name of the solution and <PROJECT> is the path of the .csproj file of the C# project which we are adding to our solution.

  • So to add our class library to our solution we shall run the command dotnet sln SimpleCalculator.sln add MathOperations\MathOperations.csproj in the terminal.

  • Navigate to the class library directory MathOperations. Rename the Class1.cs class file to MathOperations.cs. Add a simple method for adding two numbers in the class.

public static class MathOperations
{
    public static int Add(int num1, int num2) => num1 + num2;
}
  • Let us add the console application to our project. Enter the command dotnet new console --name Calculator in the terminal. This will create a console application project.

  • We need to add this console application to our solution. To do so enter the command dotnet sln SimpleCalculator.sln add Calculator\Calculator.csproj.

  • For accessing the methods we defined in our class library by our console application we need to add a reference of our class library project in our console application. We can use dotnet add reference command for that. Enter the command dotnet add Calculator\Calculator.csproj reference MathOperations\MathOperations.csproj for adding the reference.

  • Modify the Program.cs file to use our method from the class library.

class Program
{
    static void Main(string[] args)
    {
        int num1 = 10;
        int num2 = 20;
        int sum = MathOperations.Add(num1, num2); // Method from class library
        Console.WriteLine($"{num1} + {num2} = {sum}");
        Console.ReadLine();
    }
}
  • Navigate to the console application directory and execute the command dotnet run. We shall get the output 10 + 20 = 30 in the console.

We had just created a .NET Core solution with a class library and a console application with .NET CLI. But for the developers who are used to Visual Studio and are less comfortable with command-line tools, this approach is a letdown. Fortunately, a nice VS Code extension was developed to address this issue.

.NET Core Solutions - The Solution Explorer Way

  • Open Visual Studio Code and install the extension vscode-solution-explorer. This is an extension that will provide a solution explorer view for VS Code. Even though it does not fully feature equivalent to Solution Explorer in Visual Studio, this will provide most of the necessary options like displaying the solution and projects in a tree mode, providing context menus for tasks like adding and removing projects, adding and removing project references etc.

solution-explorer-extension-visual-studio-code.png

  • After installing this extension, a new pane named Solution Explorer will be displayed in the VS Code Explorer sidebar.

solution-explorer-pane-in-vscode.png

  • Let us create an empty solution. Open the command palette (Ctrl + Shift + P) and enter solution. Choose the option Create new empty solution. VS Code will prompt you to give the solution name. Let's the same solution name SimpleCalculator we gave earlier.

  • Now VS Code will create an empty solution with the name we supplied. Behind the scenes, the extension we installed will execute the dotnet new sln command. You can see the empty solution in the Solution Explorer pane.

  • This extension will ask you to create a template folder. If allowed it will add many templates in the .vscode/solution-explorer directory. These templates will be used by the extension to boilerplate for code files in the application when they are added through the extension.

vscode-solution-explorer-extension-templates.png

  • Now let’s add the class library and Console Application to our solution. Right-click on the solution (in the Solution Explorer pane) and select Add new project option from the context menu. This will list the available project types provided by the .NET CLI. Select the Class Library option.

vscode-solution-explorer-project-types.png

  • You will be asked which language you prefer to use. Select C# and the editor will prompt you to enter the name for the project. Give the name MathOperations like we gave before. The class library is added to the solution.

  • Repeat the same steps and add the console application also. Remember to select the Console application from the project templates.

  • Now we need to add the reference of our class library project in our console application. Right-click on the console application project and select the Add Reference option from the context menu. Since there are only two projects in the solution, the extension will automatically add the reference of the other project. If there are more than two projects we need to select the project from a list.

  • Now after adding the reference, make the necessary code changes in the application. You can refer to the previous example we did before. Now right-click on the console application project in the solution explorer tree and select the Run option from the context menu. You can see that .NET CLI will run behind the scenes and run the application and the output will be displayed in the Output window.

vscode-solution-explorer-extension-run-application.png

  • That’s it. We have created a .NET Core application using the Solution Explorer extension of Visual Studio Code.

Summary

.NET CLI will not be a choice for most of the developers who are accustomed to the GUI way of doing things in Visual Studio. So in this article, I have mentioned an awesome extension that provides the capabilities of the solution explorer of Visual Studio which can be used by developers who do not wish to do development the CLI way. This extension is an abstraction over the .NET CLI i.e. the actual CLI commands are being executed in the background by this extension. You can view the commands being executed in the Output window of Visual Studio Code.

So we have just enhanced our awesome text editor to make development in .NET Core more interesting. To know more about this extension, check out the GitHub repository here.