如何在.NET 6中使用Program.cs文件

rvpgvaaj  于 2023-05-19  发布在  .NET
关注(0)|答案(1)|浏览(251)

在最新版本的.Net中,Startup类和Program类合并在一起,using和其他语句被简化并从Program.cs中删除。我真的很困惑如何在一个文件中使用它。请帮助我将所有服务迁移到program.cs文件。

启动.cs文件:

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

程序.cs文件:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
6ojccjat

6ojccjat1#

修改您的csproj文件

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
</PropertyGroup>

相关问题