.NET Core 6.0 is a major release of the .NET Core framework that was released in November 2021. It is an open-source, cross-platform framework for building modern applications, including web applications, microservices, desktop applications, and more.
Some of the key features and improvements in .NET Core
6.0 include:
Performance
improvements - .NET Core 6.0 includes various runtime and
framework performance improvements, making it faster and more efficient than
previous versions.
ASP.NET
Core updates - This release includes new features in ASP.NET
Core such as Web API improvements, better Blazor support, and more.
Improved
productivity - .NET 6.0 includes many new features designed
to improve developer productivity, including support for C# 10, top-level
statements, file-scoped namespaces, and more.
Better
support for cloud-native applications - .NET 6.0 includes better
support for building and running cloud-native applications, including
containerization, Kubernetes, and Azure Functions.
New
APIs and libraries - .NET 6.0 introduces new APIs and libraries,
including new APIs for HTTP/3 and gRPC, as well as new libraries for JSON, XML,
and more.
Overall, .NET Core 6.0 is a major release that brings
many new features and improvements to the framework, making it a great choice
for building modern, high-performance applications.
Some
fundamental things we should know when we started to work with .Net Core 6.0
Hot Reload
Hot Reload is a feature
introduced in .NET 6.0 that allows you to make changes to your code while your
application is running and see the changes immediately, without having to stop
and restart the application. This can be a huge productivity boost for
developers, as it enables a faster feedback loop when making changes to the
code.
Hot Reload in .NET 6.0 is
supported in Visual Studio 2022, Visual Studio for Mac 2022, and in the .NET
CLI. The feature supports several scenarios, including adding or removing code,
changing method signatures, and editing XAML or Razor files.
To use Hot Reload in Visual
Studio, you can simply set a breakpoint in your code and then make changes to
your code while your application is running. When you save your changes, the
code will be reloaded automatically, and your breakpoint will be hit again. You
can also make changes to XAML or Razor files and see the changes in real-time.
To use Hot Reload in the
.NET CLI, you can use the dotnet watch command to automatically reload your
application when changes are detected. For example, you can run dotnet watch
run to start your application and watch for changes.
Hot Reload in .NET 6.0 is a
powerful feature that can help boost developer productivity and streamline the
development process. It allows you to make changes to your code in real-time
and see the results immediately, making it easier to debug and test your code.
Project file
In .NET Core 6.0, the
project file format has been updated to a simplified format called
"SDK-style" project files. The new project file format is designed to
be more concise and easier to read, with a focus on the project's dependencies
and configuration.
The SDK-style project file
format eliminates the need for separate project files for different project
types, such as class library, console app, and web app projects. Instead, all
project types are supported within a single project file.
Here is an example of an
SDK-style project file for a console application in .NET 6.0:
<Project
Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference
Include="Microsoft.Extensions.Logging.Console"
Version="6.0.0" />
<PackageReference
Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
</Project>
As you can see, the project
file starts with a <Project> element that specifies the SDK version to
use. The <PropertyGroup> element contains configuration settings for the
project, such as the output type and target framework. The <ItemGroup>
element lists the project's dependencies, which are defined as package
references.
The new project file format
in .NET 6.0 makes it easier to manage and configure projects, with a simplified
structure that makes it easier to read and understand.
Launch Setting Json
In .NET Core 6.0, the launchSettings.json
file is used to configure how the application is launched when running in
Visual Studio or using the dotnet run command. The launchSettings.json file can
be found in the Properties folder of a project, and it contains a set of
profiles that define different launch configurations for the application.
Here's an
example of a launchSettings.json file in .NET 6.0:
{
"profiles": {
"MyApp": {
"commandName":
"Project",
"workingDirectory":
"$(ProjectDir)",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT":
"Development"
},
"applicationUrl":
"https://localhost:5001;http://localhost:5000"
}
}
}
In this example, there is
one profile named "MyApp". The commandName property specifies that
the project should be launched as a project. The workingDirectory property
specifies the project's root directory. The environmentVariables property sets
the ASPNETCORE_ENVIRONMENT variable to "Development". The
applicationUrl property specifies the URLs to use when launching the
application.
You can add additional
profiles to the launchSettings.json file to define different launch
configurations for the application. For example, you might have one profile for
running the application locally and another profile for running the application
in a production environment.
Overall, the
launchSettings.json file in .NET 6.0 provides a convenient way to configure and
manage the launch settings for your application, making it easier to launch and
debug your application in different environments.
Appsetting.json
In .NET Core 6.0, the
appsettings.json file is used to store configuration data for your application.
The file contains key-value pairs that can be used to configure various aspects
of the application, such as database connections, logging, and other
application settings.
Here's an example of an
appsettings.json file in .NET 6.0:
{
"ConnectionStrings": {
"MyDatabase":
"Server=myserver;Database=mydatabase;User
Id=myuser;Password=mypassword;"
},
"Logging": {
"LogLevel": {
"Default":
"Information",
"Microsoft": "Warning",
"System": "Error"
}
},
"MyAppSettings": {
"SomeSetting":
"SomeValue",
"AnotherSetting":
"AnotherValue"
}
}
In this example, the file
contains three sections: ConnectionStrings, Logging, and MyAppSettings. The
ConnectionStrings section contains a connection string that can be used to
connect to a database. The Logging section specifies the log levels for various
parts of the application. The MyAppSettings section contains custom settings
for the application.
You can access the values in
the appsettings.json file from your application code using the Configuration
object. The Configuration object is created by the ASP.NET Core host and is
available throughout the application.
Here's an example of how to
access the ConnectionStrings section in the appsettings.json file:
var builder =
WebApplication.CreateBuilder(args);
// Add services to the
container.
builder.Services.AddControllers();
var connectionString =
builder.Configuration.GetConnectionString("DBConnectionString");
builder.Services.AddDbContext<SQLDBContext>(options
=> options.UseSqlServer(connectionString));
Overall, the appsettings.json file in .NET 6.0 provides a convenient way to store and manage configuration data for your application, making it easier to configure and customize your application's behavior.
Program.cs
Program and
Startup files are unified into Program.cs file. Here you can see how new
Program.cs file’s code looks like.
var builder =
WebApplication.CreateBuilder(args);
// Add services to the
container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request
pipeline.
if
(!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days.
You may want to change this for production scenarios, see
https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
- ConfigureServices is replaced with WebApplication.Services.
- builder.Build() returns a configured WebApplication to the variable app. Configure is replaced with configuration calls to same services using app.