应用程序设置.Net Core

编程入门 行业动态 更新时间:2024-10-28 10:35:42
本文介绍了应用程序设置.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

本文发布于:2023-11-13 23:06:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Net   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!