Tap the power of breath, mantra, mudra, and dhyana.
Online course in Advanced Ajapa Japa and Shambhavi Mudra Meditation by Bipin Joshi.


ASP.NET Core 3.0 - Ten features beginners should know

If you are tracking the progress of ASP.NET Core, you are probably aware that .NET Core 3 and ASP.NET Core 3.0 are being released at .NET Conf 2019. There are many exciting feature additions and improvements to the latest version of ASP.NET Core. You might have already stumbled upon the prominent ones. In this article I am enumerating over ten features / improvements for beginners that are worth noting. My aim is to quickly mention them here so that you can explore them in more detail by visiting Microsoft's official channels such as product documentation and Channel 9.

1. New project templates

In order to use .NET Core 3 and ASP.NET Core 3 you need Visual Studio 2019. There are several project templates and sub-templates that are related to ASP.NET. Some of them are:

  • ASP.NET Core Web Application
  • Blazor App
  • gRPC Service
  • Razor Class Library

These project templates appear as top level templates in New Project dialog.

Each of the top level template takes you to "sub-templates". For example, selecting ASP.NET Core Web Application template shows these options:

As you can see, the above figure displays the primary development options under ASP.NET Core - MVC, Razor Pages, and Web API. A few more templates can be found that use JS frameworks.

These project templates contain several improvements and changes. I encourage you to create a project based on each of these templates and take a peek into Program.cs, Startup.cs, and .csproj files.

2. New methods to register MVC / Razor Pages / Web API services

Prior to ASP.NET Core 3, you used methods such as AddMvc() inside ConfigureServices() to register certain services with DI container. The same method was used by MVC, Razor Pages, and Web API projects. In ASP.NET Core 3 there are three independent methods that do this job.

Take a look at this code fragment:

public void ConfigureServices
(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddRazorPages();
    services.AddControllers();
}

You should use AddControllersWithViews() method in MVC applications. The Razor Pages and Web API applications should use AddRazorPages() and AddControllers() methods respectively. That means each of these methods is fine tuned for a specific development option and registers only services related to that option. of course, you can call any combination of these methods depending on you needs. For example, if your app requires MVC and Web API then you will use AddControllersWithViews() and AddControllers() methods.

3. End point routing

In previous versions of ASP.NET Core you used UseMvc() or UseMvcWithDefaultRoute() extension methods to wire routing middleware. Routing through these methods was the default option. A new form of routing called Endpoint Routing was introduced later but was not the default option. In ASP.NET Core 3 end point routing is the default way to deal with application routes. Older methods are still available but you should avoid using them because endpoint routing offers several advantages over traditional routing. This is how the endpoint routing looks like at code level:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}
                 /{action=Index}
                 /{id?}");
});

4. Blazor server side

Blazor has come a long way since its inception. From being an "experimental" feature it has become a full-fledged framework for building SPA kind of applications using C#, HTML, and CSS. That's right! You can build the goodness of SPA using Blazor without using JavaScript. Of course, it will takes its own time to mature into a framework of choice but at least that process has began. Blazor comes with two hosting models - server side and client side. The server side hosting model uses SignalR as a channel for client-server communication. The client side model uses WebAssembly download and run a .NET application in order to run it within the browser's boundary. As a part of the final release Blazor server side is being released whereas Blazor client side is planned to be released at some later date. Don't miss this welcome addition to the ASP.NET Core family.

The following figure shows Blazor server side and client project templates.

 

5. gRPC support

As far as ASP.NET Core is concerned Web API became the prominent way to create services. Web API is a REST framework. gRPC is a framework for building services that are based on Remote Procedure Calls. It was originally developed by Google. and uses HTTP/2 for transport. gRPC project template is shown below:

6. JSON Serialization classes

JSON data format is quite common these days. In the previous versions, ASP.NET Core used Newtonsoft's Json.NET framework for its JSON processing. You can continue to use it in ASP.NET Core 3 also. However, there is a new set of classes that has taken over the responsibility. These classes reside inside System.Text.Json namespace and are used by default for all JSON processing in ASP.NET Core 3.

The JsonSerializer class has Serialize() and Deserialize() methods that can serialize .NET types to JSON and vice a versa. Async versions of these methods are also available.

7. Entity Framework Core 3 support

Entity Framework Core is a preferred way of writing data access logic under ASP.NET Core. EF Core 3 has a lot of additions and improvements as discussed here. Obviously, ASP.NET Core 3 can leverage all the feature improvements in EF Core 3. EF Core now also has Azure Cosmos DB provider that allows you to work with this emerging NoSQL database engine. 

8. New data provider for SQL Server - Microsoft.Data.SqlClient

Although EF Core has become quite popular due to its OR/M capabilities, there are cases when you need raw data access. If you ever worked with ADO.NET objects such as SqlConnection, SqlCommand, and SqlDataReader you know what I am referring to. Microsoft has developed a successor of System.Data.SqlClient that is available to .NET Core 3 applications as Microsoft.Data.SqlClient NuGet package. Although System.Data.SqlClient will be still available, going forward the new data provider is a recommended way to use in ASP.NET Core applications. If you are looking to quickly port existing ADO.NET code to ASP.NET Core 3 applications then Microsoft.Data.SqlClient would be of great help. Take a look into what it has to offer here.

9. Authentication and authorization middleware

In earlier versions of ASP.NET Core you used UseAutnentication() in the Configure() method to add authentication support and then used [Authorized] attribute in controllers and Razor Pages. Although this is still required, you also need UseAuthorization() to use [Authorize] attribute. There are other improvements to authentication system such as support for certificate and Kerberos authentication. 

10. C# 8 support

.NET Core 3 supports C# 8 and you get all the additions and improvements outlined here. For examples, features such as default interface members, ranges, nullable reference types, asynchronous streams, and null-coalescing assignment can be used in your applications targeting .NET Core 3.

I hope you got a glimpse of what's coming with ASP.NET Core 3.0. Following its release in September 2019, it is expected that version 3.1 will be released in November 2019 that will include minor improvements and bug fixes.

That's it for now! Keep coding!!


"Pay close attention to your breath just as you do to curly braces and semicolons. Breath is a master key to mind and meditation."
#YogaForSoftwareDevelopers

Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Ajapa Japa and Shambhavi Mudra online course are available here.

Posted On : 23 September 2019