メモです。
最近DMM版ウマ娘の画面サイズ自動変更ツールのUIをWPF化しようとしていた時に起こった事象。
.NET Core 3.1からアプリケーションをpublishする時にリンクしているアセンブリの不使用な部分をパッケージから削除する機能が追加されています。
これを使用してWPFなアプリケーションのTrimを行ったところ、アプリケーションが実行時にクラッシュしてしまいました。 イベントビューアでエラーを確認したところ
Application: UmaMadoManager.Windows.exe CoreCLR Version: 5.0.521.16609 .NET Version: 5.0.5 Description: The process was terminated due to an unhandled exception. Exception Info: System.TypeInitializationException: The type initializer for 'System.Windows.FrameworkElement' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Windows.Documents.TextElement' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Windows.Documents.Typography' threw an exception. ---> System.IO.FileNotFoundException: File name: 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at System.Windows.Media.TextFormatting.TextRunTypographyProperties.OnPropertiesChanged() at MS.Internal.Text.TypographyProperties.ResetProperties() at MS.Internal.Text.TypographyProperties..ctor() at System.Windows.Documents.Typography..cctor() --- End of inner exception stack trace --- at System.Windows.Documents.TextElement..cctor() --- End of inner exception stack trace --- at System.Windows.FrameworkElement..cctor() --- End of inner exception stack trace --- at System.Runtime.CompilerServices.RuntimeHelpers._RunClassConstructor(RuntimeType type) at System.Windows.DependencyProperty.FromName(String name, Type ownerType) at System.Windows.Baml2006.WpfXamlType.FindDependencyPropertyBackedProperty(String name, Boolean isAttachable, Boolean skipReadOnlyCheck) at System.Windows.Baml2006.WpfXamlType.FindMember(String name, Boolean isAttached, Boolean skipReadOnlyCheck) at System.Windows.Baml2006.WpfXamlType.LookupMember(String name, Boolean skipReadOnlyCheck) at System.Xaml.XamlType.GetMember(String name) at System.Windows.Baml2006.Baml2006SchemaContext.GetProperty(Int16 propertyId, XamlType parentType) at System.Windows.Baml2006.Baml2006Reader.Process_PropertyWithStaticResourceId() at System.Windows.Baml2006.Baml2006Reader.Process_OneBamlRecord() at System.Windows.Baml2006.Baml2006Reader.ReadObject(KeyRecord record) at System.Windows.ResourceDictionary.CreateObject(KeyRecord key) at System.Windows.ResourceDictionary.OnGettingValue(Object key, Object& value, Boolean& canCache) at System.Windows.ResourceDictionary.OnGettingValuePrivate(Object key, Object& value, Boolean& canCache) at System.Windows.ResourceDictionary.GetValueWithoutLock(Object key, Boolean& canCache) at System.Windows.ResourceDictionary.GetValue(Object key, Boolean& canCache) at System.Windows.ResourceDictionary.GetValueWithoutLock(Object key, Boolean& canCache) at System.Windows.ResourceDictionary.GetValue(Object key, Boolean& canCache) at System.Windows.ResourceDictionary.get_Item(Object key) at System.Windows.Application.FindResource(Object resourceKey) at UmaMadoManager.Windows.App.OnStartup(StartupEventArgs e) 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.Dispatcher.WrappedInvoke(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) --- End of stack trace from previous location --- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 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.WrappedInvoke(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 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg) 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.Threading.Dispatcher.Run() 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 UmaMadoManager.Windows.App.Main()
以下のようなエラーが発生していました。
Exceptionでぐぐってみたら
こんなIssueが引っかかった。
Trimming default WPF app breaks it · Issue #14261 · dotnet/sdk · GitHub
ここらへんで詳しい原因を述べていているで気になる方はこちらをチェック。
とりあえずWorkaroundとして載せられている
<ItemGroup> <TrimmerRootAssembly Include="System.Runtime"/> <TrimmerRootAssembly Include="System.Runtime.Extensions"/> <TrimmerRootAssembly Include="System.Diagnostics.Debug"/> </ItemGroup>
こいつをcsprojに追記してとりあえず動作するようになった。 やっぱり動作するかのテスト追加しないとだなー