This is the 12th day of my participation in Gwen Challenge

With the continuous expansion and update of technology, the technology we use is also constantly upgraded and optimized, and the framework of the project is also constantly upgraded. This time, WE will explain the matters needing attention when upgrading.NET Core 2.1 to 3.1.

When the project framework is upgraded, all Nuget references will change accordingly. These can be upgraded according to the technology used by the framework. I will not go into too much detail here.

The second is to modify the program. cs file. Here, the code before and after modification is uniformly posted for adjustment. The code is as follows:

2.1 Version of the file code:

using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using System; namespace S2_Xxxx_XxxNetApi { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); Console.WriteLine(" Interface started successfully "); // QuartzHelper.ExecuteInterval<Test>(200); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) http:// * : / /. UseUrls (" 5000 ") / / release need comments. UseStartup < Startup > (); }}Copy the code

3.1 version of the file code

using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; using System; Namespace S2_Cggc_PmsNetApi {/// <summary> /// startup // </summary> public class Program {/// <summary> /// startup /// </summary> public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); Console.WriteLine(" Interface started successfully "); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }}Copy the code

Then there is the Startup file, which looks like this:

Version 2.1 File code:

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Quartz; using Quartz.Impl; using S2_Xxxx_XxxNetApi; using System.Linq; namespace S2_Xxxx_XxxNetApi { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); AddSingleton<ISchedulerFactory, StdSchedulerFactory>(); // Register an instance of ISchedulerFactory. string conn = Configuration.GetSection("AppSettings:ConnectString").Value; MySqlHelper.Conn = conn; string orclconn = Configuration.GetSection("AppSettings:OrclConnectString").Value; OracleHelper.connectionString = orclconn; string redisconn = Configuration.GetSection("AppSettings:RedisConnectString").Value; RedisHelper.SetCon(redisconn); RedisHelper.BuildCache(); services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); AddCors(options => {options.AddPolicy("CorsPolicy", corsBuild => corsBuild.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.Configure<CookiePolicyOptions>(options => { options.CheckConsentNeeded = context => false; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddDistributedMemoryCache(); // services.addSession (); services.AddSession(options => { options.Cookie.Name = ".AdventureWorks.Session"; options.IdleTimeout = System.TimeSpan.FromSeconds(1200); // Set session expiration time options.cookie. HttpOnly = true; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Version = "v1", Title = "interface document"}); options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); //app.usehets(); } app.UseSession(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseSwagger(); App.useswaggerui (c => {c. waggerEndpoint("/swagger/v1/swagger. Json ", "WebAPI Documentation "); }); // Cross-domain support app.usecors ("CorsPolicy"); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id? } "); }); }}}Copy the code

3.1 Version file code:

using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Quartz; using Quartz.Impl; using System; Namespace S2_Cggc_PmsNetApi {/// <summary> /// Startup // </summary> public class Startup {/// <summary> /// Startup // </summary> public Startup(IConfiguration configuration) { Configuration = configuration; } /// </summary> /// start /// </summary> public IConfiguration Configuration {get; } /// <summary> // start /// </summary> public void ConfigureServices(IServiceCollection Services) {// Memory must be added before enabling session services.AddDistributedMemoryCache(); services.AddSession(options => { options.Cookie.Name = ".AdventureWorks.Session"; options.IdleTimeout = System.TimeSpan.FromSeconds(1200); // Set session expiration time options.cookie. HttpOnly = true; }); services.Configure<CookiePolicyOptions>(options => { options.CheckConsentNeeded = context => false; / / here will change to false, the default is true, true, when the session is invalid options. MinimumSameSitePolicy = SameSiteMode. None; }); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(); services.AddCors(options => { options.AddPolicy("any", builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }); }); AddSingleton<ApiResultFilterAttribute>(); services.AddSingleton<ApiExceptionFilterAttribute>(); services.AddMvc( config => { config.EnableEndpointRouting = false; config.Filters.AddService(typeof(ApiResultFilterAttribute)); config.Filters.AddService(typeof(ApiExceptionFilterAttribute)); }) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) .AddNewtonsoftJson(); //services.AddMvc(options => { options.Filters.Add<ResultFilterAttribute>(); }); services.AddSwaggerDocument(); AddSingleton<IActionContextAccessor, ActionContextAccessor>(); AddSingleton<ISchedulerFactory, StdSchedulerFactory>(); // Register an instance of ISchedulerFactory. QuartzHelper.AddJobForSeconds<Deduction>(); QuartzHelper.Start(); } /// </summary> /// </summary> public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseSession(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseCors("any"); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id? } "); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id? } "); }); app.UseOpenApi(); // Add swagger to generate API file (default routing file /swagger/v1/swagger.json) app.usesWaggerUI3 (); // Add Swagger UI to request pipeline (default route: / Swagger).app.usehttpsredirection (); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id? } "); }); }}}Copy the code

After the corresponding adjustment, check whether there are errors after the compilation, you can upgrade successfully, there may be some controller inside the property writing method will be some changes, this has not been studied in detail, welcome friends to supplement, I will accept your comments modestly, thank you ~

Cease to struggle and you cease to live! As long as you believe, as long as you insist, as long as you really love with your life, it must be a natural mission, that is what a person should adhere to and strive for, no matter what the dream is, no matter how tortuous and far away the road is, as long as it is the deep love of the soul, it will always adhere to the stage of their own!