Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

c# - How can I add a custom JSON file into IConfiguration?

I'm using asp.net + Autofac.

I'm trying to load a custom JSON configuration file, and either create/instance an IConfiguration instance based on that, or at least include my file into whatever IConfiguration asp.net builds by default.

My problem is that asp.net seems to override the dependency registration for IConfiguration.

I can't seem to register my own IConfiguration object - the DI container will always resolve this to some instance that seems to have been generated by the asp.net library itself.

And I'm not sure how I can get asp.net to at least load my custom config file additionally - i.e. if there is any way to get a hold of the ConfigurationBuilder it uses, and add my own file before it is building the IConfiguration object.

I've tried the following:

public class Startup
{
    public IConfigurationRoot Configuration { get; }

    public Startup(IHostingEnvironment env)
    {
        this.Configuration = new ConfigurationBuilder()
            .SetBasePath(path)
            .AddJsonFile("somefile.json")
            .Build();
    }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services
          .AddMvc()
          .AddControllersAsServices();

         var builder = new ContainerBuilder();
         builder.Register(x => this.Configuration).As<IConfiguration>();
         builder.Populate(services);

         var container = builder.Build();

         // This will return another IConfiguration instance then the one I registered; 
         // namely One that contains 1 provider, a MemoryConfigurationProvider
         var xxx = container.Resolve<IConfiguration>();

         return new AutofacServiceProvider(container);
    }
}

How can I get asp.net to load my custom JSON config file as well?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For .Net Core 2.2, you need to modify Program.cs:

Before

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
           .UseStartup<Startup>();

After

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)

            //This is how you attach additional JSON files
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.AddJsonFile("customSettings.json", optional: false, reloadOnChange: false);
            })
            //End of update
            .UseStartup<Startup>();

For the latest amendments and to add other kinds of custom settings, please refer to Microsoft documentation at the following article.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...