我有一个基于我在Autofac注册的MvvM Light库的通用集合类:
public class DialogCollection<TViewModel> : ObservableCollection<TViewModel>, IDialogCollection<TViewModel>
{
private readonly IUIManager _uiManager;
public DialogCollection( IUIManager uiManager )
{
_uiManager = uiManager ?? throw new NullReferenceException( nameof(uiManager) );
ViewModelSelectedCommand = new RelayCommand<TViewModel>( DetailItemSelectedHandler );
AddNewViewModelCommand = new RelayCommand( AddNewItemHandler );
}
public RelayCommand<TViewModel> ViewModelSelectedCommand { get; }
public RelayCommand AddNewViewModelCommand { get; }
}
Autofac注册:
builder.RegisterGeneric( typeof(DialogCollection<>) )
.As( typeof(IDialogCollection<>) );
IXIManager是DialogCollection构造函数的唯一参数,也在Autofac中注册,并在程序运行时正确实例化。
IDialogCollection的特定实例是从Autofac创建的lambda方法生成的,该方法通过类的构造函数传递,该类包含我想要创建的集合的实例:
public class CommunitiesModel
{
private readonly Func<IDialogCollection<CommunityModel>> _colBuilder;
private readonly Func<CommunityModel> _communityBuilder;
private DialogCollection<CommunityModel> _communities;
public CommunitiesModel(
Func<IDialogCollection<CommunityModel>> colBuilder,
Func<CommunityModel> commmunityBuilder
)
{
_colBuilder = colBuilder ?? throw new NullReferenceException(nameof(colBuilder));
_communityBuilder= commmunityBuilder?? throw new NullReferenceException(nameof(commmunityBuilder));
}
// I'm not showing how Load() gets called, but it does :)
public override void Load()
{
// this next line creates an instance of DialogCollection
// it's also where the Autofac Missing Method exception gets thrown
Communities = (DialogCollection<CommunityModel>) _colBuilder();
}
}
当传递到CommunitiesModel构造函数时,colBuilder和communityBuilder都被正确实例化,我认为这意味着Autofac能够使用注册信息来创建创建这些类实例的方法。
我不明白为什么在执行_colBuilder()时会出现方法异常,因为代码编译得很好(意味着RelayCommand ctor已知且可用)。
如果我在DialogCollection&lt;&gt;的定义中注释掉两个RelayCommand创建行(即&#34; new RelayCommand ...&#34;),则不会抛出异常。
这意味着Autofac创建者方法正在解决在构造函数中没有找到已使用适当的constructur参数调用的内容。
以下是抛出的异常:
Autofac.Core.DependencyResolutionException HResult = 0x80131500
消息=激活特定时发生错误 注册。有关详细信息,请参阅内部异常注册: Activator = DialogCollection1 (ReflectionActivator), Services =[WpfFramework.IDialogCollection
1 [[Olbert.CommunityScanner.Manager.ViewModel.CommunityModel, CommunityScannerManager,Version = 0.0.0.1,Culture = neutral, PublicKeyToken = null]]],Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,Sharing = None,Ownership = OwnedByLifetimeScope Source = Autofac StackTrace:at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute()
1个参数)at at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration 注册,IEnumerable
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable
1参数)at Olbert.CommunityScanner.Manager.ViewModel.CommunitiesModel.Load()in C:\程序\ CommunityScanner \ CommunityScannerManager \视图模型\ CommunitiesModel.cs:行 58点 Olbert.CommunityScanner.Manager.ViewModel.AppStateModel.set_ActivePageInfo(PageInfo 价值) C:\程序\ CommunityScanner \ CommunityScannerManager \视图模型\ AppWide \ AppStateModel.cs:行 117点 Olbert.CommunityScanner.Manager.App.OnStartup(StartupEventArgs e)in C:\程序\ CommunityScanner \ CommunityScannerManager \ App.xaml.cs:行 44在System.Windows.Application。&lt; .ctor&gt; b__1_0(Object unused)at System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表 回调,对象args,Int32 numArgs)at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, 委托回调,Object args,Int32 numArgs,Delegate catchHandler) 在System.Windows.Threading.DispatcherOperation.InvokeImpl()at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象 国家) MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(对象 obj)at System.Threading.ExecutionContext.RunInternal(执行上下文 executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)in f:\ dd \ ndp \ clr \ src \ BCL \ system \ threading \ executioncontext.cs:第954行
在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 preserveSyncCtx)in f:\ dd \ ndp \ clr \ src \ BCL \ system \ threading \ executioncontext.cs:第901行 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) f:\ dd \ ndp \ clr \ src \ BCL \ system \ threading \ executioncontext.cs:第890行
在 MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext,ContextCallback回调,对象状态)at System.Windows.Threading.DispatcherOperation.Invoke()at System.Windows.Threading.Dispatcher.ProcessQueue()at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&amp;处理) MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam, IntPtr lParam,Boolean&amp;处理) MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)at System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表 回调,对象args,Int32 numArgs)at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, 委托回调,Object args,Int32 numArgs,Delegate catchHandler) 在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(的DispatcherPriority 优先级,TimeSpan超时,委托方法,对象args,Int32 numArgs)在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg,IntPtr wParam,IntPtr lParam)at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)at at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)
在System.Windows.Application.RunDispatcher(Object ignore)at System.Windows.Application.RunInternal(窗口窗口)at System.Windows.Application.Run(窗口窗口)at System.Windows.Application.Run()at Olbert.CommunityScanner.Manager.App.Main()内部异常1:DependencyResolutionException:异常是 在调用构造函数&#39; Void时抛出 .ctor(WpfFramework.IUIManager)&#39;在类型&#39; DialogCollection`1&#39;。
内部异常2:MissingMethodException:未找到方法:&#39; Void GalaSoft.MvvmLight.Command.RelayCommand
1..ctor(System.Action
1)&#39;
如果需要,我可以提供更多细节;我在这里展示的内容是从更大的代码库中抽象出来的,但希望能够显示相关的细节。
答案 0 :(得分:1)
我建议您查看RelayCommand1
- 看起来IUIManager
或DialogCollection<T>
需要该构造函数,但它不存在。在此自下而上。 Autofac部分是红鲱鱼。问题的核心是底层的内部例外。