How to move code from startup to program wehn migrate from .NET core 2.2 to .NET core 6 ?

ahmed salah 3,216 Reputation points
2022-11-20T05:11:43.297+00:00

i work on .net core 2.2 web application

i found class name start up

now i need to migrate from .net core 2.2 to .net core 6

so i don't found start up

i found only program class only

so how to add my code Exist on start up to program on .net 6 .

.net 2.2

public class Startup  
    {  
        private readonly IConfigurationRoot configRoot;  
        private AppSettings AppSettings { get; set; }  
  
        public Startup(IConfiguration configuration)  
        {  
            Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();  
            Configuration = configuration;  
  
            IConfigurationBuilder builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");  
            configRoot = builder.Build();  
  
            AppSettings = new AppSettings();  
            Configuration.Bind(AppSettings);  
        }  
  
        public IConfiguration Configuration { get; }  
  
        public void ConfigureServices(IServiceCollection services)  
        {  
            services.AddController();  
  
            services.AddDbContext(Configuration, configRoot);  
  
            services.AddIdentityService(Configuration);  
  
            services.AddAutoMapper();  
  
            services.AddScopedServices();  
  
            services.AddTransientServices();  
  
            services.AddSwaggerOpenAPI();  
  
            services.AddMailSetting(Configuration);  
  
            services.AddServiceLayer();  
  
            services.AddVersion();  
  
            services.AddHealthCheck(AppSettings, Configuration);  
  
            services.AddFeatureManagement();  
        }  
  
  
  
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory log)  
        {  
            if (env.IsDevelopment())  
            {  
                app.UseDeveloperExceptionPage();  
            }  
  
            app.UseCors(options =>  
                 options.WithOrigins("http://localhost:3000")  
                 .AllowAnyHeader()  
                 .AllowAnyMethod());  
  
            app.ConfigureCustomExceptionMiddleware();  
  
            log.AddSerilog();  
  
            //app.ConfigureHealthCheck();  
  
  
            app.UseRouting();  
  
            app.UseAuthentication();  
  
            app.UseAuthorization();  
            app.ConfigureSwagger();  
            app.UseHealthChecks("/healthz", new HealthCheckOptions  
            {  
                Predicate = _ => true,  
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,  
                ResultStatusCodes =  
                {  
                    [HealthStatus.Healthy] = StatusCodes.Status200OK,  
                    [HealthStatus.Degraded] = StatusCodes.Status500InternalServerError,  
                    [HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,  
                },  
            }).UseHealthChecksUI(setup =>  
              {  
                  setup.ApiPath = "/healthcheck";  
                  setup.UIPath = "/healthcheck-ui";  
                  //setup.AddCustomStylesheet("Customization/custom.css");  
              });  
  
  
            app.UseEndpoints(endpoints =>  
            {  
                endpoints.MapControllers();  
            });  
        }  
    }  
  
  
  
   
  

  
and his code as following  

peogram.cs .net core 6

using HealthChecks.UI.Client;  
using Microsoft.AspNetCore.Diagnostics.HealthChecks;  
using Microsoft.EntityFrameworkCore.Infrastructure;  
using Microsoft.Extensions.Diagnostics.HealthChecks;  
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;  
//using static Microsoft.EntityFrameworkCore.DbLoggerCategory;  
  
  
var builder = WebApplication.CreateBuilder(args);  
  
// Add services to the container.  
builder.Services.AddRazorPages();  
var app = builder.Build();  
  
  
if (!app.Environment.IsDevelopment())  
{  
    app.UseExceptionHandler("/Error");  
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.  
    app.UseHsts();  
}  
  
app.UseHttpsRedirection();  
app.UseStaticFiles();  
app.UseRouting();  
  
app.UseAuthorization();  
  
  
  
  
app.UseEndpoints(endpoints =>  
{  
    endpoints.MapControllers();  
});  
  
app.MapRazorPages();  
  
app.Run();  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,132 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,180 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,626 Reputation points
    2022-11-20T18:40:00.82+00:00

    Move services.* to after line 12 before line 13 (use builder.Services instead of services) The config where you need it (builder.Configuration)
    .

    Then the required middleware registration in the proper order. You should read the updates as much of the middleware has changed, especially endpoint routing.


  2. JasonPan - MSFT 4,201 Reputation points Microsoft Vendor
    2022-11-21T07:58:35.15+00:00

    Hi @ahmed salah

    Our advice is to follow the official documentation and migrate step by step.

    From
    2.2 -> 3.0
    3.0 -> 3.1
    3.1 -> 6.0 LTS

    If there are other questions, please let me know.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards
    Jason

    0 comments No comments