DOT NET 6

.NET 6 : Introduction, Features & Example

Introduction

.NET 6 was released by Microsoft on November 8, 2021. A developer needs Visual Studio 2022 to develop .NET 6. console applications.

Support

  • .NET 6 is a Long-term Support (LTS) release that will be supported for 3 years.
  • The .NET 6 runtime is supported with Visual Studio 2022 and Visual Studio 2022 for Mac. It is not supported with Visual Studio 2019, Visual Studio for Mac 8, or MSBuild 16.
  • It is necessary to upgrade to Visual Studio 2022 (which is now 64-bit as well) if you wish to use .NET 6.
  • Multi-OS compatibility (Windows, Linux, macOS, Android, iOS, and tvOS) is supported, including macOS Apple Silicon, and Windows Arm64.
  • With .NET 6, developers can develop browser, desktop, cloud, IoT, and mobile applications on a single platform.

Hire .NET Developers

Implementing .NET 6 :

  • For .NET 6, you need to use a .NET 6 target framework, such as the following:
    • <PropertyGroup>
      <TargetFramework>net6.0</TargetFramework>
      <Nullable>enable</Nullable>
      <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
  • The net6.0 Target Framework Moniker (TFM) grants you access to all of the cross-platform APIs offered. NET.
  • If you want to develop a specific operating-system-specific application (e.g., if you are using Windows Forms or Apple’s iOS), then there are other TFM tools for you to use (that target the various self-evident operating systems). You get access to all the APIs in net6.0 along with a bunch of OS-specific APIs

Features

Hot Reload

  • With Hot Reload, code changes can be viewed without restarting the app.
  • Hot Reload lets you view new changes while your app is running without rebuilding and restarting. It is supported by Visual Studio 2022 and the .NET CLI, for visual basic and c#.

File IO Improvements:

  • The FileStream system has been almost entirely rewritten for .NET 6, with a focus on improving the performance of async file IO.
  • It can be up to a few times faster on Windows now that the implementation doesn’t use blocking APIs.

Global using directives:

  • When you use global using directives, you specify a using directive just once and it will be applied to every file you compile.
  • Syntax:
    • global using System;
      global using a static System.Console;
      global using Env = System.Environment;
  • Statements for global reference can be put in any .cs file, including Program. cs.
  • Other features:
    • Support for Open Telemetry and DotNet Monitor.
    • JavaScript can be used to render Blazor components.
    • HTTP/3 support
    • Integration of File IO with symbolic links.

Example of .NET 6: Create a web API with ASP.NET Core

  • This tutorial shows you how to:
    • Create a web API project.
    • Add a model class and a database context.
    • Scaffold a controller with CRUD methods.
    • Configure routing, URL paths, and return values.
    • Call the web API with Postman.
  • Overview
  • For this demo, I’m using Visual Studio Code(VS Code).
  • Below are the necessary things if you don’t have it installed on your machine, just visit those links.

Create a web API Project:

  • Open VS Code and then open Terminal(ctrl + ~).
  • Change directories (cd) to the folder containing the project folder.
  • Execute the following commands:
    • dotnet new webapi -o BookApi
    • cd BookApi
    • dotnet add package Microsoft.EntityFrameworkCore.InMemory –prerelease
    • code -r ../BookApi
  • These commands,
    • Make a new web API project and open it in Visual Studio Code.
    • The dialog box that appears will ask if you want to add the required assets to the project. Click Yes.

Execute the Project:

  • This template generates an API with support for Swagger for weather forecasting.
  • In the terminal execute the following command:
    ‘dotnet watch’
  • ASP .NET6
  • It will open SwaggerUI on your browser. Now Test the WeatherForecast API.
  • ASP .NET 6 MVC
  • When you hit that button you will see the following kind of JSON response.
    [
    {
    "date": "2022-02-12T14:46:20.8240112+05:30",
    "temperatureC": -3,
    "temperatureF": 27,
    "summary": "Warm"
    },
    {
    "date": "2022-02-13T14:46:20.8242589+05:30",
    "temperatureC": 46,
    "temperatureF": 114,
    "summary": "Sweltering"
    },
    {
    "date": "2022-02-14T14:46:20.8242617+05:30",
    "temperatureC": 49,
    "temperatureF": 120,
    "summary": "Bracing"
    },
    {
    "date": "2022-02-15T14:46:20.8242619+05:30",
    "temperatureC": -14,
    "temperatureF": 7,
    "summary": "Sweltering"
    },
    {
    "date": "2022-02-16T14:46:20.824262+05:30",
    "temperatureC": -4,
    "temperatureF": 25,
    "summary": "Freezing"
    }
    ]

Add Model class:

  • Create a new folder named Models.
  • Add a new C# class Book. cs
  • namespace BookApi.Models
  • {
    public class Book
    {
    public int BookId { get; set; }
    public string? Title { get; set; }
    public string? AuthorName { get; set; }
    public string? Language { get; set; }
    public int Price { get; set; }
    }
    }

Add database context:

  • Create a BookContext.cs file to the Models folder.
  • namespace BookApi.Models
    {
    public class BookContext : DbContext
    {
    public BookContext(DbContextOptions<BookContext> options)
    : base(options)
    {
    }
    
    public DbSet<Book> Books { get; set; } = null!;
    }
    }

Register Database Context in Program.cs:

  • In the previous version of .NET 5.0, We are using the Startup.cs file to register Database context and any kind of other dependencies/middleware.
  • But In .NET 6.0, Microsoft removed Startup.cs file and enabled Program.cs to accept Dependency Injection(DI).
  • A dependency injection container (DI container) in ASP.NET Core is responsible for delivering DI services to controllers, such as the DB context.
using BookApi.Models;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddDbContext<BookContext>(opt =>
opt.UseInMemoryDatabase("BookList"));

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Scaffolding a Controller:

Run the following commands:

  • dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design –prerelease
  • dotnet add package Microsoft.EntityFrameworkCore.Design –prerelease
  • dotnet add package Microsoft.EntityFrameworkCore.SqlServer –prerelease
  • dotnet tool install -g dotnet-aspnet-codegenerator –version 6.0.1
  • dotnet aspnet-codegenerator controller -name BookController -async -api -m Book -dc BookContext -outDir Controllers

The above commands:

  • Structuring requires NuGet packages for Scaffolding.
  • Set up the scaffolding engine (dotnet-aspnet-codegenerator).
  • Scaffold the BookController

Now run the application by dotnet watch command, it will open Swagger-UI. Execute the POST endpoint that will add records into memory.

DOT NET 6 Watch

After executing the GET endpoint, it will return a JSON response.

Out Put

And now from the Swagger, you can execute other endpoints as well.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply