我已经用谷歌搜索这个问题 2 天了,我看到的所有解决方案都不太适合我的场景,或者修复不起作用。
我有一个 WPF 桌面应用程序。当我调试项目时,一切都工作正常且 100%。但是当我部署我的应用程序时(即使是在我开发它的 PC 上),它就崩溃了。它似乎在它找到的第一个类实例化时崩溃并抛出一个异常,该异常的 innerException 始终是一个空引用异常。前一个异常一直在变化,例如 TargetInvocationException、TypeInitializationException 等。
这是堆栈跟踪:
System.TypeInitializationException: The type initializer for 'eTute.BusinessLayer' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at eTute.BusinessLayer..cctor()
--- End of inner exception stack trace ---
at eTute.BusinessLayer.EncryptPassword(String password)
at eTute.MainWindow.imgLock_MouseDown(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(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 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(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()
“BusinessLayer”就像我的数据访问层 (DAL)。起初它是实例驱动的,但是当它开始在 BusinessLayer 的构造函数上崩溃时,我将整个类更改为静态。现在它似乎只是在下一次实例化时崩溃。
注意:要创建安装程序,我只使用了 Visual Studio 的安装向导。该应用程序使用 SQL Express 数据库,但它甚至从未使用它,因为它在任何小实例上都会崩溃。
请,任何帮助或评论将不胜感激。我以前从未做过安装,但觉得它不应该这么复杂!
更新
问题似乎与访问 app.config 文件中的 connectionStrings 有关。ConfigurationManager.ConnectionStrings["name"].ConnectingString
当我用硬编码的字符串变量替换所说的代码时,问题就消失了。
但是,当我的应用程序部署时,我应该如何使用 app.config 文件呢?