English 中文(简体)
C# DI集装箱载荷没有错误,但未经JSON config文档读
原标题:C# DI container loading without errors but not reading from JSON config file
  • 时间:2023-12-28 21:57:03
  •  标签:
  • c#
  • .net

我有以下C#.NET/Visualroom项目目录结构:

C:Usersmyuser
eposmyappMyApp
    bin/
    obj/
    MyConfig.cs
    myconfig.json
    Program.cs

www.un.org/Depts/DGACM/index_spanish.htm

{
  "SerialKey": "12345"
}

在<代码>MyConfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class MyConfig
{
    public string SerialKey { get; set; }
}

在<代码>Program.cs上:

namespace MyAppNamespace
{
    class Program
    {
        static void Main(string[] args)
        {

            IConfiguration configuration = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("myconfig.json", optional: true, reloadOnChange: true)
                .Build();

            // load app settings configs
            var serviceProvider = new ServiceCollection()
                .Configure<MyConfig>(options => configuration.GetSection("MyConfig").Bind(options))
                .BuildServiceProvider();

            var myConfig = serviceProvider.GetRequiredService<IOptions<MyConfig>>().Value;
            
            Console.WriteLine("serial key is: " + myConfig.SerialKey)

            // do something
            DoSomething();

        }
    }
}

如果发生这种情况,我就没有错误,但myConfig.SerialKey Field is null(我可以通过确定一个破碎点和在Debugger方式上运行来核实)。 因此,我的法典要么是不正确的,要么我不把JSON的档案放在正确的地点。 http://www.ohchr.org。

如果需要,请列入任何包裹的Console指挥部(dotnet Add.)。

问题回答

页: 1

var configuration = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("myconfig.json", optional: true, reloadOnChange: true)
                .Build();
var myconfig = configuration.Get<MyConfig>();
Console.WriteLine(myconfig.SerialKey);

myconfig.json (path:...inDebug et8.0)

{
  "SerialKey": "12345"
}

Test output
enter image description here





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签