简体   繁体   English

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

[英]How to use Program.cs file in .NET 6

In the latest version of .Net the Startup class and Program class are merged together and the using and other statements are simplified and removed from Program.cs.在最新版本的 .Net 中,Startup 类和 Program 类合并在一起,并且 using 和其他语句被简化并从 Program.cs 中删除。 I am really confused about how to use it all in one file.我真的很困惑如何在一个文件中使用它。 Please help me to migrate all services to the program.cs file.请帮我将所有服务迁移到 program.cs 文件。

Startup.cs file : Startup.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();
        });
    }
}

Program.cs file : Program.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>();
            });
}

You can disable implicit usings in the csproj file您可以在 csproj 文件中禁用隐式使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net core 6 Program.cs文件中appsettings.json的使用方法 - How to use appsettings.json in Asp.net core 6 Program.cs file 如何在 .NET 6 Program.cs 中创建记录器 - How to create a Logger in .NET 6 Program.cs 如何在不使用启动 class 的情况下将其迁移到我的 .NET 6 Program.cs 文件中? 代码来自 .NET Core 2.1,也在 Program.cs - How do I migrate this into my .NET 6 Program.cs file without using the startup class? The code is from .NET Core 2.1, also in Program.cs Asp.Net Core 3 如何在 program.cs 文件中放入 function - 修饰符“private”对此项目无效 - Asp.Net Core 3 how to put a function in the program.cs file - The modifier 'private' is not valid for this item 如何在 .NET 6 Program.cs 类中获取 ILoggerFactory 实例 - How to get ILoggerFactory instance in .NET 6 Program.cs class 如何在 .NET 6 的 Program.cs 中处理请求管道和服务注册 - How request pipeline and service registration is handled in Program.cs in .NET 6 如何仅在 .NET 6 Program.cs 中添加配置? - How to add configuration in .NET 6 Program.cs only? 如何在 .NET 6 最小 API Program.cs 中访问 DbContext - How to access DbContext in .NET 6 minimal API Program.cs 如何返回在 Program.cs ASP.Net core 6 中找不到 - How to return not found in Program.cs ASP.Net core 6 .NET 6(稳定)Program.cs 中的 IConfiguration 设置 - .NET 6 (stable) IConfiguration setup in Program.cs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM