This are the findings of my test:
Redirecting to M.E.DependencyInjection works, but has an important fallacy: It creates its own IServiceProvider instance, which will break our neck when used in combination with Consolonia/Avalonia.
Sucks hard, because it requires an ILoggerFactory instance when using the
UseMicrosoftExtensionsLoggingWithWrappingFullLogger extension method. It
should use the following code instead:
AppLocator.CurrentMutable.RegisterLazySingleton<ILogManager>(() =>
{
var loggerFactory = AppLocator.Current.GetService<ILoggerFactory>() ?? throw new InvalidOperationException();
return new FuncLogManager(type =>
{
var actualLogger = loggerFactory.CreateLogger(type.ToString());
var miniLoggingWrapper = new MicrosoftExtensionsLoggingLogger(actualLogger);
return new WrappingFullLogger(miniLoggingWrapper);
});
});The code above pulls the ILoggerFactory from the DI container.
Consolonia requires a custom lifetime, which can only be set during a "Setup" operation, which in turn initializes the whole Avalonia system, which - in combination with ReactiveUI - will break our neck later.
During the initialization of ReactiveUI for Avalonia, which requires the new RxAppBuilder, several services are registered, which has several implications as can be read in the "Results" section.
The combination is unusable due to several requirements which make proper integration of M.E.Hosting impossible:
- RxAppBuilder is only called during Avalonia Setup
- DI container must not be read-only yet for ReactiveUI services to be added
- Avalonia/Consolonia setup causes instantiation of App class
- The instantiation of the App class causes the Instanatiation of MainWindow und this MainViewModel
- The instantiation uses Splat DI
- The instantiation with Splat DI creates a temporary IServiceProvider
- This results in a different IServiceProvider for the App/MainWindow/MainViewModel classes than the IServiceProvider of IHost
- This in turn results in different singleton services for App/MainWindow/MainViewModel and IHost.Services