WPF Apps crash on Windows 10/11 after Windows Updates installed: System.IO.FileFormatException: StoryFragments part failed to load

Mark Chesney 26 Reputation points
2022-12-15T01:57:03.347+00:00

My company develops .NET WPF apps (32-bit, targeting .NET Framework 4.7.2, and 4.5 for one of the old ones) written in C# using Visual Studio. My Windows 11 dev system installed updates last night, and today when I run any of our WPF apps (either under the Visual Studio debugger or standalone) they all crash with the same error: "System.IO.FileFormatException: StoryFragments part failed to load".

I initially thought the crash was due to code changes I had made, but eventually came to suspect it was due to the recent upgrades. I verified this by running the apps on another Windows 11 system and confirming that they ran just fine. I then installed the latest updates from the Windows Update part of the Settings app and rebooted the PC. After the reboot, when our apps are run they crash with the same error!

I then ran the same test on a Windows 10 system and got the same result - the app crashes with the same error!

My questions are:

  • Has anyone else experienced this error?
  • If so, is there a way to prevent it from occurring?
  • Or is there a problem with a particular Windows updates that needs to be fixed?

Since the applications crash every time they are run, I am currently dead in the water as far as application development for my employer goes. So obviously I need a resolution to this problem ASAP.

If any of these updates happen to get installed on our sales staff PCs, then they will be unable to demo the applications for customers.

Fortunately we have a private Windows Update server for our deployed systems, so I've notified the person in charge of that to hopefully prevent any new updates from reaching those systems. Otherwise we'd be in a world of hurt!

Here are the Windows updates that were installed on the various systems:

Windows 10 PC
2022-12 Cumulative Update for Windows 10 Version 21H2 for x64-based Systems (KB5021233)
2022-12 Cumulative Update for .NET Framework 3.5, 4.8 and 4.8.1 for Windows 10 Version 21H2 for x64 9KB5021088)

Windows 11 PC #1
2022-12 Cumulative Update for .NET Framework 3.5, 4.8 and 4.8.1 for Windows 11 for x64 (KB5021090)
2022-12 .NET 6.0.12 Security Update for x64 Client (KB5021954)
2022-12 .NET Core 3.1.32 Security Update for x64 Client (KB5021953)
2022-12 Cumulative Update for Windows 11 for x64-based Systems (KB5021234)

Windows 11 PC #2
2022-12 Cumulative Update for Windows 11 Version 22H2 for x64-based Systems (KB5021255)
2022-12 Cumulative Update for .NET Framework 3.5 and 4.8.1 for Windows 11, version 22H2 for x64 (KB5020880)
2022-12 .NET Core 3.1.32 Security Update for x64 Client (KB5021953)
2022-12 .NET 6.0.12 Security Update for x64 Client (KB5021954)

Here is the call stack from when one of our apps crashed:

