【发布时间】:2019-11-12 15:06:24
【问题描述】:
我有一个为我工作的公司构建的 WPF 应用程序。整个事情在我的机器和分支网络中的其他机器上运行良好。
但是,我们总部的一位用户无法使用它。应用程序在启动时立即关闭。我尝试在任务管理器中监控它,它出现了,然后立即关闭。
所以,我决定在启动表单中放一些消息框,看看它失败的地方:
public LoginWindow()
{
MessageBox.Show("CHECKING FOR UPDATES");
var updated = Jmis.IsUpdated();
MessageBox.Show("UPDATES CHECKED");
if (updated)
{
MessageBox.Show("LOADING SETTINGS");
Settings = FileManager.LoadSettings();
MessageBox.Show("SETTINGS LOADED");
this.DataContext = Settings;
InitializeComponent();
Password.Password = Settings.Password;
}
else
{
MessageBox.Show("A new version is available!", "Update", MessageBoxButton.OK, MessageBoxImage.Information);
new UpdateWindow().Show();
Close();
}
InitializeComponent();
}
它显示CHECKING FOR UPDATES 消息,然后关闭。
同样,我在Jmis.IsUpdated() 方法中添加了一些消息框:
public static bool IsUpdated()
{
try
{
MessageBox.Show("Sending request");
var response = Http.GetAsync($"{JmisUri}/toasterNotification/version.php").Result;
MessageBox.Show("Ensuring success");
response.EnsureSuccessStatusCode();
MessageBox.Show("Getting version string");
var version = response.Content.ReadAsStringAsync().Result;
MessageBox.Show("Checking version");
return version == Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
catch(Exception ex)
{
return true;
}
}
我至少期待Sending request 出现。但它没有。我仍然得到CHECKING FOR UPDATES,但没有别的。
就好像根本没有调用该方法。
我真的被难住了。
有什么原因会发生这种情况吗?
编辑
查看事件日志后发现:
Application: JMIS Notifier.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
at JMIS_Notifier.JMIS.Jmis..cctor()
Exception Info: System.TypeInitializationException
at JMIS_Notifier.JMIS.Jmis.IsUpdated()
at JMIS_Notifier.LoginWindow..ctor()
Exception Info: System.Windows.Markup.XamlParseException
at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri)
at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean)
at System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext)
at System.Windows.Application.LoadComponent(System.Uri, Boolean)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__1_0(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
at System.Windows.Application.RunDispatcher(System.Object)
at System.Windows.Application.RunInternal(System.Windows.Window)
at System.Windows.Application.Run(System.Windows.Window)
at System.Windows.Application.Run()
at JMIS_Notifier.App.Main()
有趣的是异常发生在JMIS_Notifier.JMIS.Jmis..cctor(),但它是一个静态类。
【问题讨论】:
-
查看系统事件日志。那里应该有一条错误消息。
-
你为什么在
LoginWindow中打电话给InitializeComponent();?从表面上看,可能有很多事情是错误的。您是否正确地通过了Jmis.IsUpdated();? -
你catch unhandled exceptions吗?远程调试器是一个更好的选择。
-
此行为的最常见原因是无法加载引用的程序集,因为 a) 它未安装 b) 它依赖于未安装的其他 dll 或 c) 它需要操作系统是 64 位,但操作系统是 32 位,反之亦然
-
@NineBerry 我发布了一个答案,似乎已经为我们的一位用户解决了这个问题。我很快就会为其他用户测试它。