我有一个 WPF 应用程序,它可能会或可能不会使用命令行参数启动。我曾经在 App.xaml 代码隐藏的方法中拥有我的组合根OnStartup(StartupEventArgs e)
,但这会导致应用程序关闭问题,所以我将 App.xaml 变成了“页面”(而不是“应用程序定义”)并编写了我自己的Program
类包含我自己的应用程序入口点,这将成为我的新组合根位置。
自那次更改以来,我一直无法启动应用程序,Ninject 似乎无法解析应用程序的主要对象(或者它可能是它的依赖项之一?)。
这个异常让我浪费了很多时间,而且堆栈跟踪都是 Ninject 内部的,我不知道要在我的代码中修复什么,我绑定现在导致此异常的类型的方式最近没有改变:
at DynamicInjector54d92ac63a2e47fda5ffbcc19b9942a9(Object[] )
at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
at MyProgram.Program.Main(String[] args) in C:\Dev\MyProject\MyProject.WinPresentation\Program.cs:ligne 40
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
这是Main
方法/应用程序入口点:
[STAThread]
public static void Main(string[] args)
{
var module = new MyAppNinjectModule(args);
var kernel = new StandardKernel(module);
var argsHelper = module.CommandLineArgs;
var logProvider = kernel.Get<ILogProvider>();
var logger = logProvider.GetLogger(typeof(Program).Name);
if (argsHelper.LoggingDisabledArgument.IsSpecified()) logProvider.DisableLogging();
logger.Info(log.LogAppStart);
var installer = kernel.Get<IInstaller>(); // >>> InvalidCastException here
if (argsHelper.QuietInterfaceArgument.IsSpecified())
{
// running with -quiet command-line switch: just execute and exit.
installer.Execute();
}
else
{
// instantiate a new App object (WPF), and run it.
// installer.Execute() may or may not be executed, depending on user actions.
var app = new App(installer);
app.Run();
}
}
NinjectModule 绑定IInstaller
到这样或这样的实现,这取决于提供的命令行参数(例如,SilentInstaller
何时QuietInterfaceArgument
指定,ManualInstaller
何时不指定等)。
通常 Ninject 会给出非常有用和详细的异常消息——当它的ActivationException
生活是美好的时候。但这InvalidCastException
让我一无所知,我不是执行无效演员阵容的人,我什至不知道所涉及的类型。我只知道我可能编写了一些 Ninject 不喜欢的代码,这可能与我绑定IInstaller
到它的实现的方式有关,但是如果我注释掉 NinjectModule 的“分支”部分强制绑定ManualInstaller
我所追求的特定实现(),它仍然失败InvalidCastException
。
实现的构造函数:
public ManualInstaller(IView<MainWindowViewModel> view,
IProcessHelper processHelper,
ISettingsHelper settingsHelper,
ILogProvider logProvider,
ISetupBootstrapper installer,
bool notifySuccess)
: base(notifySuccess, processHelper, settingsHelper, logProvider, installer)
相应的绑定代码(其余依赖项已绑定,ActivationException
因此不确定这与我的问题有多大相关性):
var msg = string.Empty;
if (CommandLineArgs.CompletionMessageArgument.IsSpecified())
msg = CommandLineArgs.CompletionMessageArgument.ParameterValue();
Bind<MainWindowViewModel>().ToSelf().WithConstructorArgument("completionMessage", msg);
Bind<IView<MainWindowViewModel>>().To<MainWindow>();
Bind<IInstaller>().To<ManualInstaller>()
.WithConstructorArgument("notifySuccess", notifySuccess);
不要犹豫,让我知道是否需要其他任何事情来了解发生了什么......
编辑
单独解决安装程序的依赖关系会产生更多信息:
// resolve installer dependencies:
var view = kernel.Get<IView<MainWindowViewModel>>(); // >>> InvalidCastException here
var processHelper = kernel.Get<IProcessHelper>();
var settingsHelper = kernel.Get<ISettingsHelper>();
var bootstrapper = kernel.Get<ISetupBootstrapper>();
var installer = new ManualInstaller(view, processHelper, settingsHelper, logProvider, bootstrapper, true);
因此,我至少可以将其缩小到我要解决的类型的一个特定依赖项:问题出在 View 或其唯一依赖项 ViewModel 上。所以我这样做了:
// resolve ViewModel dependencies:
var processHelper = kernel.Get<IProcessHelper>(); // >>> InvalidCastException here
var settingsHelper = kernel.Get<ISettingsHelper>();
var messenger = kernel.Get<INetworkMessenger>();
var factory = kernel.Get<IBuildServerFactory>();
var dialogs = kernel.Get<ICommonDialogs>();
很明显,IProcessHelper 实现有问题 - 再次:
// resolve ProcessHelper dependencies:
var processWrapper = kernel.Get<IProcessWrapper>();
var wmiWrapper = kernel.Get<IWindowsManagementInstrumentationWrapper>();
var helper = new ProcessHelper(processWrapper, wmiWrapper, logProvider, 300);
现在我不再得到InvalidCastException
.
这是有问题的类的构造函数和字段:
private readonly ILogProvider _logProvider;
private readonly IProcessWrapper _process;
private readonly IWindowsManagementInstrumentationWrapper _wmi;
public int TimeoutSeconds { get; private set; }
public ProcessHelper(IProcessWrapper process,
IWindowsManagementInstrumentationWrapper wmiWrapper,
ILogProvider logProvider,
int timeout)
{
_logProvider = logProvider;
_process = process;
_wmi = wmiWrapper;
TimeoutSeconds = timeout;
}
以及 NinjectModule 如何绑定它:
Bind<IProcessHelper>().To<ProcessHelper>()
.WithConstructorArgument("timeout", Properties.Settings.Default.ProcessTimeoutSeconds);
** 重新编辑 **
在@jure 评论的帮助下,我发现 in 的类型ProcessTimeoutSeconds
实际上Properties.Settings.Default
设置为string
- 当显然它想要一个int
. 砰!