Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate from ASP.NET Core 2.0 to 3.0

I have an application in ASP.NET Core 2.0. I want to upgrade it to ASP.NET Core 3.0. How can I do that?

like image 876
LCA Avatar asked Feb 20 '19 05:02

LCA


People also ask

Is .NET Core 3.1 still supported?

Starting with . NET Core 3.1, end of life dates will align with Microsoft Patch Tuesday (second Tuesday of each month). For example, . NET Core 3.1 was originally released on December 3, 2019 and is supported for three years.

Is .NET Core backwards compatible?

NET Framework 4.5 and later versions are backward-compatible with apps that were built with earlier versions of the . NET Framework. In other words, apps and components built with previous versions will work without modification on the . NET Framework 4.5 and later versions.

Is .NET Core 3.1 cross-platform?

NET Core is cross-platform. It runs on Windows, OS X and multiple distributions of Linux. It also supports different CPU architectures.


2 Answers

Official documentations and most answers for this question in stackoverflow too, have steps for upgrading .NET core versions one by one (2.0 -> 2.1 -> 2.2 -> 3.0 ->....). However since .NET Core 3.0 has ended it's life, I will give the instructions for upgrading to .NET Core 3.1(LTS) version directly from .NET Core 2.0.

1. Change Target Framework to 3.1

In project file change the TargetFramework <TargetFramework>netcoreapp2.0</TargetFramework> to <TargetFramework>netcoreapp3.1</TargetFramework>

enter image description here

2) Changes to Main

These are the changes in program.cs

public static void Main(string[] args)
        {
            //BuildWebHost(args).Run(); //Remove this in your code
            CreateWebHostBuilder(args).Build().Run(); //Add this instead
        }

        //Remove this in your code
        public static IWebHost BuildWebHost(string[] args) =>
           WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
               .Build();

        //Add this instead
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>();

The new Main replaces the call to BuildWebHost with CreateWebHostBuilder.IWebHostBuilder was added to support a new integration test infrastructure.

3) Changes to Startup.cs

3.1) AddMvc() Method

You can see a method named AddMvc(). This method has all features. So you create can any type of application (Web API, MVC, and Razor Pages). It will add extra features even though which are not required to your application which might impact the performance of your application.

   services.AddMvc(); //Remove this

Instead,

  • If you want to create a Web API application where there are no views, Add services.AddControllers()

  • If you want to work with the Razor Page application, Add services.AddRazorPages();

  • If you want to develop a Model View Controller (i.e. MVC application), Add services.AddControllersWithViews();

new methods can also be combined

3.2) Newtonsoft.Json (Json.NET) support

Newtonsoft.Json has been removed from the ASP.NET Core shared framework. now default JSON serializer for ASP.NET Core is System.Text.Json But if your application already use it, first install Microsoft.AspNetCore.Mvc.NewtonsoftJson package and then append .AddNewtonsoftJson(); to newly added MVC service registration method.

services.AddControllers()
    .AddNewtonsoftJson();

3.3) Routing startup code

Replace UseMvc with UseEndpoints

   //REMOVE
    //app.UseMvc(routes =>
    //{
    //    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
    //});
    
    //ADD
    app.UseRouting(); //If your app calls UseStaticFiles, place UseStaticFiles before UseRouting
    app.UseEndpoints(endpoints =>
    {
       endpoints.MapControllerRoute(
               name: "default",
               pattern: "{controller=Home}/{action=Index}/{id?}");
    });

Note : calls to UseAuthentication, UseAuthorization, and UseCors must appear between the calls to UseRouting and UseEndpoints to be effective

3.4) In Configure method,

replace IHostingEnvironment with IWebHostEnvironment

//public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment  env)

Add app.UseHttpsRedirection() and for non-development environments add app.UseHsts();

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseHsts();
}

 app.UseHttpsRedirection();

4 Update .NET Core SDK version in global.json

If you rely upon a global.json file to target a specific .NET Core SDK version update the version property to the 3.1 SDK version installed

{
  "sdk": {
    "version": "3.1.101"
  }
}

5) Remove obsolete package references

A large number of NuGet packages aren't produced from versions after ASP.NET Core v3.0. As a example remove package references for Microsoft.AspNetCore.All (When you comming from v2.0) or Microsoft.AspNetCore.App (When you comming from v2.1)

**

6) Update Microsoft package references

In project file update Microsoft package reference's version attribute to 3.1.0 or later. You can do this easily with Nuget package manager too. enter image description here

like image 102
Thilina Sandunsiri Avatar answered Oct 18 '22 20:10

Thilina Sandunsiri


Follow this link. This will give some guidance for your migration.

https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio

As stated in the comments, the complete path of migration from 2.0 to 3.0 would be:

  • Migrate from ASP.NET Core 2.0 to 2.1
  • Migrate from ASP.NET Core 2.1 to 2.2
  • Migrate from ASP.NET Core 2.2 to 3.0...

You will have to go through them step by step.

like image 45
Msk Avatar answered Oct 18 '22 19:10

Msk