Scottintexas
Well-known member
Second attempt!
I am trying to inject a logger into my MainWindowViewModel.
My MainWindowViewModel constructor takes an argument of type MyLogger. But what I think is right is not working. I am getting the exception "
System.InvalidOperationException: 'No service for type 'Izod_Impact.Loggers.MyLogger' has been registered.'" But isn't that what line 6 does?
Any help would be appreciated. What I want to do is write to a log file, not Microsoft logging extension.
Thank you.
I am trying to inject a logger into my MainWindowViewModel.
C#:
/// <summary>
/// Entry point for application.
/// </summary>
/// <param name="e"></param>
protected override async void OnStartup(StartupEventArgs e)
{
await AppHost!.StartAsync();
var startupForm = AppHost.Services.GetRequiredService<MainWindowView>();
startupForm.DataContext = AppHost.Services.GetRequiredService<MainWindowViewModel>();
startupForm.Show();
base.OnStartup(e);
}
/// <summary>
/// Application default constructor.
/// </summary>
public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices((HostBuilderContext, services) =>
{
services.AddSingleton<MainWindowView>();
services.AddSingleton<IMyLogger, MyLogger>();
services.AddSingleton<MainWindowViewModel>(serviceProvider =>
{
var paramVal = serviceProvider.GetRequiredService<MyLogger>();
return new MainWindowViewModel(paramVal);
});
})
.Build();
}
My MainWindowViewModel constructor takes an argument of type MyLogger. But what I think is right is not working. I am getting the exception "
System.InvalidOperationException: 'No service for type 'Izod_Impact.Loggers.MyLogger' has been registered.'" But isn't that what line 6 does?
Any help would be appreciated. What I want to do is write to a log file, not Microsoft logging extension.
Thank you.