How to use startup class inside program file on .NET 6?
Asked Answered
M

2

7

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

Meadows answered 20/11, 2022 at 8:52 Comment(4)
See this migration guide by Microsoft which explains how to deal with the changes between earlier .NET Core versions, and .NET 6, when it comes to the startup of your appVision
i make it but remining call configure function on startup classMeadows
Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory log) on startup when calling on program configure give exceptionvar startup = new Startup(builder.Configuration); startup.ConfigureServices(builder.Services); var app = builder.Build(); startup.Configure(app, app.Environment);Meadows
This c# corner might help c-sharpcorner.com/article/…Baugher
B
7

New templates use the so called minimal hosting model but nobody prevents from switching back to the generic hosting one used previously (or via WebHost).

If you want to work with top-level statements you can copy contents of Main method to the Program.cs file and then copy all other methods declared in the old Program class. New Program.cs potentially can look something like this:

await CreateHostBuilder(args)
    .Build()
    .RunAsync();

// do not forget to copy the rest of the setup if any
static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Or just remove the Startup class completely and move configure methods to corresponding parts of new file (maybe extracting some to concise extension methods).

Base answered 20/11, 2022 at 22:54 Comment(0)
C
8

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.

Cohort answered 20/11, 2022 at 9:22 Comment(10)
yes good but i face issue i can't pass logging to configure startup.Configure(app, builder.Environment);Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory log)Meadows
how to pass illoger factory as thid paramter to startup.configureMeadows
@ahmedbarbary - ILoggerFactory is a service. You can pass like FooService in the example.Cohort
this interface ILoggerFactory log is built in how to pass it pleaseMeadows
@ahmedbarbary - I updated the example to include ILoggerFactory .Cohort
yes it working but remaining three app.UseHttpsRedirection(); app.UseStaticFiles(); app.MapRazorPages(); app.Run(); where i use that on configure or where is place i put itMeadows
Or just switch back to the generic hosting model =)Base
This worked great for me in a normal web application, but I am having trouble now trying to use this startup class with my tests using WebApplicationFactory in .NET 7.0. I have a custom WebApplicationFactory and I can't understand what I need to write / override to be able to use Startup as suggested here. Can anyone please help?Bicentennial
@killswitch, In derived WebApplicationFactory, I only override ConfigureWebHost. If you need more detail, can you open a question?Cohort
@Cohort I just posted a question here: #77409251Bicentennial
B
7

New templates use the so called minimal hosting model but nobody prevents from switching back to the generic hosting one used previously (or via WebHost).

If you want to work with top-level statements you can copy contents of Main method to the Program.cs file and then copy all other methods declared in the old Program class. New Program.cs potentially can look something like this:

await CreateHostBuilder(args)
    .Build()
    .RunAsync();

// do not forget to copy the rest of the setup if any
static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Or just remove the Startup class completely and move configure methods to corresponding parts of new file (maybe extracting some to concise extension methods).

Base answered 20/11, 2022 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.