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.
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
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
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
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:
{
"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