应用程序设置.Net Core

时间:2018-02-14 01:03:26

标签: asp.net-core

我正在尝试添加appsettings.json并遵循了很多教程但仍然无法做到。

我创建了appsettings.json

{
  "option1": "value1_from_json",

  "ConnectionStrings": {
    "DefaultConnection": "Server=,\\SQL2016DEV;Database=DBName;Trusted_Connection=True"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

添加我的课程:

public class MyOptions
{
    public string Option1 { get; set; }
}

public class ConnectionStringSettings
{
    public string DefaultConnection { get; set; }
}

然后在我的Startup.cs上

public IConfiguration Configuration { get; set; }

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
                        .SetBasePath(env.ContentRootPath)
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    if (env.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

和:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddScoped<IDataService<Sale>, DataService<Sale>>();

    // add My services
    // Register the IConfiguration instance which MyOptions binds against.
    services.AddOptions();

    // Load the data from the 'root' of the json file
    services.Configure<MyOptions>(Configuration);

    // load the data from the 'ConnectionStrings' section of the json file
    var connStringSettings = Configuration.GetSection("ConnectionStrings");
    services.Configure<ConnectionStringSettings>(connStringSettings); 
}

并将Dependency注入控制器构造函数。

public class ForecastApiController : Controller
{
    private IDataService<Sale> _SaleDataService;
    private readonly MyOptions _myOptions;

    public ForecastApiController(IDataService<Sale> service, IOptions<MyOptions> optionsAccessor)
    {
        _SaleDataService = service;
        _myOptions = optionsAccessor.Value;
        var valueOfOpt1 = _myOptions.Option1;
    }
}

EDITED: 问题是我得到了红色下划线的配置

 services.Configure<MyOptions>(Configuration); 

错误CS1503
参数2:无法转换为Microsoft.Extensions.Configuration.IConfiguration&#39;到&#39; System.Action Exercise.Models.MyOptions

我知道有类似的问题解释如何: ASP.NET Core MVC App Settings

但它对我不起作用

干杯

2 个答案:

答案 0 :(得分:2)

您是否包含正确的命名空间?

using Microsoft.Extensions.DependencyInjection;

你也有参考?:

Microsoft.Extensions.Options.ConfigurationExtensions

在上面的大会上我们有:

public static IServiceCollection Configure<TOptions>(this IServiceCollection services, IConfiguration config) where TOptions : class;

很可能你正在使用Microsoft.Extensions.Options程序集中的扩展方法(这是错误的)

public static IServiceCollection Configure<TOptions>(this IServiceCollection services, Action<TOptions> configureOptions) where TOptions : class;

答案 1 :(得分:0)

确保您导入了必要的所有内容并安装了所需的软件包。然后你可以做以下

services.Configure<MyOptions>(options => Configuration.GetSection("options1").Bind(options)); 

这将导致在您以编程方式更改应用程序设置时在运行时更新选项。