development

ConfigureServices에서 개발 / 스테이징 / 생산 호스팅 환경을 얻는 방법

big-blog 2020. 7. 5. 07:36
반응형

ConfigureServices에서 개발 / 스테이징 / 생산 호스팅 환경을 얻는 방법


시작 방법에서 개발 / 스테이징 / 생산 호스팅 환경을 얻으려면 어떻게해야 ConfigureServices합니까?

public void ConfigureServices(IServiceCollection services)
{
    // Which environment are we running under?
}

ConfigureServices방법은 단일 IServiceCollection매개 변수 만 사용합니다 .


ConfigureServices에서 쉽게 액세스 할 수 있으며, 시작 메소드를 호출하여 전달 된 속성에 유지하면 ConfigureServices에서 특성에 액세스 할 수 있습니다.

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
    ...your code here...
    CurrentEnvironment = env;
}

private IHostingEnvironment CurrentEnvironment{ get; set; } 

public void ConfigureServices(IServiceCollection services)
{
    string envName = CurrentEnvironment.EnvironmentName;
    ... your code here...
}

TL; DR

환경 ASPNETCORE_ENVIRONMENT이름으로 호출 된 환경 변수를 설정하십시오 (예 :) Production. 그런 다음 두 가지 중 하나를 수행하십시오.

  • 주사 IHostingEnvironmentStartup.cs, 다음 (즉,를 사용하여 env확인하기 위해 여기) env.IsEnvironment("Production"). 하지 마십시오 사용 확인 env.EnvironmentName == "Production"!
  • 별도의 Startup클래스 또는 개별 Configure/ ConfigureServices함수를 사용하십시오 . 클래스 또는 함수가 이러한 형식과 일치하면 해당 환경의 표준 옵션 대신 사용됩니다.
    • Startup{EnvironmentName}() (전체 수업) || 예:StartupProduction()
    • Configure{EnvironmentName}()|| 예:ConfigureProduction()
    • Configure{EnvironmentName}Services()|| 예:ConfigureProductionServices()

전체 설명

.NET Core 문서 는이를 수행하는 방법을 설명합니다 . ASPNETCORE_ENVIRONMENT원하는 환경으로 설정된 환경 변수를 사용하면 두 가지 중에서 선택할 수 있습니다.

환경 이름 확인

문서에서 :

IHostingEnvironment서비스는 환경 작업을위한 핵심 추상화를 제공합니다. 이 서비스는 ASP.NET 호스팅 계층에서 제공하며 Dependency Injection을 통해 시작 논리에 주입 될 수 있습니다. Visual Studio의 ASP.NET Core 웹 사이트 템플릿은이 방법을 사용하여 환경 별 구성 파일 (있는 경우)을로드하고 앱의 오류 처리 설정을 사용자 지정합니다. 두 경우 모두,이 동작은 호출 EnvironmentName하거나 적절한 메소드로 전달 된 IsEnvironment인스턴스 에서 현재 지정된 환경을 참조하여 수행됩니다 IHostingEnvironment.

참고 : 실제 값 확인 env.EnvironmentName되어 있지 권장!

응용 프로그램이 특정 환경에서 실행 중인지 확인해야하는 env.IsEnvironment("environmentname")경우 ( env.EnvironmentName == "Development"예를 들어 확인하는 대신) 대소 문자를 올바르게 무시하므로 사용 하십시오 .

별도의 수업 사용

문서에서 :

ASP.NET Core 응용 프로그램이 시작되면이 Startup클래스는 응용 프로그램을 부트 스트랩하고 구성 설정을로드하는 데 사용됩니다 ( ASP.NET 시작에 대해 자세히 알아보기 ). 그러나 이름이 지정된 클래스 Startup{EnvironmentName}(예 :)가 StartupDevelopment있고 ASPNETCORE_ENVIRONMENT환경 변수가 해당 이름과 일치하면 해당 Startup클래스가 대신 사용됩니다. 따라서 Startup개발을 위해 구성 할 수 있지만 StartupProduction프로덕션에서 앱을 실행할 때 사용되는 별도의 구성 이 있습니다. 혹은 그 반대로도.

Startup현재 환경에 따라 완전히 별개의 클래스 를 사용하는 것 외에도 클래스 내에서 응용 프로그램이 구성되는 방식을 조정할 수도 있습니다 Startup. Configure()ConfigureServices()방법은 유사 환경의 특정 버전 지원 Startup형태의 클래스 자체를, Configure{EnvironmentName}()하고 Configure{EnvironmentName}Services(). 메소드를 정의 하면 환경이 개발로 설정 될 때 ConfigureDevelopment()대신 메소드 가 호출됩니다 Configure(). 마찬가지로 같은 환경에서 ConfigureDevelopmentServices()대신 호출됩니다 ConfigureServices().


