Step by step migration of ASP.NET Core 2.2 to 3.1

In order to upgrade/migrate asp.net core version to 3.1 following steps to be perform.


  • Download and install Visual Studio 2019 version 16.4 or higher.
  • Download and install .NET Core 3.1  https://dotnet.microsoft.com/download/dotnet-core/3.1
  • Upgrade the projects of solution to .NET Core 3.1 , because ASP.NET Core 3.1 requires it.
  • Update existing Nuget packages to a version compatible with ASP.NET Core 3.1 in each project
  • Use IHost interface from a IHostBuilder instead of building and running a IWebHost from a IWebHostBuilder in Program.cs
  public static IHostBuilder CreateWebHostBuilder(string[] args) =>  
           Host.CreateDefaultBuilder(args)  
           .ConfigureWebHostDefaults(webBuilder =>  
           {  
             webBuilder.UseKestrel()  
                  .UseSerilog()  
                  .UseStartup<Startup>();  
           })  
  • Change AddMvc method has been replaced by AddControllers in Startup.cs of each projects.
  • In Startup.cs, UseMvc method to be replaced by UseRouting method and UseEndpoints method. Note that these two methods must not be nested if you are using Authentication and Authorization. UseRouting method must be set before UseAuthentication and UseAuthorization methods. UseEndpoints method has to be set at the end.
  • IHostingEnvironment to be replaced by IHostEnvironment in all projects 
  • Replace UseSignalR with UseEndpoint method in Startup.cs
 public class Startup  
   {  
     public Startup(IConfiguration configuration)  
     {  
       Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();  
       Configuration = configuration;  
     }  
     public IConfiguration Configuration { get; }  
     public void ConfigureServices(IServiceCollection services)  
     {  
       services.AddControllers().AddNewtonsoftJson(options =>   
       {  
         options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;  
         options.SerializerSettings.ContractResolver = new DefaultContractResolver();  
       }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0)  
       .AddFluentValidation();  
        services.AddSignalR();  
       services.AddHttpClient<IDataClient, DataClient>(client =>  
       {  
         client.BaseAddress = new Uri("http://localhost:4444/api/");  
       })  
       .AddPolicyHandlers(policyConfig);  
       services.AddApplicationInsightsTelemetry();  
     }  
     public void Configure(IApplicationBuilder app, IHostEnvironment env)  
     {  
       if (env.IsDevelopment())  
       {  
         app.UseDeveloperExceptionPage();  
       }  
       app.UseRouting();  
         app.UseEndpoints(endpoints =>  
                  {     
                   endpoints.MapHub<MyHub>("/myHub");  
                 });  
       // Needs to be added after UseRouting() in 3.1  
       // app.UseAuthorization();   
       app.UseEndpoints(endpoints =>  
       {  
         endpoints.MapDefaultControllerRoute();  
       });  
     }  
   }  

Comments

Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Restore the lost focus of Auto post back controls in asp.net update Panel control

Disable backspace key using Jquery