Home > front end >  IHostBuilder configuration cannot load xml files
IHostBuilder configuration cannot load xml files

Time:02-11

I have a co-worker who have a problem with posting here, he will be the one answering comments and validating answer. Here is his question:

I'm using Net5 with Microsoft.Extensions.Hosting. Instead of appsettings.json, I need to use xml files. I have App.config to specify common app settings (log4net config, etc.) and AcquisitionManagerConfiguration.xml for the settings on each installation (hardware stuff).

I went like this : Program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            IHostBuilder ihb = Host.CreateDefaultBuilder(args);
            ihb.ConfigureAppConfiguration((hostingContext, configuration) =>
            {
                
                //configuration.Sources.Clear();
                configuration.SetBasePath(AppContext.BaseDirectory);
                configuration.AddXmlFile("AcquisitionManagerConfiguration.xml", optional: false, reloadOnChange: true)
                   .AddXmlFile("App.config", optional: false, reloadOnChange: true);

                configuration.AddEnvironmentVariables();

            }).ConfigureServices((hostingContext, services) =>
            {
               
                services.AddHttpClient();               
                services.Configure<AcquisitionManagerConfiguration>(hostingContext.Configuration.GetSection("AcquisitionManagerConfiguration"));
                services.AddTransient<AcquisitionManager.AcquisitionManager>();
                services.AddTransient<IServiceClient, ServiceClient>();
                services.AddHostedService<Worker>();
            });
            return ihb;
        }
    }

AcquisitionManager.cs

public AcquisitionManager(IServiceClient serviceClient, IOptions<AcquisitionManagerConfiguration> config)
{
client = serviceClient;
configuration = config.Value;
}

and the first xml

    <?xml version="1.0" encoding="utf-8" ?>
    <AcquisitionManagerConfiguration>
        <val1>1</val>
    </AcquisitionManagerConfiguration>

AcquisitionManagerConfiguration.cs matches exactly the xml, it worked fine if I manually deserialize.

But using the hosting system, I never have anything in config.Value.

Any idea what I'm doing wrong ? I didn't find the solution so I posted here. Thanks for the help.

CodePudding user response:

It looks like you are just missing the Bind part in your services registration.

services.Configure<AcquisitionManagerConfiguration>(options => Configuration.GetSection("AcquisitionManagerConfiguration"));

should be

services.Configure<AcquisitionManagerConfiguration>(options => Configuration.GetSection("AcquisitionManagerConfiguration").Bind(options));

CodePudding user response:

The problem come from the xml structure:

As this is application configuration the structure from this:

    <?xml version="1.0" encoding="utf-8" ?>
    <AcquisitionManagerConfiguration>
        <val1>1</val>
    </AcquisitionManagerConfiguration>

become this:

    <?xml version="1.0" encoding="utf-8" ?>
    <Configuration>
        <AcquisitionManagerConfiguration>
            <val1>1</val>
        </AcquisitionManagerConfiguration>
    </Configuration>
  • Related