Reader small image

You're reading from  C# 7 and .NET Core 2.0 High Performance

Product typeBook
Published inApr 2018
Reading LevelBeginner
Publisher
ISBN-139781788470049
Edition1st Edition
Languages
Right arrow
Author (1)
Ovais Mehboob Ahmed Khan
Ovais Mehboob Ahmed Khan
author image
Ovais Mehboob Ahmed Khan

Ovais Mehboob Ahmed Khan is a seasoned programmer and solution architect with nearly 20 years of experience in software development, consultancy, and solution architecture. He has worked with various clients across the world and is currently working as a senior customer engineer at Microsoft. He specializes mainly in application development using .NET and OSS technologies, Microsoft Azure, and DevOps. He is a prolific writer who has written several books and articles on various technologies. He really enjoys talking about technology and has given a number of technical sessions around the world.
Read more about Ovais Mehboob Ahmed Khan

Right arrow

What comes with ASP.NET Core 2.0

ASP.NET Core is one of the most powerful platforms for developing cloud-ready and enterprise web applications that run cross-platform. Microsoft has added many features with ASP.NET Core 2.0, and that includes new project templates, Razor Pages, simplified provisioning of Application Insights, connection pooling, and so on.

The following are some new improvements for ASP.NET Core 2.0.

ASP.NET Core Razor Pages

Razor syntax-based pages have been introduced in ASP.NET Core. Now, developers can develop applications and write syntax on the HTML with no controller in place. Instead, there is a code behind file where other events and logic can be handled. The backend page class is inherited from the PageModel class and its member variables and methods can be accessed using the Model object in Razor syntax. The following is a simple example that contains the GetTitle method defined in the code-behind class and used in the view page:

public class IndexModel : PageModel 
{ 
  public string GetTitle() => "Home Page"; 
}

Here is the Index.cshtml file that displays the date by calling the GetCurrentDate method:

@page 
@model IndexModel 
@{ 
  ViewData["Title"] = Model.GetTitle(); 
} 

Automatic Page and View compilation on publishing

On publishing the ASP.NET Core Razor pages project, all the views are compiled into one single assembly and the published folder size is comparatively small. In case we want view and all the .cshtml files to be generated when the publishing process takes place, we have to add an entry, which is shown as follows:

Razor support for C# 7.1

Now, we can use C# 7.1 features such as inferred tuple names, pattern matching with generics, and expressions. In order to add this support, we have to add one XML tag as follows in our project file:

<LangVersion>latest</LangVersion>

Simplified configuration for Application Insights

With ASP.NET Core 2.0, you can enable Application Insights with a single click. A user can enable Application Insights by just right clicking Project and hitting Add | Application Insights Telemetry before going through a simple wizard. This allows you to monitor the application and provides complete diagnostics information from Azure Application Insights.

We can also view the complete telemetry from the Visual Studio 2017 IDE from the Application Insights Search window and monitor trends from Application Insights Trends. Both of these windows can be opened from the View | Other Windows menu.

Pooling connections in Entity Framework Core 2.0

With the recent release of Entity Framework Core 2.0, we can pool connections by using the AddDbContextPool method in the Startup class. As we already know, in ASP.NET Core, we have to add the DbContext object using Dependency Injection (DI) in the ConfigureServices method in the Startup class, and when it is used in the controller, a new instance of the DbContext object is injected. To optimize performance, Microsoft has provided this AddDbContextPool method, which first checks for the available database context instance and injects it wherever it is needed. On the other hand, if the database context instance is not available, a new instance is created and injected.

The following code shows how AddDbContext can be added in the ConfigureServices method in the Startup class:

services.AddDbContextPool<SampleDbContext>( 
  options => options.UseSqlServer(connectionString)); 
There are some more features added to Owned Types, Table splitting, Database Scalar Function mapping, and string interpolation that you can refer to from the following link: https://docs.microsoft.com/en-us/ef/core/what-is-new/.
Previous PageNext Page
You have been reading a chapter from
C# 7 and .NET Core 2.0 High Performance
Published in: Apr 2018Publisher: ISBN-13: 9781788470049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ovais Mehboob Ahmed Khan

Ovais Mehboob Ahmed Khan is a seasoned programmer and solution architect with nearly 20 years of experience in software development, consultancy, and solution architecture. He has worked with various clients across the world and is currently working as a senior customer engineer at Microsoft. He specializes mainly in application development using .NET and OSS technologies, Microsoft Azure, and DevOps. He is a prolific writer who has written several books and articles on various technologies. He really enjoys talking about technology and has given a number of technical sessions around the world.
Read more about Ovais Mehboob Ahmed Khan