Repository navigation
Description
- .NET Core Version: 3.0
- Windows version: 1903 (18362.356)
- Does the bug reproduce also in WPF for .NET Framework 4.8?: No
- Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No
Description
I have a Textblock with text of "Hello" within a Canvas on the MainWindow. I am running the following code on mouse down of the main window (as a test):
private void MainWindow_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var sfd = new SaveFileDialog { Filter = "XPS files (*.xps)|*.xps" };
if (sfd.ShowDialog() != true) return;
var c = Canv;
var doc = new XpsDocument(sfd.FileName, FileAccess.Write);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(c, new PrintTicket { PageMediaSize = new PageMediaSize(c.ActualWidth, c.ActualHeight) });
doc.Close();
}
Actual behavior:
Entries cannot be created while previously created entries are still open.
at System.IO.Compression.ZipArchive.AcquireArchiveStream(ZipArchiveEntry entry)
at System.IO.Compression.ZipArchiveEntry..ctor(ZipArchive archive, String entryName)
at System.IO.Compression.ZipArchiveEntry..ctor(ZipArchive archive, String entryName, CompressionLevel compressionLevel)
at System.IO.Compression.ZipArchive.DoCreateEntry(String entryName, Nullable`1 compressionLevel)
at System.IO.Compression.ZipArchive.CreateEntry(String entryName, CompressionLevel compressionLevel)
at System.IO.Packaging.ZipPackage.CreatePartCore(Uri partUri, String contentType, CompressionOption compressionOption)
at System.IO.Packaging.Package.CreatePart(Uri partUri, String contentType, CompressionOption compressionOption)
at System.Windows.Xps.Packaging.XpsManager.GeneratePart(ContentType contentType, Uri partUri)
at System.Windows.Xps.Packaging.XpsManager.GenerateUniquePart(ContentType contentType)
at System.Windows.Xps.Packaging.XpsFixedDocumentSequenceReaderWriter.AddFixedDocument()
at System.Windows.Xps.Serialization.XpsPackagingPolicy.AcquireXmlWriterForFixedDocument()
at System.Windows.Xps.Serialization.XpsSerializationManager.AcquireXmlWriter(Type writerType)
at System.Windows.Xps.Serialization.ReachHierarchySimulator.SimulateBeginFixedDocument()
at System.Windows.Xps.Serialization.ReachHierarchySimulator.BeginConfirmToXPSStructure(Boolean mode)
at System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject)
at System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
at System.Windows.Xps.XpsDocumentWriter.Write(Visual visual, PrintTicket printTicket)
at Test.MainWindow.MainWindow_OnMouseDoubleClick(Object sender, MouseButtonEventArgs e) in C:\Users\ashdo\source\repos\Test\Test\MainWindow.xaml.cs:line 27
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.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 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, 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 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()
at Test.App.Main()
Expected behavior: An XPS document to be created of the Canvas
Minimal repro:
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" MouseDown="MainWindow_OnMouseDoubleClick">
<Grid>
<Canvas Name="Canv">
<TextBlock FontSize="200" Foreground="BlueViolet" Text="Hello" >
</TextBlock>
</Canvas>
</Grid>
</Window>
Activity
rladuca commentedon Oct 14, 2019
The underlying zip implementation does not support the stream being kept open when opening it in write mode.
This seems to be just another tail end bug of switching zip implementations.
@EmaginationStore can you try opening with FileAccess.ReadWrite? That allowed me to continue properly. I believe the reason for this is the underlying stream does specific things when in Create mode.
https://github.com/dotnet/corefx/blob/967eb3c5cb5fd36c8bb56d06f4c840f6a7a5a754/src/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs#L138
Setting the FileAccess to ReadWrite makes this function in Update mode, which skirts the issue.
There are changes needed in WPF for this, but you should be able to workaround this in the meantime.
EmaginationStore commentedon Oct 15, 2019
I can confirm that creating the XpsDocument with ReadWrite file mode did allow me to create the XPS document. Thank you
rladuca commentedon Oct 18, 2019
After some investigation, we have several assumptions in the WPF XPS document stack that rely on being able to simultaneously add to a
ZipArchive. Specifically, the serialization managers will do something akin to this:wpf/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachSerializationUtils.cs
Line 312 in ac9d1b7
and
wpf/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/ReachSerializationUtils.cs
Line 368 in ac9d1b7
Basically, this sort of split logic keeps the underlying streams open until the entire set of tags are written out. This design persists down into the packaging policies that are used to manage the
PackageParts.In the past, we have fixed several instances of places where we were not closing these streams eagerly, but this case is a bit more complicated. We'll need to think more carefully about if/how to change the serialization design to conform to the new requirements from System.IO.Compression.
In the meantime, I think the best fix for 3.1+ would be to implement the workaround here in the code itself. When an
XPSDocumentis created withFileAccess.Write, we should promote that toFileAccess.ReadWriteso that theZipArchiveends up in Update mode and can be manipulated as it was in .NET Framework.We can revisit the design of the serialization stack for 5.0. I'll file another bug for that when this one is completed.
rladuca commentedon Oct 22, 2019
Merged for 5.0 and 3.1