Autofac:缺少涉及 MvvmLight RelayCommand 的方法
Autofac: Missing Method Involving MvvmLight RelayCommand
我有一个通用集合 class,它基于我在 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<>) );
IUIManager,DialogCollection 构造函数的唯一参数,也已注册到 Autofac,并在程序运行时正确实例化。
IDialogCollection 的特定实例由 Autofac 创建的 lambda 方法生成,该方法通过 class 的构造函数传入,其中包含我要创建的集合的实例:
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();
}
}
colBuilder 和 communityBuilder 都在传递到 CommunitiesModel 构造函数时被正确实例化,我认为这意味着 Autofac 能够使用注册信息来创建创建这些 classes 实例的方法。
我不明白为什么在执行 _colBuilder() 时会出现缺少方法异常,因为代码可以正常编译(这意味着 RelayCommand ctor 已知且可用)。
如果我在 DialogCollection<> 的定义中注释掉两个 RelayCommand 创建行(即 "new RelayCommand..."),则不会抛出异常。
这意味着 Autofac creator 方法在构造函数中找不到已使用适当的构造函数参数调用的内容。
这是抛出的异常:
Autofac.Core.DependencyResolutionException HResult=0x80131500
Message=An error occurred during the activation of a particular
registration. See the inner exception for details. Registration:
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()<br>
at
Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope
currentOperationScope, IComponentRegistration registration,
IEnumerable
1 parameters) at
Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration
registration, IEnumerable1 parameters) at
Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration
registration, IEnumerable
1 parameters) at
Olbert.CommunityScanner.Manager.ViewModel.CommunitiesModel.Load() in
C:\Programming\CommunityScanner\CommunityScannerManager\ViewModel\CommunitiesModel.cs:line
58 at
Olbert.CommunityScanner.Manager.ViewModel.AppStateModel.set_ActivePageInfo(PageInfo
value) in
C:\Programming\CommunityScanner\CommunityScannerManager\ViewModel\AppWide\AppStateModel.cs:line
117 at
Olbert.CommunityScanner.Manager.App.OnStartup(StartupEventArgs e) in
C:\Programming\CommunityScanner\CommunityScannerManager\App.xaml.cs:line
44 at System.Windows.Application.<.ctor>b__1_0(Object unused) at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Int32 numArgs) at
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,
Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl() at
System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object
state) at
MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object
obj) at
System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx) in
f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 954
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx) in
f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 901
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state) in
f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 890
at
MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext
executionContext, ContextCallback callback, Object state) 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& handled) at
MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam,
IntPtr lParam, Boolean& handled) at
MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Int32 numArgs) at
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,
Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at
System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority
priority, TimeSpan timeout, Delegate method, Object args, Int32
numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,
Int32 msg, IntPtr wParam, IntPtr lParam) at
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame
frame) at
System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore) at
System.Windows.Application.RunInternal(Window window) at
System.Windows.Application.Run(Window window) at
System.Windows.Application.Run() at
Olbert.CommunityScanner.Manager.App.Main()
Inner Exception 1: DependencyResolutionException: An exception was
thrown while invoking the constructor 'Void
.ctor(WpfFramework.IUIManager)' on type 'DialogCollection`1'.
Inner Exception 2: MissingMethodException: Method not found: 'Void
GalaSoft.MvvmLight.Command.RelayCommand1..ctor(System.Action
1)'.
如果需要,我可以提供更多细节;我在这里展示的内容是从更大的代码库中抽象出来的,但希望能展示相关的细节。
我建议查看 RelayCommand1
- 它似乎 IUIManager
或 DialogCollection<T>
需要该构造函数,但它不存在。自下而上地工作。 Autofac 部分是红色鲱鱼。问题的核心是底部的内部异常。
我有一个通用集合 class,它基于我在 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<>) );
IUIManager,DialogCollection 构造函数的唯一参数,也已注册到 Autofac,并在程序运行时正确实例化。
IDialogCollection 的特定实例由 Autofac 创建的 lambda 方法生成,该方法通过 class 的构造函数传入,其中包含我要创建的集合的实例:
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();
}
}
colBuilder 和 communityBuilder 都在传递到 CommunitiesModel 构造函数时被正确实例化,我认为这意味着 Autofac 能够使用注册信息来创建创建这些 classes 实例的方法。
我不明白为什么在执行 _colBuilder() 时会出现缺少方法异常,因为代码可以正常编译(这意味着 RelayCommand ctor 已知且可用)。
如果我在 DialogCollection<> 的定义中注释掉两个 RelayCommand 创建行(即 "new RelayCommand..."),则不会抛出异常。
这意味着 Autofac creator 方法在构造函数中找不到已使用适当的构造函数参数调用的内容。
这是抛出的异常:
Autofac.Core.DependencyResolutionException HResult=0x80131500
Message=An error occurred during the activation of a particular registration. See the inner exception for details. Registration: 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()<br> at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable
1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable
1 parameters) at Olbert.CommunityScanner.Manager.ViewModel.CommunitiesModel.Load() in C:\Programming\CommunityScanner\CommunityScannerManager\ViewModel\CommunitiesModel.cs:line 58 at Olbert.CommunityScanner.Manager.ViewModel.AppStateModel.set_ActivePageInfo(PageInfo value) in C:\Programming\CommunityScanner\CommunityScannerManager\ViewModel\AppWide\AppStateModel.cs:line 117 at Olbert.CommunityScanner.Manager.App.OnStartup(StartupEventArgs e) in C:\Programming\CommunityScanner\CommunityScannerManager\App.xaml.cs:line 44 at System.Windows.Application.<.ctor>b__1_0(Object unused) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 954
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 901
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 890
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) 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& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at Olbert.CommunityScanner.Manager.App.Main()Inner Exception 1: DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(WpfFramework.IUIManager)' on type 'DialogCollection`1'.
Inner Exception 2: MissingMethodException: Method not found: 'Void GalaSoft.MvvmLight.Command.RelayCommand
1..ctor(System.Action
1)'.
如果需要,我可以提供更多细节;我在这里展示的内容是从更大的代码库中抽象出来的,但希望能展示相关的细节。
我建议查看 RelayCommand1
- 它似乎 IUIManager
或 DialogCollection<T>
需要该构造函数,但它不存在。自下而上地工作。 Autofac 部分是红色鲱鱼。问题的核心是底部的内部异常。