Click here to Skip to main content
15,398,439 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello all,

I have used Autofac to write a Console application in Net6 for Windows, Ubuntu and Raspberry.

I have managed to wire up my dependencies using an external json file for Autofac as shown below.

C#
protected override void Load(ContainerBuilder builder)
{
  var currentExecutionLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  var autofacConfigurationFileName = Path.Combine(currentExecutionLocation, "AutofacConfiguration.json");
  Console.WriteLine($"* Wiring up Autofac configuration using file {autofacConfigurationFileName}");
  var config = new ConfigurationBuilder();
  config.AddJsonFile(autofacConfigurationFileName);
  var module = new ConfigurationModule(config.Build());
  builder.RegisterModule(module);}


That all worked out nicely and gave me the possibility to reconfigure the dependencies without recompiling the code.
However, for different reasons I've decided to leave Autofac and start using the .Net Core DI framework.

I removed Autofac NuGet packages and the Autofac code altogether and added some new usings

C#
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;


After doing some modifications to the code, the application runs again and I can register the dependencies programmatically using the code below
C#
public static IHost CreateHost(IConfigurationBuilder builder)
{
    WireupExternalModuleLoading();
    WireupLog4Net();
    ConfigBuilder(builder);
    var host = Host.CreateDefaultBuilder()
        .ConfigureServices((context, services) =>
        {
            services.AddSingleton<IFileLogging, FileLogging>();
            services.AddSingleton<IHardwareEngine, HardwareEngineStub>();
            services.AddSingleton<IProgramTests, ProgramTests>();
        }).Build();
    return host;
}

private static void ConfigBuilder(IConfigurationBuilder builder)
{
    builder.SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
        .AddEnvironmentVariables();
}



Now I want to have the same functionality as I had with Autofac, i.e. the possibility to load the configuration (mapping between interfaces and concrete classes) from an external json (or equivalent) file.
Basically removing the code
C#
services.AddSingleton<IFileLogging, FileLogging>();
services.AddSingleton<IHardwareEngine, HardwareEngineStub>();
services.AddSingleton<IProgramTests, ProgramTests>();

and replacing it with a json file (or equivalent) that is loaded at runtime.

What I have tried:

I thought that it would be fairly easy to find some tips on how to load the configuration from a file, but yet again my Google Fu skills have failed me.

My previous Autofac json file looked something like this:

XML
{
  "components": [
    {
      "type": "UtilitiesLibrary.Logging.FileLogging, UtilitiesLibrary",
      "services": [
        {
          "type": "UtilitiesLibrary.Logging.IFileLogging, UtilitiesLibrary"
        }
      ],
      "instanceScope": "single-instance",
      "injectProperties": true
    },
    {
      "type": "HardwareEngineStub.HardwareEngine, HardwareEngineStubLibrary",
      "services": [
        {
          "type": "AccordionDefinitionsLibrary.IHardwareEngine, AccordionDefinitionsLibrary"
        }
      ],
      "instanceScope": "single-instance",
      "injectProperties": true
    },
    {
      "type": "AccordionMainLibrary.ProgramTests, AccordionMainLibrary",
      "services": [
        {
          "type": "AccordionDefinitionsLibrary.IProgramTests, AccordionDefinitionsLibrary"
        }
      ],
      "instanceScope": "single-instance",
      "injectProperties": true
    }
  ]
}


Is someone would be kind enough to give me a pointer or push me in the right direction that would be appreciated.

Thanks in advance,
King regards
Magnus
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900