相关疑难解决方法(0)

如何在.net core 2上启动时访问IConfigurationRoot?

我已经用ConfigurationProvider实体框架编写了一个自定义.因为我也希望在运行时使其可更新,所以我创建了一个IWritableableOption.

我需要在更新后刷新配置.这可以通过IConfigurationRoot.Reload.

但是,我如何获得IConfigurationRoot.net核心2?

我发现,在以前的版本中IConfigurationRoot,它是启动的一部分.但是在.net核心2中,我们只有更简单的类型IConfiguration:

public Startup(IConfiguration configuration)
{
    // I tried to change this to IConfigurationRoot,
    // but this results in an unresolved dependency error
    Configuration = configuration;
}

public IConfiguration Configuration { get; }
Run Code Online (Sandbox Code Playgroud)

我也发现了,我可以使用自己的实例

WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration(context, builder) => {
    var configurationRoot = builder.build()
})
Run Code Online (Sandbox Code Playgroud)

但我想更新Startup使用的配置.

那么我怎样才能将其IConfigurationRoot用于Startup将其注入我的服务集合中?

c# asp.net-core-mvc asp.net-core asp.net-core-2.0

8
推荐指数
2
解决办法
4968
查看次数

将appsettings.json保存在.net core 3.1中

我想要我的 .net core 3.1 winforms 项目中有一个配置文件。我有以下工作来读取文件

using Microsoft.Extensions.Configuration;
using System;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace FeedRead
{

    public partial class Form1 : Form
    {

        private ConfigurationBuilder configuration;
        public Form1()
        {
            configuration = new ConfigurationBuilder();
            configuration.SetBasePath(System.IO.Directory.GetCurrentDirectory());
            configuration.AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true);
            configuration.Build();
        }
Run Code Online (Sandbox Code Playgroud)

但是当我进行更改后如何保存配置呢?

我尝试了以下操作,但不知道如何完成。

    private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
    {

        configuration.Properties.Add("col1Width", listView1.Columns[0].Width);
        var extn = configuration as Microsoft.Extensions.Configuration.IConfigurationBuilder; // not sure about
        var provider = extn.GetFileProvider();
        var info = provider.GetFileInfo(subpath: "appsettings.json"); // not sure about
        // …
Run Code Online (Sandbox Code Playgroud)

c# .net-core

6
推荐指数
1
解决办法
9840
查看次数

将 IConfigurationRoot 部分的更改保存到 .net Core 2.2 中的 *.json 文件

我一直在寻找解决方案,但没有设法找到它,我敢打赌有人遇到过这个问题,那么问题是什么?。

为了测试,我创建了简单的控制台应用程序(解决方案将在 asp.net core web api 中使用)。

我已经设置了“始终复制”的 TestSetting.json 配置文件。

{
  "setting1" : "value1" 
}
Run Code Online (Sandbox Code Playgroud)

和简单的代码

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

IConfigurationRoot configuration = configurationBuilder.AddJsonFile("TestSettings.json",false, reloadOnChange: true).Build();

Console.WriteLine(configuration.GetSection("setting1").Value); // Output: value1
//Change configuration manually in file while console is waiting
Console.ReadKey();

//Changed manually in file Value appears
Console.WriteLine(configuration.GetSection("setting1").Value); // Output: Whatever you have setuped
Console.ReadKey();

configuration.GetSection("setting1").Value = "changed from code";
//Changed in code value appear
Console.WriteLine(configuration.GetSection("setting1").Value); // Output: changed from code
Run Code Online (Sandbox Code Playgroud)

我有 2 个要求,我希望可以在应用程序运行时手动更改 json 配置文件中的值,并且应用程序将在下次获取设置部分时看到更新的值并且它正在工作。

第二个要求是,我想保留一些信息,准确地说是任务的最后执行时间,应该在每个设置的时间段执行一次,例如。每天一次,所以一些循环检查上次执行时间值并确定是否必须执行操作。有人会问我有什么可以工作,但我还需要涵盖执行操作和重新启动应用程序时的场景(服务器错误、用户重新启动等),我需要以一种允许我的方式保存这些信息在应用程序启动后阅读它。

阅读代码示例我们可以看到,在代码中更改 setting1 后,我们看到该部分在尝试将其输出到控制台时已更改。 …

c# asp.net-web-api .net-core

2
推荐指数
2
解决办法
4419
查看次数