Upgrading our projects to .Net Core 3.0

Posted by

Today we are going to upgrade our applications to the latest versions of .Net Core 3.0 and .Net Standard 2.1, both of which were released on Monday the 23rd September, 2019. There are tons of performance and development quality of life benefits to encourage us to upgrade to 3.0.

Of particular note with this release, .NET Core 2.2 will reach end of support on the 23rd December, 2019 – this is very soon, less than three months. Hopefully you have a decent set of automated tests to help with your upgrade. Fortunately, most of the upgrades today seemed to isolated to the startup.cs class, program.cs class, project file, and installed NuGet packages.

Updating all of the projects

First things first, we need to update Visual Studio 2019 to version 16.3. This adds .Net Core 3.0 and C# 8 features. The easiest way to do this is to open the “Visual Studio Installer”, and upgrade Visual Studio there.

Next, we open each project and change the target framework to “.NET Core 3.0”.

Note that as our models project was using .NET Standard 2.0, we will upgrade this to .NET Standard 2.1.

Our solution now has 6 errors and 51 warnings. There are a number of known breaking changes in .Net Core 3 we need to be aware of, and will upgrade the projects one by one, starting with the website, then web service, and finishing with whatever is left, starting with the errors first.

Updating the website

In .NET Core 3, many of the entity framework and authentication modules have moved into their own NuGet packages. First step is to re-add these packages back to the project from NuGet. The list of packages we need in the website is listed below. We also take this moment to ensure we apply all updates to the remaining NuGet packages, to the latest version.

  • Microsoft.AspNetCore.Identity.UI
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
  • Microsoft.AspNetCore.Authentication.MicrosoftAccount
  • Microsoft.AspNetCore.Authentication.Twitter
  • Microsoft.AspNetCore.Authentication.Google
  • Microsoft.AspNetCore.Authentication.Facebook

Looking at the next error, in the startup.cs, the Bootstrap default UI is throwing an error. This is an easy one to fix, we first delete line 48 from the startup.cs:

Next, in our project file, we add “<IdentityUIFrameworkVersion>Bootstrap4</IdentityUIFrameworkVersion>” to line 15.

While in the project file, we need make a few more edits to the packages, as some of the items don’t have a version, or reference .NET Core 2.2. We start by removing the “Microsoft.AspNetCore.App” package on line 27, which has no version. We also remove the Razor package on line 34, which we can see in the screenshot, did not upgrade from .Net 2.2, and is not required in .NET 3. All of the other versions look right, but this is a good upgrade item to ensure you follow.

Next, we are going to resolve the Application Insights warning. The first step is to remove the “UseApplicationInsights()” code from line 26 from our program.cs file:

Then in the ConfigureServices function, in startup.cs, we add the following text: “services.AddApplicationInsightsTelemetry();”.

While in the startup.cs, we also update the compatibility version to 3.0.

We also resolve the endpoint error, adding a flag to disable endpoint routing.

In the Configure function, there is an error on IHostingEnvironment parameter, which is now obsolete.

We replace this with IWebHostEnvironment, also adding a using statement for “Microsoft.Extensions.Hosting” to the top of the class.

Our web project now compiles with no errors or warnings!

Updating the web service

Our web service has many of the same issues as the website, but we have a few extra items with Swagger and the Entity Framework Core 3.0 to address too. We also need to add a new NuGet package for processing JSON, “Microsoft.AspNetCore.Mvc.NewtonsoftJson”.

Swagger is in a complicated spot as of publishing. They haven’t quite been able to release a full version of a .NET 3.0 supported package for “Swashbuckle.AspNetCore”, but they do have some release candidates that work. (Note that the naming here is confusing, Swashbuckle is essentially Swagger). This is where things get sticky. We found that rc3 did not work, but rc2 did. More information about this here in GitHub, but our current recommendation is to NOT use rc3 at this time.

We then need to make a minor change to our startup.cs file, where we initialize Swagger, as currently the “Info” property is no longer working.

To resolve, we need to add “using Microsoft.OpenApi.Models;” to the top of startup.cs, and then replace the “Info” keyword with “OpenApiInfo”.

On the Entity Framework core side, we had to make some minor changes to replace all of the “using System.Data.SqlClient” in our data access with the the newer “using Microsoft.Data.SqlClient” namespaces, to resolve a SQL Parameters warning.

What else did we need to update?

That was most of the resolutions required. We had a small issue in our unit testing project, where we needed to add the “Microsoft.EntityFrameworkCore.InMemory” NuGet package for our mock testing initialization.

All of our console and Azure function applications did not need any extra upgrades, they pretty much worked.

Building in Azure DevOps

As of publishing, the Azure Devops hosted build agents are starting to roll out agents with .NET Core 3 installed on them. We have found the following YAML to be useful to force the agent to use the latest .NET Core 3 SDK. (The .x forces the latest, if you need a specific version, type it out, for example the RTM SDK version is 3.0.100). We assume that in the future, this YAML won’t be needed, but it helps today:

    - task: UseDotNet@2
      displayName: 'Use dotnet sdk 3.x'
      inputs:
        version: 3.x

Deploying to Azure

Finally, as of publishing, Azure web apps still don’t support .Net Core 3.0, but there are three workarounds. Today, we are going to install the extension as a short term work around. We start by opening our web apps in the Azure Portal, and opening the “Extensions”.

Now we select the ASP.NET 3.0 (x64) extension, and accept the legal terms.

We run our DevOps pipelines to build and release our application. As we added version information to our website last week, it was easy to verify that the application is running .Net Core 3.0.

The YAML for this was relatively difficult to find, here it is, as a resource under the web app:

    {
      "type": "siteextensions",
      "name": "AspNetCoreRuntime.3.0.x86",
      "apiVersion": "2015-04-01",
      "location": "[resourceGroup().location]",
      "properties": {
        "version": "3.0.0"
      },
      "dependsOn": [
        "[concat('Microsoft.Web/Sites/', variables('webappname'))]"
      ]
    }

Wrap-up

Today we updated all of our projects to .Net Core 3.0, and our “Models” project to .Net Standard 2.1. This was a relatively quick process, as it took about 15 minutes to resolve the errors, after upgrading the .NET versions in the application properties, and then about 4 hours to resolve the remaining warnings.

As we mentioned earlier, it is worth reviewing your csproj files to ensure there are no 2.2 packages left. In another solution we saw a few 2.2 packages that weren’t upgraded, but were able to resolve by removing the packages manually and adding the NuGet packages back in.

This isn’t the end of this subject, it’s just the beginning. Next we will look at a new .Net Core 2.2 project upgraded to .Net Core 3, and compare it to a NEW .Net Core 3.0 project, and see what is different – spoiler alert: They are different…

A final reminder that .Net Core 2.2 is reaching end of support on December 23rd 2019! Don’t wait too long to start this upgrade!

Note: we continued this series in one more blog post to complete the upgrade: https://samlearnsazure.blog/2019/10/04/comparing-our-upgraded-net-core-3-projects-to-new-net-core-3-projects/

References

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s