Home > Software design >  How to use startup class inside program file on .net 6?
How to use startup class inside program file on .net 6?

Time:11-20

I working on an ASP.NET Core 2.2 web application. I have some issues when upgrade my application to .NET 6.

My issue is that there's no startup class in .NET 6.0 and I found program.cs file only.

I add startup class on my web application but I don't know how to use it inside Program.cs.

How to add or use startup class inside my program.cs?

This is the startup.cs file in .NET Core 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 this is my .NET 6 program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
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.MapRazorPages();

app.Run();

How to use the startup class inside program.cs class ?

Updated Post every thing is working but configure service not working because i don't know how to implement ILoggerFactory on startup

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory log)
        {
        }

on program.cs

startup.Configure(app, app.Environment,???);

How to add logger factory as third paramter on program.cs ILoggerFactory is buit in class Updated it solved using

var app = builder.Build();
startup.Configure(
    app,
    builder.Environment,
    app.Services.GetRequiredService<FooService>(),
    app.Services.GetRequiredService<ILoggerFactory>()
);

can you please tell me how to apply swagger ui to check my api

CodePudding user response:

You can manually instantiate the Startup and manually call the method ConfigureServices and Configure :

var builder = WebApplication.CreateBuilder(args);

var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);

var app = builder.Build();
startup.Configure(app, builder.Environment);

In ASP.NET Core 2.*, Startup.Configure accepted injected service :

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddSingleton<IFooService, FooService>();
    }

    public void Configure(WebApplication app, IWebHostEnvironment env, IFooService fooService, ILoggerFactory loggerFactory)
    {

       fooService.Init();
       ...
    }
}

Then you can :

var app = builder.Build();
startup.Configure(
    app,
    builder.Environment,
    app.Services.GetRequiredService<FooService>(),
    app.Services.GetRequiredService<ILoggerFactory>()
);

When I migrated my APIs, first I consider to reuse the Startup class... but finally I moved the configuration in extension methods.

  • Related