I´m using Autofac in my WPF application for dependency injection and can´t resolve this problem.
I have created this abstract class ListItemViewModelBase from which two classes PasswordItemViewModel and CardItemViewModel inherits.
ListItemViewModelBase.cs
public abstract class ListItemViewModelBase
{
protected readonly IMessenger _messenger;
public string Id { get; }
public string Name { get; }
public string Notes { get; }
protected ListItemViewModelBase(IMessenger messenger, string id, string name, string notes)
{
_messenger = messenger;
Id = id;
Name = name;
Notes = notes;
}
public abstract void SeeDetails();
}
PasswordItemViewModel.cs
public partial class PasswordItemViewModel : ListItemViewModelBase
{
public string UserName { get; }
public string Password { get; }
public string Website { get; }
public PasswordItemViewModel(IMessenger messenger, string id, string name, string userName, string password, string website, string notes) : base(messenger, id, name, notes)
{
UserName = userName;
Password = password;
Website = website;
}
[RelayCommand]
public override void SeeDetails()
{
_messenger.Send(new PasswordDetailMessage(this));
}
}
CardItemViewModel.cs
public partial class CardItemViewModel : ListItemViewModelBase
{
public string CardNumber { get; }
public int ExpMonth { get; }
public int ExpYear { get; }
public CardItemViewModel(IMessenger messenger, string id, string name, string cardNumber, int expMonth, int expYear, string notes) : base(messenger, id, name, notes)
{
CardNumber = cardNumber;
ExpMonth = expMonth;
ExpYear = expYear;
}
[RelayCommand]
public override void SeeDetails()
{
_messenger.Send(new CardDetailMessage(this));
}
}
My App.xaml.cs where the Autofac is configured looks like this:
App.xaml.cs
public partial class App : Application
{
public static IServiceProvider ServiceProvider { get; private set; }
[STAThread]
public static void Main(string[] args)
{
using IHost host = CreateHostBuilder(args).Build();
CreateGenericHost(host);
InitAppAndRun();
}
private static void InitAppAndRun()
{
var app = new App();
app.InitializeComponent();
app.MainWindow = ServiceProvider.GetRequiredService();
app.MainWindow!.Visibility = Visibility.Visible;
app.Run();
}
#region Host builder
private static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(ConfigureServices)
.ConfigureContainer(ConfigureAutofacBuilder);
}
private static void ConfigureAutofacBuilder(HostBuilderContext ctx, ContainerBuilder builder)
{
builder.RegisterModule>();
builder.RegisterModule>();
var config = new ConfigurationBuilder();
config.AddJsonFile("autofac.json");
var module = new ConfigurationModule(config.Build());
builder.RegisterModule(module);
}
private static void CreateGenericHost(IHost host)
{
host.Start();
ServiceProvider = host.Services;
}
private static void ConfigureServices(HostBuilderContext ctx, IServiceCollection services)
{
services.AddSingleton();
}
#endregion
}
When I try to start the application, program fails on using IHost host = CreateHostBuilder(args).Build(); and throws this error message:
Autofac.Core.Activators.Reflection.NoConstructorsFoundException: 'No accessible constructors were found for the type 'TestApp.Bases.ListItemViewModelBase'.'
Seems like Autofac can´t resolve non-public constructor of ListItemViewModelBase class. Is there a way to make Autofac resolve these type of non-public constructors?
Thanks, Tobias
Response to reply from Orenico:
Here is autofac.json which contains basically nothing.
{
"modules": [],
"components": []
}