2022-12-14 16:58:31.5440,Fatal,App,Fatal Exception,TruMed.AccuShelf.App.OnDispatcherUnhandledException,981,"System.IO.FileFormatException: StoryFragments part failed to load.
at System.Windows.Documents.FixedDocument.GetStoryFragments(FixedPage fixedPage)
at System.Windows.Documents.FixedTextBuilder.EnsureTextOMForPage(Int32 pageIndex)
at System.Windows.Documents.FlowPosition._xGetNextFlowNode()
at System.Windows.Documents.FlowPosition._vGetSymbolType(LogicalDirection dir)
at System.Windows.Documents.FixedTextPointer.System.Windows.Documents.ITextPointer.GetPointerContext(LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.xGetClingDSTP(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.xGapAwareGetSymbolType(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.GetPointerContext(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.System.Windows.Documents.ITextPointer.GetPointerContext(LogicalDirection direction)
at System.Windows.Documents.TextPointerBase.GetBorderingElementCategory(ITextPointer position, LogicalDirection direction)
at System.Windows.Documents.TextPointerBase.IsAtNormalizedPosition(ITextPointer position, Boolean respectCaretUnitBoundaries)
at System.Windows.Documents.TextPointerBase.IsAtNormalizedPosition(ITextPointer position, LogicalDirection direction, Boolean respectCaretUnitBoundaries)
at System.Windows.Documents.TextRangeBase.IsAtNormalizedPosition(ITextRange thisRange, ITextPointer position, LogicalDirection direction)
at System.Windows.Documents.TextRangeBase.CreateNormalizedTextSegment(ITextRange thisRange, ITextPointer start, ITextPointer end)
at System.Windows.Documents.TextRangeBase.SelectPrivate(ITextRange thisRange, ITextPointer position1, ITextPointer position2, Boolean includeCellAtMovingPosition, Boolean markRangeChanged)
at System.Windows.Documents.TextRangeBase.Select(ITextRange thisRange, ITextPointer position1, ITextPointer position2, Boolean includeCellAtMovingPosition)
at System.Windows.Documents.TextRange..ctor(ITextPointer position1, ITextPointer position2, Boolean ignoreTextUnitBoundaries)
at System.Windows.Documents.TextSelection..ctor(TextEditor textEditor)
at System.Windows.Documents.TextEditor..ctor(ITextContainer textContainer, FrameworkElement uiScope, Boolean isUndoEnabled)
at System.Windows.Controls.Primitives.DocumentViewerBase.AttachTextEditor()
at System.Windows.Controls.Primitives.DocumentViewerBase.OnDocumentChanged()
at System.Windows.Controls.DocumentViewer.OnDocumentChanged()
at System.Windows.Controls.Primitives.DocumentViewerBase.DocumentChanged(IDocumentPaginatorSource oldDocument, IDocumentPaginatorSource newDocument)
at System.Windows.Controls.Primitives.DocumentViewerBase.DocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.ScheduleTransfer(Boolean isASubPropertyChange)
at MS.Internal.Data.ClrBindingWorker.NewValueAvailable(Boolean dependencySourcesChanged, Boolean initialValue, Boolean isASubPropertyChange)
at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
at MS.Internal.Data.PropertyPathWorker.OnDependencyPropertyChanged(DependencyObject d, DependencyProperty dp, Boolean isASubPropertyChange)
at MS.Internal.Data.ClrBindingWorker.OnSourceInvalidation(DependencyObject d, DependencyProperty dp, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at TruMed.AccuShelf.App.<>c__DisplayClass69_0.<OnApplicationStartup>b__1() in C:\TS-475\AccuShelf\App\App\App.xaml.cs:line 885
at System.Windows.Threading.DispatcherOperation.InvokeDelegateCore()
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at TruMed.AccuShelf.App.<OnApplicationStartup>d__69.MoveNext() in C:\TS-475\AccuShelf\App\App\App.xaml.cs:line 883
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
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.Documents.FixedDocument.GetStoryFragments(FixedPage fixedPage)
at System.Windows.Documents.FixedTextBuilder.EnsureTextOMForPage(Int32 pageIndex)
at System.Windows.Documents.FlowPosition._xGetNextFlowNode()
at System.Windows.Documents.FlowPosition._vGetSymbolType(LogicalDirection dir)
at System.Windows.Documents.FixedTextPointer.System.Windows.Documents.ITextPointer.GetPointerContext(LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.xGetClingDSTP(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.xGapAwareGetSymbolType(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.GetPointerContext(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
at System.Windows.Documents.DocumentSequenceTextPointer.System.Windows.Documents.ITextPointer.GetPointerContext(LogicalDirection direction)
at System.Windows.Documents.TextPointerBase.GetBorderingElementCategory(ITextPointer position, LogicalDirection direction)
at System.Windows.Documents.TextPointerBase.IsAtNormalizedPosition(ITextPointer position, Boolean respectCaretUnitBoundaries)
at System.Windows.Documents.TextPointerBase.IsAtNormalizedPosition(ITextPointer position, LogicalDirection direction, Boolean respectCaretUnitBoundaries)
at System.Windows.Documents.TextRangeBase.IsAtNormalizedPosition(ITextRange thisRange, ITextPointer position, LogicalDirection direction)
at System.Windows.Documents.TextRangeBase.CreateNormalizedTextSegment(ITextRange thisRange, ITextPointer start, ITextPointer end)
at System.Windows.Documents.TextRangeBase.SelectPrivate(ITextRange thisRange, ITextPointer position1, ITextPointer position2, Boolean includeCellAtMovingPosition, Boolean markRangeChanged)
at System.Windows.Documents.TextRangeBase.Select(ITextRange thisRange, ITextPointer position1, ITextPointer position2, Boolean includeCellAtMovingPosition)
at System.Windows.Documents.TextRange..ctor(ITextPointer position1, ITextPointer position2, Boolean ignoreTextUnitBoundaries)
at System.Windows.Documents.TextSelection..ctor(TextEditor textEditor)
at System.Windows.Documents.TextEditor..ctor(ITextContainer textContainer, FrameworkElement uiScope, Boolean isUndoEnabled)
at System.Windows.Controls.Primitives.DocumentViewerBase.AttachTextEditor()
at System.Windows.Controls.Primitives.DocumentViewerBase.OnDocumentChanged()
at System.Windows.Controls.DocumentViewer.OnDocumentChanged()
at System.Windows.Controls.Primitives.DocumentViewerBase.DocumentChanged(IDocumentPaginatorSource oldDocument, IDocumentPaginatorSource newDocument)
at System.Windows.Controls.Primitives.DocumentViewerBase.DocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.ScheduleTransfer(Boolean isASubPropertyChange)
at MS.Internal.Data.ClrBindingWorker.NewValueAvailable(Boolean dependencySourcesChanged, Boolean initialValue, Boolean isASubPropertyChange)
at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
at MS.Internal.Data.PropertyPathWorker.OnDependencyPropertyChanged(DependencyObject d, DependencyProperty dp, Boolean isASubPropertyChange)
at MS.Internal.Data.ClrBindingWorker.OnSourceInvalidation(DependencyObject d, DependencyProperty dp, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at TruMed.AccuShelf.App.<>c__DisplayClass69_0.<OnApplicationStartup>b__1() in C:\TS-475\AccuShelf\App\App\App.xaml.cs:line 885
at System.Windows.Threading.DispatcherOperation.InvokeDelegateCore()
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at TruMed.AccuShelf.App.<OnApplicationStartup>d__69.MoveNext() in C:\TS-475\AccuShelf\App\App\App.xaml.cs:line 883
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
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)"

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,196 questions
{count} votes

8 answers

Sort by: Most helpful
  1. Strano, Emanuele 10 Reputation points
    2023-02-09T10:26:18.0966667+00:00

    Further solution according to this blog post: [http://dedjo.blogspot.com/2008/04/converting-fixeddocument-xpsdocument.html]

    the XpsDocument.GetFixedDocumentSequence() method can be replaced by the following code

            public FixedDocumentSequence GetFixedDocumentSequence(XpsDocument xpsDoc)
            {
                var sequence = new FixedDocumentSequence();
                var pdlgPrint = new PrintDialog();
    
                var path = $"{System.IO.Path.GetDirectoryName(System.IO.Path.GetTempPath())}\\Resources\\tempfonts\\{this.GetHashCode().ToString()}\\";
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }
                Directory.CreateDirectory(path);
    
                foreach (var fdReader in xpsDoc.FixedDocumentSequenceReader.FixedDocuments)
                {
                    var fixedDocument = new FixedDocument();
                    var reference = new DocumentReference();
                    foreach (var fpReader in fdReader.FixedPages)
                    {
                        while (fpReader.XmlReader.Read())
                        {
                            string page = fpReader.XmlReader.ReadOuterXml().Replace("/Resources/", path ).Replace("ODTTF", "TTF");
                            FixedPage fixedPage = XamlReader.Load(new MemoryStream(Encoding.Unicode.GetBytes(page))) as FixedPage;
                            fixedPage.Background = Brushes.White;
                            fixedPage.Width = pdlgPrint.PrintableAreaWidth;
                            fixedPage.Height = pdlgPrint.PrintableAreaHeight;
    
                            ElaborateFonts(fpReader, path);
    
                            var pageContent = new PageContent();
                            ((IAddChild)pageContent).AddChild(fixedPage);
                            fixedDocument.Pages.Add(pageContent);
                        }
                    }
                    reference.SetDocument(fixedDocument);
                    sequence.References.Add(reference);
                }
    
                return sequence;
            }
    
            private void ElaborateFonts(IXpsFixedPageReader fpReader, string path)
            {
                foreach (XpsFont font in fpReader.Fonts)
                {
                    var fontpath = font.Uri.ToString().Replace("/Resources/", path).Replace("ODTTF", "TTF");
                    if (!File.Exists(fontpath))
                    {
                        using (Stream stm = font.GetStream())
                        {
                            using (FileStream fs = new FileStream(fontpath, FileMode.Create))
                            {
                                byte[] dta = new byte[stm.Length];
                                stm.Read(dta, 0, dta.Length);
                                if (font.IsObfuscated)
                                {
                                    string guid = new Guid(System.IO.Path.GetFileName(fontpath).Split('.')[0]).ToString("N");
                                    byte[] guidBytes = new byte[16];
                                    for (int i = 0; i < guidBytes.Length; i++)
                                    {
                                        guidBytes[i] = Convert.ToByte(guid.Substring(i * 2, 2), 16);
                                    }
    
                                    for (int i = 0; i < 32; i++)
                                    {
                                        int gi = guidBytes.Length - (i % guidBytes.Length) - 1;
                                        dta[i] ^= guidBytes[gi];
                                    }
                                }
                                fs.Write(dta, 0, dta.Length);
                            }
                        }
                    }
                }
            }
    
    1 person found this answer helpful.

  2. Lo Cedre 6 Reputation points
    2023-01-03T10:03:52.117+00:00

    Same issue displaying xps in a WPF .NET6 application

    FileFormatException: StoryFragments part failed to load.


  3. Lo Cedre 6 Reputation points
    2023-01-12T16:19:38.6266667+00:00

    At this time the only solution i find is to not display the xps document in the application but to open it.

    0 comments No comments