0

Azure에서 호스팅되는 ASP.NET Core 2 응용 프로그램이 있고 Azure 포털에서 내 응용 프로그램에 대한 새 응용 프로그램 설정 MyNewSetting을 추가했습니다.컨트롤러에서 Azure AppSettings 가져 오기

컨트롤러에서 어떻게 해당 설정에 액세스합니까?

내 코드를 넣고 :

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

    services.Configure<AppSecrets>(Configuration); 
    services.AddSingleton<ITableRepositories, TableClientOperationsService>(); 
    //... 

내 컨트롤러 : 여기

public class RecordController : Controller 
{ 
    const int MyNewSetting = 7; // this one to replace with Azure Setting one 
    private readonly ITableRepositories repository; 

    public RecordController(ITableRepositories rep) { 
     repository = rep; 
    } 

, 나는 FromServices 주입을 추가 아마 필요하지만, 나는 그것이 작동하는지 모르겠습니다 ...

편집 :

Folowing @dee_zg의 대답은 다음과 같은 코드는 아마 일을 할 수있는 :

public class RecordController : Controller 
{ 
    int MyNewSetting = 7; 
    private readonly ITableRepositories repository; 

    public RecordController(ITableRepositories rep) { 
     repository = rep; 
     int myInt; 
     if (int.TryParse(System.Environment.GetEnvironmentVariable("MY_NEW_SETTING"), 
         out myInt)) { 
      MyNewSetting = myInt; 
     }; 
    } 

답변

1

당신은 선택할 중 하나 AppSettings["your-key"] 수집 또는 환경 변수로를 얻을 수 있습니다 : Environment.GetEnvironmentVariable("your-key").

거기에서 사용자 지정 IOptions에 매핑하고 필요에 따라 입력 할 수 있습니다.

+0

내 편집을 받아보실 수 있습니다. 많이 받았습니다. – Serge

0

할 수있는 일이 많습니다.

  1. 사용 Options and configuration objects

    옵션 패턴 관련 설정의 그룹을 대표하는 사용자 정의 옵션 클래스를 사용합니다. 앱 내의 각 기능에 대해 분리 된 클래스를 만드는 것이 좋습니다.

  2. IOptionsSnapshot을 사용하십시오.

    IOptionsSnapshot 구성 파일이 변경되면 구성 데이터를 다시로드 할 수 있습니다. 최소한의 오버 헤드가 있습니다. IOptionsSnapshotreloadOnChange: true과 함께 사용하면 옵션을 Configuration에 바인딩하고 변경하면 다시로드됩니다.

  3. ... 한마디로

Configuration in ASP.NET Core를 보라 (설명서 참조), 필요에 가장 적합한 시나리오를 결정하고 그것을 가지고!

희망이 도움이됩니다.

+0

질문하기 전에 해당 문서를 보았습니다. 말해봐, 내가 무엇을 사용해야하는지, 어떻게 사용하는지 모르겠다. 그래서 이것이 내가 몇 가지 코드를 추가 한 이유이다. – Serge

+0

설명서를 보셨습니까? 실제로 예제 코드가 ..... ...../ –

+0

고마워요. 예제의 코드가 제 경우에 유용했는지 묻지 않습니다. – Serge