应用程序设置.Net Core [英] App Settings .Net Core

查看:66
本文介绍了应用程序设置.Net Core的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加一个appsettings.json并遵循了很多教程,但仍然无法做到.

I am trying to add an appsettings.json and followed a lot of tutorials and still can not do it.

我创建appsettings.json

I create 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中

then on my 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); 
}

,还将依赖项注入到控制器构造函数中.

and also injected the Dependency into the controller constructor.

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: The problem is that I get Configuration underlined in red

 services.Configure<MyOptions>(Configuration); 

错误CS1503
参数2:无法从"Microsoft.Extensions.Configuration.IConfiguration"转换为"System.Action Exercise.Models.MyOptions

Error CS1503
Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfiguration' to 'System.Action Exercise.Models.MyOptions

我知道有类似的问题说明如何: ASP.NET Core MVC应用设置

I know there are similar questions explaining how to: ASP.NET Core MVC App Settings

但这对我不起作用

欢呼

推荐答案

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

Did you include the correct namespace?

using Microsoft.Extensions.DependencyInjection;

您还有参考吗?

Microsoft.Extensions.Options.ConfigurationExtensions

在上面的程序集中,我们有:

In above Assembly we have:

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

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

Most probably you are using the extension method from Microsoft.Extensions.Options assembly (which is wrong)

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

这篇关于应用程序设置.Net Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