Announcing ASP.NET Core 2.0

The ASP.NET team is proud to announce general availability of ASP.NET Core 2.0. This release features compatibility with .NET Core 2.0, tooling support in Visual Studio 2017 version 15.3, and the new Razor Pages user-interface design paradigm. For a full list of updates, you can read the release notes and you can check the list of changed items in the ASP.NET Announcements GitHub repository for a list of changes from previous versions of ASP.NET Core. The latest SDK and tools can be downloaded from https://dot.net/core. Read the .NET Core 2.0 release announcement for more information and watch the launch video:

With the ASP.NET Core 2.0 release we’ve added many new features to make building and monitoring web apps easier and we’ve worked hard to improve performance even more. More details about these features are available in our follow up post about ASP.NET Core features.

Updating a Project to ASP.NET Core 2.0

ASP.NET Core 2.0 runs on both .NET Framework 4.6.1 and .NET Core 2.0, so you will need to update your target framework in your project to netcoreapp2.0 if you were previously targeting a 1.x version of .NET Core.

Figure 1 – Setting Target Framework in Visual Studio 2017

Next, we recommend you reference the new Microsoft.AspNetCore.All metapackage instead of the collection of individual Microsoft.AspNetCore.* packages that you previously used. This new metapackage contains references to all of the AspNetCore packages and maintains a complete line-up of compatible packages. You can still include explicit references to specific Microsoft.AspNetCore.* package versions if you need one that is outside of the lineup, but our goal is to make this as simple a reference as possible.

What happens at publication time? We know that you don’t want to publish the entire AspNetCore framework to your target environments, so the publish task now distributes only those libraries that you reference in your code. This tree-pruning step should help make your publish process much smoother and easier to distribute your web applications.

Introducing Razor Pages

With this release of ASP.NET Core, we are introducing a new coding paradigm that makes writing page-focused scenarios easier and simpler than our current Model-View-Controller architecture. Razor Pages are a page-first structure that allow you to focus on the user-interface and simplify the server-side experience by writing PageModel objects.

If you are familiar with how to configure your ASP.NET Core Startup class for MVC, then you already have the following lines in your Startup class:

Surprise! The AddMvc and UseMvc configuration calls in your Startup class also activate the Razor Pages feature. You can start writing a Razor Page by placing a new cshtml file called Now.cshtml in the Pages/ top-level folder of your application. Let’s look at a simple page that shows the current time:

This looks like a standard MVC View written in Razor, but also has the @page directive at the top to indicate that this is a stand-alone Razor Page built with that paradigm. HtmlHelpers, TagHelpers, and other .NET Code is available to us in the course of the page. We can add methods just as we could in Razor views, by adding a block level element called @functions and writing methods into that element:

We can build more complex structures by taking advantage of the new PageModel object. The PageModel is an MVVM architectural concept that allows you to execute methods and bind properties to the Page content that is being rendered. We can enhance our sample by creating a NowModel.cshtml.cs C# class file in the Pages folder with this content:

With this class that inherits from PageModel, we can now do more complex interactions and build out a class that can be unit tested. In this case, we are simply loading the LastModified property on the Now page and setting that to the LastModified property. Also note the OnGet method to indicate that this PageModel handles the HTTP GET verb. We can update our Razor Page with the following syntax to start using the PageModel and output the last update date:

Updated Templates and SPA Templates

The templates that ship with ASP.NET Core have been enhanced to not only include a web application that is built with the MVC pattern, but also a razor pages web application template, and a series of templates that enable you to build single-page-applications (SPA) for the browser. These SPA templates use the JavaScript Services functionality to embed NodeJS within ASP.NET Core on the server, and compile the JavaScript applications server-side as part of the .NET build process.

Figure 2 – New ASP.NET Core Templates in Visual Studio 2017

These same templates are also available out of the box at the command-line when you type dotnet new:

Figure 3 – Templates available with the dotnet new command

DbContext Pooling with Entity Framework Core 2.0

Many ASP.NET Core applications can now obtain a performance boost by configuring the service registration of their DbContext types to use a pool of pre-created instances, avoiding the cost of creating new instance for every request. Try adding the following code to your Startup/ConfigureServices to enable DbContext pooling:

Monitor and Profile with No Code Changes and Application Insights

ASP.NET Core 2.0 runs with no modifications necessary on Azure App Service and comes with integrations that provide performance profiling, error reporting, and diagnostics from Azure Application Insights. In Visual Studio 2017, right-click on your project and choose “Add – Application Insights Telemetry” to start collecting data from your application. You can then review the performance of your application including all log messages directly within Visual Studio 2017.

Figure 4 – Telemetry Reported in Visual Studio 2017

That’s nice when you’re developing your application, but what if your application is already in Azure? We’ve got support in the Azure portal to start profiling and debugging, and it starts when you first publish your application and navigate to the cloud portal for your new app service. Azure will prompt you with a new purple banner indicating that Application Insights for monitoring and profiling is available

When you click through that banner, you will create an Application Insights service for your application and attach those features without recompiling or redeploying. Shortly afterwards, your new Application Insights service will start reporting data about the activity captured.

Figure 6 – Initial Application Insights Overview on Azure Portal

It even shows the number of failed requests and errors in the application. If you click through that area, you’ll see details about the failed requests:

Figure 7 – Failed Requests Report on Azure Portal

There is a System.Exception thrown, and identified at the bottom of the screen, if we click through that reported exception, we can see more about each time that exception was thrown. When you click through a single instance of those exceptions, you get some neat information about the exception including the call stack:

Figure 8 – Exception Analysis in the Azure Portal

