在控制器中获取 Azure AppSettings
Get Azure AppSettings in a Controller
我有一个 ASP.NET Core 2 应用程序托管在 Azure 上,我在 Azure 门户中为我的应用程序添加了一个新的应用程序设置 MyNewSetting
。
如何从控制器访问该设置?
我的代码如下:
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
注入,但我不确定它是否有效...
编辑:
在@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;
};
}
您可以选择从 AppSettings["your-key"]
集合中获取它们或作为环境变量获取它们:Environment.GetEnvironmentVariable("your-key")
.
从那里您可以将它们映射到您的自定义 IOptions 并在您需要它们的任何地方注入。
您可以做很多事情。
- 使用Options and configuration objects
The options pattern uses custom options classes to represent a group of related settings. We recommended that you create decoupled classes for each feature within your app.
- 使用 IOptionsSnapshot。
IOptionsSnapshot
supports reloading configuration data when the configuration file has changed. It has minimal overhead. Using IOptionsSnapshot
with reloadOnChange: true
, the options are bound to Configuration
and reloaded when changed.
- ...(参见文档)
简而言之,看看Configuration in ASP.NET Core,确定最适合您需求的场景并尝试一下!
希望对您有所帮助。
我有一个 ASP.NET Core 2 应用程序托管在 Azure 上,我在 Azure 门户中为我的应用程序添加了一个新的应用程序设置 MyNewSetting
。
如何从控制器访问该设置?
我的代码如下:
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
注入,但我不确定它是否有效...
编辑:
在@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;
};
}
您可以选择从 AppSettings["your-key"]
集合中获取它们或作为环境变量获取它们:Environment.GetEnvironmentVariable("your-key")
.
从那里您可以将它们映射到您的自定义 IOptions 并在您需要它们的任何地方注入。
您可以做很多事情。
- 使用Options and configuration objects
The options pattern uses custom options classes to represent a group of related settings. We recommended that you create decoupled classes for each feature within your app.
- 使用 IOptionsSnapshot。
IOptionsSnapshot
supports reloading configuration data when the configuration file has changed. It has minimal overhead. UsingIOptionsSnapshot
withreloadOnChange: true
, the options are bound toConfiguration
and reloaded when changed. - ...(参见文档)
简而言之,看看Configuration in ASP.NET Core,确定最适合您需求的场景并尝试一下!
希望对您有所帮助。