Sunday, January 10, 2021

Visual Studio Code: Creating a C#/WebApi Project with .Net 5

The DotNet CLI worked for .NET Core and it works for .NET 5. The steps to create a C# web service (WebApi project) are as follows once .NET 5.0 is installed:

From a Visual Studio Code Terminal window invoke a command such as the following which creates a project named Learn: 

dotnet new webapi --name Learn

An example of invoking the following command from from Visual Studio Code's Terminal is as follows:

The dotnet new command created a folder, C:\Blog\DotNet5\Learn. Open that folder from Visual Studio Code (File menu | Open Folder):

Once the folder is open, Visual Studio Code will prompt to create the assets required to build and debug the project:

Select Yes from the dialog.

Visual Studio Code's Explorer shows the files contained in the folder:

The Learn.csproj file is the project file. Double clicking on the file in Explorer displays Learn.csproj in an edit window:

The project file, Learn.csproj, shows that the project type is a WebApi project of the .NET 5/.Net Core variety Microsoft.NET.Sdk.Web. The TargetFramework is net5.0 and the PackakageReferemce elements are NuGet packages used by the project.

The program.cs file is the entry point to the web service, meaning the code initially runs when the service is launched:

The entry point configures the web service and invokes the startup class (see Line 17). The startup class is aptly named Startup and is defined in Startup.cs. The purpose of the Startup class is to setup configuration information and invoke middleware used by the service.

The project contains a sample model, WeatherForecast.cs:

The project also contains a sample controller, WeatherForecast.cs, inside the project folder's Controller's folder:

The controller specifies the routes to be used by the service.

The project folder contains project-level configuration files:

The appsettings.json file contains project-level application settings and appsettings.Development.json contains project-level application settings which are only invoked when the service runs in the development environment. Both of the aforementioned applications settings files initial contain settings related to logging and the allowed hosts. A mature project would contain a application settings file per-environment (production, stage, qa, integration, etc.).

The .vscode folder contains files specific to how the code behaves when run under Visual Studio Code. The .vscode folder contains the files the launch.json and task.json. The task.json file contains tasks including how to build the project using Visual Studio Code:

The command to build in Visual Studio Code is ctrl-shift-b.

The launch.json file under the .vscode folder contains launch settings for when the service is run by Visual Studio Code:

Line 13 of launch.json (above) shows that the DLL invoked on launch is Learn.dll. The settings at Line 23 indicates that when running under Visual Studio Code, the environment is considered to be Development which means that the application settings in appsettings.Development.json takes precedence over appsettings.json.

The Properties folder contains a launchSettings.json file which pertains to when the service is launched outside of Visual Studio Code:

Line 25 shows the HTTPS and HTTP URL used when the web service is launched. The environment, Development, is specified as Line 27. When run under Visual Studio Code this value is ignored in deference to the setting in .vscode\launch.json.

To run/debug the code, from Visual Studio Code, click on the Debug Hub:

From the Debug Hub click on the Start Debugging button:

When the Start Debugging button is clicked (above) a browser tab such as the following will be lauched:

The content in the above browser instance is not overly useful. To display something with meaning, change the URL to: https://localhost:5001/swagger

The above change to the URL displays the following:

To glean a glimpse of what Swagger can provided check out the documentation What Is OpenAPI?:

Appendix A: Dotnet New

The documentation for the DotNet CLI's new command is found at dotnet new. The available project templates described in the documentation are as follows:

No comments :

Post a Comment