.NET Core 2.0에서 ConfigurationManager.AppSettings를 사용할 수 있나요?
다음과 같이 내 구성 파일에서 설정을 읽는 방법이 있습니다.
var value = ConfigurationManager.AppSettings[key];
.NET Standard 2.0만을 대상으로 할 때 잘 컴파일됩니다.
이제 여러 대상이 필요하므로 프로젝트 파일을 다음으로 업데이트했습니다.
<TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks>
그러나 이제 netcoreapp2.0
다음 오류 메시지와 함께 컴파일이 실패 합니다.
Error CS0103 The name 'ConfigurationManager' does not exist in the current context (netcoreapp2.0)
별도로 새 .NET Core 2.0 콘솔 애플리케이션을 만들었지 만 (이번에는 .NET Core 2.0 만 대상으로 함) 마찬가지로 ConfigurationManager
네임 스페이스 아래에 없는 것 같습니다 System.Configuration
.
.NET Standard 2.0에서 사용할 수 있기 때문에 혼란 스럽기 때문에 .NET Core 2.0은 .NET Standard 2.0과 호환되므로 .NET Core 2.0에서 사용할 수있을 것으로 예상합니다.
내가 무엇을 놓치고 있습니까?
예, ConfigurationManager.AppSettings
NuGet 패키지를 참조한 후 .NET Core 2.0에서 사용할 수 있습니다 System.Configuration.ConfigurationManager
.
저에게 해결책을 주신 @JeroenMostert의 크레딧이 있습니다.
패키지 설정이 완료되면 app.config 또는 web.config를 만들고 다음과 같은 항목을 추가해야합니다.
<configuration>
<appSettings>
<add key="key" value="value"/>
</appSettings>
</configuration>
내가 설치 System.Configuration.ConfigurationManager
내 그물 코어 2.2 응용 프로그램에 Nuget에서.
그런 다음 참조 using System.Configuration;
다음으로
WebConfigurationManager.AppSettings
to ..
ConfigurationManager.AppSettings
지금까지는 이것이 옳다고 믿습니다. 4.5.0 is typical with .net core 2.2
최신 지침은 다음과 같습니다. ( https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables에서 )
사용하다:
System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
문서에서 :
public static class EnvironmentVariablesExample
{
[FunctionName("GetEnvironmentVariables")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
log.LogInformation(GetEnvironmentVariable("AzureWebJobsStorage"));
log.LogInformation(GetEnvironmentVariable("WEBSITE_SITE_NAME"));
}
public static string GetEnvironmentVariable(string name)
{
return name + ": " +
System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}
}
App settings can be read from environment variables both when developing locally and when running in Azure. When developing locally, app settings come from the
Values
collection in the local.settings.json file. In both environments, local and Azure,GetEnvironmentVariable("<app setting name>")
retrieves the value of the named app setting. For instance, when you're running locally, "My Site Name" would be returned if your local.settings.json file contains{ "Values": { "WEBSITE_SITE_NAME": "My Site Name" } }
.The System.Configuration.ConfigurationManager.AppSettings property is an alternative API for getting app setting values, but we recommend that you use
GetEnvironmentVariable
as shown here.
You can use Configuration to resolve this.
Ex (Startup.cs):
You can pass by DI to the controllers after this implementation.
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var microserviceName = Configuration["microserviceName"];
services.AddSingleton(Configuration);
...
}
'developer tip' 카테고리의 다른 글
rails content_tag 도우미와 함께 html5 데이터 속성을 사용하는 가장 좋은 방법은 무엇입니까? (0) | 2020.08.23 |
---|---|
Devise Secret Key가 설정되지 않았습니다. (0) | 2020.08.23 |
JUnit 4 테스트 스위트 (0) | 2020.08.23 |
OS X 10.7.3에서 디렉토리를 복사하는 방법은 무엇입니까? (0) | 2020.08.23 |
요청 된 리소스는 HTTP 메서드 'GET'을 지원하지 않습니다. (0) | 2020.08.23 |