에서 .NET Core 2.0MVC 응용 프로그램 / Microsoft.AspNetCore.All@vaindil에 의해 설명 된 바와 같이 V2.0.0, 당신은 환경 특정 시작 클래스를 가질 수 있지만, 그 방법 좋아하지 않는다.

또한 삽입 할 수 IHostingEnvironmentStartUp생성자입니다. 환경 변수를 Program클래스 에 저장할 필요는 없습니다 .

public class Startup
{
    private readonly IHostingEnvironment _currentEnvironment;
    public IConfiguration Configuration { get; private set; }

    public Startup(IConfiguration configuration, IHostingEnvironment env)
    {
        _currentEnvironment = env;
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        ......

        services.AddMvc(config =>
        {
            // Requiring authenticated users on the site globally
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            config.Filters.Add(new AuthorizeFilter(policy));

            // Validate anti-forgery token globally
            config.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());

            // If it's Production, enable HTTPS
            if (_currentEnvironment.IsProduction())      // <------
            {
                config.Filters.Add(new RequireHttpsAttribute());
            }            
        });

        ......
    }
}

추가 속성이나 메서드 매개 변수없이 다음과 같이 수행 할 수 있습니다.

public void ConfigureServices(IServiceCollection services)
{
    IServiceProvider serviceProvider = services.BuildServiceProvider();
    IHostingEnvironment env = serviceProvider.GetService<IHostingEnvironment>();

    if (env.IsProduction()) DoSomethingDifferentHere();
}

IHostingEnvironment에 쉽게 액세스 할 수없는 코드베이스의 어딘가에서 이것을 테스트 해야하는 경우 다른 쉬운 방법은 다음과 같습니다.

bool isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";

호스팅 환경은 ASPHost_ENV 환경 변수에서 가져옵니다.이 변수는 시작 중 IHostingEnvironment.IsEnvironment 확장 방법을 사용하거나 IsDevelopment 또는 IsProduction의 해당 편의 방법 중 하나를 사용하여 사용할 수 있습니다. Startup () 또는 ConfigureServices 호출에 필요한 것을 저장하십시오.

var foo = Environment.GetEnvironmentVariable("ASPNET_ENV");

문서

Configure and ConfigureServices support environment specific versions of the form Configure{EnvironmentName} and Configure{EnvironmentName}Services:

You can do something like this...

public void ConfigureProductionServices(IServiceCollection services)
{
    ConfigureCommonServices(services);

    //Services only for production
    services.Configure();
}

public void ConfigureDevelopmentServices(IServiceCollection services)
{
    ConfigureCommonServices(services);

    //Services only for development
    services.Configure();
}

public void ConfigureStagingServices(IServiceCollection services)
{
    ConfigureCommonServices(services);

    //Services only for staging
    services.Configure();
}

private void ConfigureCommonServices(IServiceCollection services)
{
    //Services common to each environment
}

I wanted to get the environment in one of my services. It is really easy to do! I just inject it to the constructor like this:

    private readonly IHostingEnvironment _hostingEnvironment;

    public MyEmailService(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

Now later on in the code I can do this:

if (_hostingEnvironment.IsProduction()) {
    // really send the email.
}
else {
    // send the email to the test queue.
}

In Dotnet Core 2.0 the Startup-constructor only expects a IConfiguration-parameter.

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

How to read hosting environment there? I store it in Program-class during ConfigureAppConfiguration (use full BuildWebHost instead of WebHost.CreateDefaultBuilder):

public class Program
{
    public static IHostingEnvironment HostingEnvironment { get; set; }

    public static void Main(string[] args)
    {
        // Build web host
        var host = BuildWebHost(args);

        host.Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        return new WebHostBuilder()
            .UseConfiguration(new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("hosting.json", optional: true)
                .Build()
            )
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var env = hostingContext.HostingEnvironment;

                // Assigning the environment for use in ConfigureServices
                HostingEnvironment = env; // <---

                config
                  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

                if (env.IsDevelopment())
                {
                    var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                    if (appAssembly != null)
                    {
                        config.AddUserSecrets(appAssembly, optional: true);
                    }
                }

                config.AddEnvironmentVariables();

                if (args != null)
                {
                    config.AddCommandLine(args);
                }
            })
            .ConfigureLogging((hostingContext, builder) =>
            {
                builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                builder.AddConsole();
                builder.AddDebug();
            })
            .UseIISIntegration()
            .UseDefaultServiceProvider((context, options) =>
            {
                options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
            })
            .UseStartup<Startup>()
            .Build();
    }

Ant then reads it in ConfigureServices like this:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    var isDevelopment = Program.HostingEnvironment.IsDevelopment();
}

참고URL : https://stackoverflow.com/questions/32548948/how-to-get-the-development-staging-production-hosting-environment-in-configurese

반응형