Getting started on ASP.NET Core & React – episode 07

Getting started on ASP.NET Core & React – episode 07

Contents

  1. Episode 1 – Introduction
  2. Episode 2 – Backend / Infrastructure
  3. Episode 3 – Frontend Setup
  4. Episode 4 – Data flow
  5. Episode 5 – Tests / Security
  6. Episode 6 – It’s your turn
  7. Episode 7 – migrate from ASP.NET Core 1.1 to 2.0! (you are here)

It’s been some time since we’ve published last episode of getting started on ASP.NET Core & React course. This time was for you to work on project by your own. We hope you did well.

Today big announcement – we’re going to upgrade our project to ASP.NET Core 2.0! Great stuff is waiting for us 🙂 Some major features of ASP.NET 2.0 you can check out here.

New features are of course great, but there is more important reason why we decided to migrate from ASP.NET Core 1.x to 2.0.

First things is – .NET Standard 2.0 (which comes with ASP.NET Core 2.0) broadens the set of APIs available to include a lot of the missing features. It now supports 32,000+ APIs. Due to this fact, now with ASP.NET Core 2.0 and .NET Standard 2.0 you will be able to reference any full .NET framework library (e.g log4net).

Second thing – we could probably say that whole ASP.NET Core 1.x platform was a one big preview. Everyone who was working with it from the beginning will admit that there was a lot of things changing from version to version. I hope with ASP.NET Core 2.0 things will be different and we can depend on this platform as a stable one.

 

Let’s migrate our app from ASP.NET Core 1.1 to ASP.NET Core 2.0

  1. Make sure you have updated your Visual Studio 2017 to the newest version.
  2. Download and install .Net Core 2.0 SDK. You can download it here.
  3. Perform all required changes according to this documentation.
  4. Migrate authentication and Identity to ASP.Net Core 2.0 according to this documentation.
  5. Verify if tests we’ve got in project are passing.
  6. Verify if application is working correctly.
  7. Commit and push changes to remote repository 😉

Ok, we know what we need to do, so let’s dive into it. I’m going to show you what I’ve done in order to migrate app to ASP.NET Core 2.0 starting from point 3 (I assume that points 1 and 2 are quite easy and don’t require additional explanation).

Update ASP.NET Core 1.1 projects to 2.0

We’re gonna do it manually.

1. Change Target Framework from netcoreapp1.1 to netcoreapp2.0:

In order to do this you must open all projects .csproj files and change:

to

I’ve done this in:

  • SoftwareHouse.Contract.csproj
  • SoftwareHouse.DataAccess.csproj
  • SoftwareHouse.Services.csproj
  • SoftwareHouse.Tests.csproj
  • SoftwareHouse.Web.csproj

2. Update package references:

  • SoftwareHouse.Contract.csproj – has no references, so we don’t need to do anything.
  • SoftwareHouse.DataAccess.csproj – change:

    to
  • SoftwareHouse.Services.csproj – has no references, so we don’t need to do anything.
  • SoftwareHouse.Tests.csproj – delete reference:
  • SoftwareHouse.Web.csproj – change:

    to

3. Update .NET Core CLI Tools:
Open SoftwareHouse.Web.csproj and change:

to

4. Change Package Target Fallback property:
Open SoftwareHouse.Web.csproj and change:

to

5. Update main method in Program.cs:
Open SoftwareHouse/SoftwareHouse.Web/Program.cs and change its content to:

That’s it. Don’t worry if you solution doesn’t rebuild successfully. We still need to do some additional changes.

Migrate Authentication and Identity to ASP.NET Core 2.0

  • Open SoftwareHouse/SoftwareHouse.DataAccess/Models/ApplicationUser.cs and change namespace:

    to
  • Open SoftwareHouse/SoftwareHouse.Web/Controllers/AccountController.cs and:
    1. Change namespace:

    to

    2. Remove field:

    3. Also remove from constructor:

    4. Last thing – in Login method change:

    to
  • Open SoftwareHouse/SoftwareHouse.Web/Controllers/ManageController.cs and:
    1. Add namespace:

    2. Remove field:

    3. Also remove from constructor:

    4. In ManageLogins method change:

    to:

    5. In LinkLogin method change:

    to

    6. In LinkLoginCallback method do the same, change:

    to
  • Open SoftwareHouse.Web/ViewModels/Identity/Models/ManageViewModels/ManageLoginsViewModel.cs and replace its content to:
  • Open SoftwareHouse/SoftwareHouse.Web/Startup.cs and add namespace:

Done 🙂

Verify if solution rebuilds correctly.

Now, we should be able to rebuild solution successfully.

Run unit tests

Go to ProjectsServiceTests.cs na according to episode 05 try to run tests. Probably you will see that no tests have been discovered after solution rebuild. To fix that issue you need to update all packages in SoftwareHouse.Tests project. You can do it using NuGet packages manager.

Eventually in SoftwareHouse/SoftwareHouse.Tests/SoftwareHouse.Tests.csproj I’ve got following package references:

Once you have that you can run tests again and everything should be ok.

Verify if application runs correctly.

To check if really everythings works after migration I’m going to recreate database:

  • Open MS SQL Management studio, delete or rename existing SoftwareHouse database.
  • Open Package Manager Console and type command Update-Database (according to instructions from episode 02)
  • New database should be created.
  • Run application, try to create new account, log in and create few new projects.

For me everything is working fine 🙂

Verify if Hot Module Replacement still works

In order to check this functionality you must run application in debug mode and make some change in any .ts file. It should trigger page reload and you should see changes you’ve made. More details about it you can find in episode 03.

As it turns out, after migrating to ASP.NET Core 2.0 there is some problem with Hot Module Replacment. In order to fix that you must open SoftwareHouse/SoftwareHouse.Web/Startup.cs and to WebpackDevMiddlewareOptions add line:

so eventually it should looks like this:

After this change Hot Module Replacement should works again.

Summary

It wasn’t hard to migrate ASP.NET Core 1.1 app to ASP.NET Core 2.0. It’s look like everything works correctly. The most important thing – we’re up to date with ASP.NET Core platform, so I think we could say that now we’re technical debt-free 🙂

Changes are pushed to master in our course repository on GitHub.