I have to admit I have not used VS Code much. When coding on my personal projects, I am using Visual Studio for Mac on my MacBook Pro or Visual Studio on my work laptop. In my last blog, I discussed options for replacing Visual Studio for Mac. Just in case you did not know, the official retirement date for Visual Studio for Mac is August 31, 2024. You have some time to start switching over to another IDE. There are several options that I pointed out in that article.
So now it is time for me to really start learning VS Code. This series of blogs introduce you to VS Code with the new C# Dev Kit and related extensions. It will be focused on building a solution on a Mac but the same should apply to running VS Code on as Windows laptop.
Downloading and Installing VS Code
Where can you download VS Code? If you installed VS Code a while ago, you probably need to go ahead and install it again with the latest (VS Code 2). Here is the download link:
On the download page for VS Code, select the correct installer for your Mac or laptop.
Once downloaded, open your downloads folder. On a Mac, the download will be a zip file.
Unzip the file by double clicking it and you will now have an application called Visual Studio Code 2. You could run it from downloads folder but not highly recommended.
So instead drag it into your Applications folder on the left bar. Now you can easily access it via CMD Space shortcut and starting to type Visual Studio Code.
Pretty simple to do on a Mac. Also notice how long that took you to do. Maybe 10 minutes… That will be an underlying theme with VS Code. Starting VS Code is so much quicker than VS. Opening a solution is also so much faster.
Installing C# Dev Kit and other related Extensions
After installing VS Code, now comes the fun. You need to install some extensions. It seems like everything in VS Code is an extension.
In order to add extensions, tap the “Extensions” icon on the left navigation bar. You will notice a badge icon when you need to update or reload VS Code since an extension was updated.
Now you can search the market place for the extensions you want to install. Here is a list that you probably want to start with…
- C# Dev Kit
- IntelliCode for C# Dev Kit
- C#
- .NET Extension Pack
- .NET Install Tool
- and since most of my development is for Azure, I also added many extensions for Azure
- Azurite
- Azure Functions
- Azure Databases
- Azure Resources
- and the list goes on and on…
VS Code Walkthroughs
When VS Code opens, you’ll notice Walkthroughs on the right side of the splash screen. There is a lot of great tutorials to help you learn VS Code.
Existing Visual Studio Solutions and Projects
Once VS Code is open, you can also easily open existing solutions you already created in Visual Studio for Mac. Just click the “Open” button to start. Browse to the folder where your existing solution is and open it.
Here is an existing project that I opened in VS Code.
I can edit, save, build and debug using VS Code.
Now let’s start something from scratch
My idea is to have an API that drops a message onto a Service Bus so an Azure Function can read the message. Let’s also add a class library to share between the API and the Azure Function. Hmm, am I biting off more than I can chew??? Probably but let’s see how easy or hard it is to do this… Once we have that working, we’ll add a Web Application and then continue building out a real world example…
My code can be found here: RWE Repo
Create a new Solution: Umm, where the #$@% do I start
Let’s create a new solution… This is soooo foreign to me. Where is my menu option to create a solution??? Well there is none. To create a new solution, you basically create a folder/workspace and run a command in the terminal. So let’s go ahead and do that.
Obviously open VS Code
Select File > Open Folder from the main menu.
In the Open Folder dialog, create a folder. I called my folder RWE for “Real World Example”. Next, click Open.
Now open terminal by selecting View > New Terminal. Terminal opens with the command prompt in the folder you just created.
In Terminal, run this command: dotnet new sln. This will output the following result: The template “Solution File” was created successfully.
Now I have a solution. But seriously nothing is in it.
So let’s create a Class Library
Most solutions in today’s world have a common class library to share across the various projects. This will be easy, right? It wasn’t terrible, but to be honest I fumbled around with all the commands. Luckily, you can just follow these steps…
In the terminal, run the command to create a class library.
- dotnet new classlib -o RWE.Common
Now we need to add it to the solution so we need to run another command.
- dotnet sln add RWE.Common/RWE.Common.csproj
Now open the solution file. You can see that it added a reference to the Project file.
The .NET Target Framework is set to 7.0. You can verify that in your project file.
Straight out of the box, you have to type every thing by hand in VS Code. Creating class files by hand each time seems a little tedious. I tried finding some CLI to create a class or an interface class. Most of what I found just created an empty file. It would be much better for me if I had menu that could help me create my classes. I quickly found an extension to do that. You might want to install the it before moving on.
Next let’s create a new folder called “Logger”. You can do this by right clicking on the Project.
In this folder, let’s create a new class called “AppLogEvents.cs”. After installing the Extension above, you can right click and select “New C#”.
Here is the class I created.
using System;
using Microsoft.Extensions.Logging;
namespace RWE.Common.Logger;
public static class AppLogEvents
{
public static EventId Create = new(1000, "Created");
public static EventId Read = new(1001, "Read");
public static EventId Update = new(1002, "Updated");
public static EventId Delete = new(1003, "Deleted");
}
Now you will notice we are missing a reference.
So let’s add it. In the Terminal window, go ahead and type the following command:
- cd RWE.Common
- dotnet add package Microsoft.Extensions.Logging
Take a look at your project file and you will notice a new line referring to the new package that was added.
Lets add another class called: “AppLogTemplates.cs“
using System; using Microsoft.Extensions.Logging; namespace RWE.Common.Logger; public static class AppLogTemplates { private const string IdPropertyName = "Id"; private const string MessageIdPropertyName = "MessageId"; private const string EmailPropertyName = "Email"; public const string SendMessagePostBegin = $"Sending a message to Service Bus. Id: {{{IdPropertyName}}} Email: {{{EmailPropertyName}}}"; public const string SendMessagePostEnd = $"Completed sending message to Service Bus. Id: {{{IdPropertyName}}} Email: {{{EmailPropertyName}}}"; public const string SendMessageBegin = $"Begin - {MessageIdPropertyName}: {{{MessageIdPropertyName}}} {EmailPropertyName}: {{{EmailPropertyName}}}"; public const string SendMessageEnd = $"End - {MessageIdPropertyName}: {{{MessageIdPropertyName}}} {EmailPropertyName}: {{{EmailPropertyName}}}"; }
Delete the class.cs class that was created when the project was created.
Finally, build the project by using the following command:
- dotnet build
Let’s create the WebAPI
In the terminal, change the directory to the root folder of the solution and run the command to create a class library.
- cd ..
- dotnet new webapi -o RWE.WebApi
Now we need to add it to the solution so we need to run another command.
- dotnet sln add RWE.WebApi/RWE.WebApi.csproj
The .NET Target Framework is set to 7.0. You can verify that in your project file.
Let’s add the class library to the is project using the following command:
- dotnet add RWE.WebApi/RWE.WebApi.csproj reference RWE.Common/RWE.Common.csproj
Let’s create a new API controller. This is much easier now that we have the new extension. In the Explorer view, right click on the Controllers folder. Under New C#, select Api Controller. A box will appear in the top middle of VS Code. Type in the name for your file – SendMessageController.cs.
I added the logger in plus a Post action that just logs a begin and end.
using Microsoft.AspNetCore.Mvc;
namespace RWE.WebApi.Controllers;
[ApiController]
[Route("[controller]")]
public class SendMessageController : ControllerBase
{
private readonly ILogger<SendMessageController> _logger;
public SendMessageController(ILogger<SendMessageController> logger)
{
_logger = logger;
}
[HttpPost]
public ActionResult Post()
{
_logger.LogInformation(RWE.Common.Logger.AppLogEvents.Update, RWE.Common.Logger.AppLogTemplates.SendMessagePostBegin, 10, "someemail@gmail.com");
// Do something cool later...
_logger.LogInformation(RWE.Common.Logger.AppLogEvents.Update, RWE.Common.Logger.AppLogTemplates.SendMessagePostEnd, 10, "someemail@gmail.com");
return Ok();
}
}
Now, let’s build the project. You will notice that is build both projects and should be successful if everything was done right.
Time to debug… Open Program.cs. Then, click the fourth icon down on the navigation bar on the left. This is the your debugger. Click the “Run and Debug” button.
Select the language…
Now select the configuration you want to debug.
Swagger is pre-configured which is pretty slick and the browser opens ready for you to to start debugging API endpoints.
Add a breakpoint to the new controller and execute your “Post” action by clicking the execute button.
The debugger should stop on the line you added your breakpoint too.
Now we have a Class Library and a Web API in our solution. We can edit, build and debug the code in VS Code.
Conclusion
So when I first started, I was a little nervous about not having all the menus that Visual Studio offers but now I am getting the hang of it. Starting to see why people rave about using VS Code. It starts much faster than Visual Studio.
In the next article, we’ll add an Azure function to the solution. This will be a little more time consuming setting up this project and debugging it.
References
- More info on the retirement can be read here:
- VS Code helpful Links
- My Git Repo