[英]Make Autofac resolve non-public constructors
我在我的 WPF 應用程序中使用 Autofac 進行依賴注入,但無法解決此問題。
我已經創建了這個抽象的 class ListItemViewModelBase
兩個類PasswordItemViewModel
和CardItemViewModel
繼承。
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)); } }
我的 App.xaml.cs 配置了 Autofac 如下所示:
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 }
當我嘗試啟動應用程序時,程序無法using IHost host = CreateHostBuilder(args).Build();
並拋出此錯誤消息:
Autofac.Core.Activators.Reflection.NoConstructorsFoundException: 'No accessible constructors were found for the type 'TestApp.Bases.ListItemViewModelBase'.'
似乎 Autofac 無法解析ListItemViewModelBase
class 的非公共構造函數。 有沒有辦法讓 Autofac 解析這些類型的非公共構造函數?
謝謝,托拜厄斯
回復 Orenico 的回復:
這是autofac.json基本上什么都不包含。
{ "modules": [], "components": [] }
示例代碼中有一些拼寫錯誤,例如builder.RegisterModule>()
,它不會編譯並表明存在注冊和其他您未向我們展示的內容。
然而:
您不能直接實例化抽象 class。 就像,你不能做new ListItemViewModelBase
。 這意味着您無法注冊它。 您可以注冊派生類As
it,但即使 Autofac 可以找到構造函數,您仍然會碰壁,因為它不是具體的 class。
如果 Autofac 正在通過反射解析類型,它不會 go 一直查看構造函數,它只會查看您正在解析的內容。 這就是為什么聽起來你可能試圖注冊那個抽象的 class。
如果我不得不猜測,您在那些模塊中有一些裝配掃描注冊,您沒有向我們展示您嘗試注冊所有視圖模型的位置,並且您沒有從注冊中排除基本 class。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.