question

ahmedsalah-1628 avatar image
0 Votes"
ahmedsalah-1628 asked Bruce-SqlWork commented

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

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();




dotnet-csharpdotnet-aspnet-core-mvcdotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thank you for reply
can you more details please
i don't understand above

0 Votes 0 ·

I find it easier to convert the application to net 6 first and get it running, before converting to min api.

so, upgrade framework version, and nuget packages. make required middleware changes.


once the app is working, then convert to mini api if you want.

0 Votes 0 ·
JasonPan-MSFT avatar image
0 Votes"
JasonPan-MSFT answered JasonPan-MSFT edited

Hi @ahmedsalah-1628

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





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.