Snapshot debugging in Application Insights now supports ASP.NET Core 2.0. If you configure snapshot debugging in your application, then the “Open Debug Snapshot” link at the top will appear and show the complete call stack and you can click through method calls in the stack to review the local variables:

Figure 9 – Stack Trace in Azure Portal

Figure 10 – Local values reported in Azure Portal

Nice! We can go one step further and click that “Download Snapshot” button in the top corner to start a debug session in Visual Studio right at the point this exception was thrown.

What about the performance of these pages? From the Application Insights blade, you can choose the Performance option on the left and dig deeper into the performance of each request to your application.

If you want the raw logs about your application, you can enable the Diagnostic Logs in App Service and set the diagnostic level to Warning or Error to see this exception get thrown.

Figure 12 – Configure Logging within the Azure Portal

Finally, choose the Log Stream on the left and you can watch the same console that you would have on your developer workstation. The errors and log messages of the selected severity level or greater will appear as they are triggered in Azure.

Figure 13 – Live Console Logging inside the Azure Portal

All of the Application Insights features can be activated in ASP.NET Core without rebuilding and deploying. Snapshot debugging requires an extra step and some code to be added, and the configuration is as simple as an extra NuGet package and a line in your Startup class.

Simplified Application Host Configuration

Host configuration has been dramatically simplified, with a new WebHost.CreateDefaultBuilder included in the default ASP.NET Core templates that automatically allocates a Kestrel server that will attempt to run on IIS if it is available, and configures the standard console logging providers. Your Program.cs file is simplified to only this content:

That reduces the possibility of accidentally breaking some of the standard configuration that most developers were not altering in their ASP.NET Core applications. Why make you write the same boilerplate code over and over, when it could be simplified to 3 lines of code?

Summary

These updates in ASP.NET Core 2.0 provide new ways to write your applications and simplify some of the operational process of managing a production application. A word of thanks to our .NET Community for their feedback, issues, and contributed source code on GitHub. They’ve really been a huge help in delivering this new version of ASP.NET Core. We encourage you to download the latest .NET Core SDK from https://dot.net/core and start working with this new version of ASP.NET Core. You can watch the launch video for .NET Core 2.0 and ASP.NET Core 2.0 at: https://aka.ms/dotnetcore2launchvideo

Don’t you think it would be helpful to indicate how UseCookieAuthentication is totally broken? Also social logins? The basic templates in the tooling don’t even show what the new world is supposed to look like.

Agreed. Probably more crazy frustrating is that all the Identity stuff is broken. Even if you look at the new template, it still includes all the older 1.1 support. Then EF 2.0 stuff breaks also. I used to tell folks I liked Microsoft tools because everything just “worked” when updates occur. Not so much any more.

So, I really don’t think I can easily upgrade to Core 2.0 as it will mean I have to learn how all the new .NET Identity stuff is supposed to work, without really any documentation or template examples.

Thanks Jeffrey, is there a list somewhere what regions are already running? It’s a little difficult to figure out if the upgrade didn’t go well on my side or my region (East US) doesn’t have .NET core 2. from just getting HTTP Error 502.5 – Process Failure

Is there a way to tell on the app service what .net core version is available? Does the app service need to be recreated?

It seems that https://github.com/aspnet/MvcPrecompilation/pull/148 caused the .PrecompiledViews.pdb file being copied to the publish directory all the time, even if ‘DebugSymbols’ is set to false (false). Unfortunately, does not work here to prevent the file from being published (Web deploy). Maybe you guys can fix in 2.1.0?

Just today I tried out the new ASP.NET Core 2.0 but for some reason the integration layer with Windows Services seems to be broken: IWebHost.RunAsService() method keeps throwing the same non-sensical exception pretty much out of the box.

I must say it is a bit of a disappointment. I had been waiting for Core 2.0 for quite some time hoping it would come close enough to full framework but apparently some gaps are still there, if not in the product then in the default configuration and documentation for sure.

Please i have downloaded the SDK and installed it, yet vs 2017 community edition not seeing the 2.0. Can you please help in this regard. I am very new to Microsoft technology, so please make it simple and step by step. Please note that I was fairly working on the 1.1 before the 2.0 came out. Please help urgently as I do not want my learning to slow down.

It looks like Azure App services is supporting 2.0 – but the Government Azure does not yet. Do you know when the US Government will support this? My code with 2.0 Preview worked in the Azure Government cloud – but failed to start once I upgraded to RTM 2.0.

> ASP.NET Core 2.0 runs on both .NET Framework 4.6.1 and .NET Core 2.0, so you will need to update your target framework in your project to netcoreapp2.0 if you were previously targeting a 1.x version of .NET Core.

Questions:

1. Why talk about .NET Framework 4.6.1 rather than .NET Framework 4.7, given that 4.7 is the newest version of full framework as of this announcement? Shouldn’t everyone be encouraged to use the newest full framework version?

2. How do you reference .NET Core 2.0 in a .NET Framework 4.6.1 project? The example shows how to change the targetFramework element when using .NET Core 1.1, but when using .NET Framework 4.6.1 the project file is much more complicated and doesn’t have the targetFramework element.

3. If .NET Core 2.0 also runs on .NET Framework 4.6.1 and above, why aren’t there any project templates that create a .NET Core 2.0 project for .NET Framework 4.6.1?

I created a netcore 2.0 app in VS2017 and using the publish to a folder feature found that it was unacceptably slow (over ten minutes). It was even worst publishing to Azure Web App.

Just to make sure it wasn’t my app, I created an asp netcore 2.0 Empty template app using VS2017 and publish to a folder and it took three minutes. The same empty template app in .Net publish to a folder took only a few seconds.

I tried to tweak the settings (e.g. delete all files) but no change. Any suggestions on how to improve Publish performance?