Skip to content

.NET core WPF print dialog freeze/not supported exception #2609

Open

Description

@Zubastic
  • .NET Core Version: 3.1.101, 3.1.102
  • Windows version: 1607 14393.3504
  • 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

Problem description:
After select print button error appears.

Actual behavior:
NotSupportedException when I selected virtual printer (for example Microsoft Print to PDF). When selected in dialog real one - nothing happends, just app became unresponsible for infinity time. At debugger it stuck at WaitHandle:

изображение

System.NotSupportedException: Specified method is not supported.
   at MS.Internal.PrintWin32Thunk.XpsPrintJobStream.Seek(Int64 offset, SeekOrigin origin)
   at System.Printing.PrintQueueStream.Seek(Int64 offset, SeekOrigin origin)
   at System.IO.Compression.ZipArchive.WriteFile()
   at System.IO.Compression.ZipArchive.Dispose(Boolean disposing)
   at System.IO.Compression.ZipArchive.Dispose()
   at System.IO.Packaging.ZipPackage.Dispose(Boolean disposing)
   at System.IO.Packaging.Package.System.IDisposable.Dispose()
   at System.IO.Packaging.Package.Close()
   at System.Windows.Xps.Packaging.XpsDocument.DisposeXpsDocument()
   at System.Printing.PrintQueue.DisposeSerializationManager(Boolean abort)
   at System.Windows.Xps.XpsDocumentWriter.EndWrite(Boolean disposeManager, Boolean abort)
   at System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
   at System.Windows.Xps.XpsDocumentWriter.Write(DocumentPaginator documentPaginator, PrintTicket printTicket)
   at System.Windows.Controls.PrintDialog.PrintDocument(DocumentPaginator documentPaginator, String description)
   at WpfNetcoreTest.Program..ctor() in C:\Users\Zubastic\Source\Repos\Zubastic\PrintDialogBug\WpfNetcoreTest\Program.cs:line 18
   at WpfNetcoreTest.App..ctor() in C:\Users\Zubastic\Source\Repos\Zubastic\PrintDialogBug\WpfNetcoreTest\App.xaml.cs:line 18
   at WpfNetcoreTest.App.Main()

Expected behavior:
I could print PDF document at printer/save pdf file.

Minimal repro:
Repo for reproduce bug:
https://github.com/Zubastic/PrintDialogBug

@rladuca could u help me with that issue? 1607 is important for me, because enterprise use LTSB version. At 1809 and newer version all is good.

Activity

  1. added
    Bug
    on Feb 20, 2020
  2. dmarcello53 commented on Feb 24, 2020

    @dmarcello53

    Oh well !

  3. removed their assignment
    on Apr 24, 2020
  4. hnhai commented on Aug 14, 2020

    @hnhai

    Any dotnet core version fixed this bug ? @dmarcello53 . Please

  5. Zubastic commented on Aug 19, 2020

    @Zubastic
    Author

    @hnhai windows 1903 and newer is fixed.

  6. Zubastic commented on Aug 19, 2020

    @Zubastic
    Author

    bump, @danmosemsft could you help with this issue?

  7. danmoseley commented on Aug 19, 2020

    @danmoseley

    @Zubastic unfortunately i don't have knowledge of WPF code. Does this mean printing is just broken or only in this particular repo?

    @ryalanms is the primary developer in this repo - perhaps he can help.

  8. Zubastic commented on Aug 19, 2020

    @Zubastic
    Author

    @danmosemsft it depends on windows version. At 1607 its broken (LTSB for enterprise users), at 1809 or newer - it is OK. But enterprise couldn't update :(
    Thank you for assistance.

  9. danmoseley commented on Aug 19, 2020

    @danmoseley

    cc @fabiant3 also

  10. Zubastic commented on Aug 20, 2020

    @Zubastic
    Author

    Well, additional info about bug:

    class System.Printing.PrintQueue
    1809:
    1809

    1607:
    1607

    if (this.IsXpsOMPrintingSupported()) returning true for 1809 and false for 1607. After that flag this.isWriterAttached is set. And at dispose method exception was thrown at ZipPackage.Dispose() (all stack at first post).
    Check ?isXpsOMPrintingSupported@?1??IsXpsOMPrintingSupported@PrintQueue@Printing@System@@QE$AAM_NXZ@$$Q4_NA; returning different values.

  11. rladuca commented on Aug 20, 2020

    @rladuca
    Member

    IsXpsOMPrintingSupported is pegged to Windows 10 1703 (RS2) and above. This has always been this way since its inception (in .NET Framework, I believe in the 4.7/4.7.1 timeframe).

    The problem is likely to be that the serialization manager for the legacy path, NgcSerializationManager, is likely not managing streams in a way conducive to the new underlying packaging libraries. This is, generally, the root cause of almost all printing (and XPS for that matter) issues in .NET Core. The management of streams for packages needs to be overhauled to match System.IO.Packaging and System.IO.Compression. The old assumptions WPF is built on when it contained its own packaging and zip libraries no longer apply.

    This isn't a workaround, but a potential test. If you force the value there to be true (perhaps forcing the static underlying the function call here) do things work? Note there may be other instances where another piece of code needs to pivot on this sort of check for printing support, but I don't have a full understanding of all of those places (@miguep maybe?).

    @ryalanms One thing you might want to check is if you can just remove the OS check or extend support for it downward. The minimum client is low, but this was guarded so as to not explicitly change behavior on applications targeting lower versions. For .NET Core it might make sense to remove this and move forward. It would need good testing, but might help alleviate the pain of fixing up the legacy serialization classes. Of course, that could just be the outside layer of issues, I haven't tried.

  12. Zubastic commented on Aug 20, 2020

    @Zubastic
    Author

    This isn't a workaround, but a potential test. If you force the value there to be true (perhaps forcing the static underlying the function call here) do things work?

    I change check if (this.IsXpsOMPrintingSupported()) to true and get this exception:
    Exception

  13. Zubastic commented on Aug 20, 2020

    @Zubastic
    Author

    Well, if change flag to false and return NgcSerializationManager, it work.
    flag

  14. self-assigned this
    on Aug 24, 2020
  15. added this to the 5.0.0 milestone on Aug 24, 2020
  16. added
    urgent
    on Aug 24, 2020
  17. ericstj commented on Oct 26, 2020

    @ericstj
    Member

    cc @stevenbrix @rladuca

    This appears related to a57ed01

    In this case, based on the callstack, it seems that PrintQueueStream is exposing CanSeek (and CanRead) as true

    Boolean
    PrintQueueStream::CanRead::
    get(
    void
    )
    {
    return true;
    }
    Boolean
    PrintQueueStream::CanWrite::
    get(
    void
    )
    {
    return true;
    }
    Boolean
    PrintQueueStream::CanSeek::
    get(
    void
    )
    {
    return true;
    }

    But then is directly calling down to the SpoolStream:

    return printerThunkHandler->SpoolStream->Seek(offset, origin);

    And the SpoolStream is created with CanRead as false

    spoolerStream = gcnew XpsPrintJobStream((IXpsPrintJobStream *)tempDocStream, tempCompletedEvent, false, true);

    and hardcodes CanSeek to false:
    Boolean
    XpsPrintJobStream::CanSeek::
    get(
    void
    )
    {
    return false;
    }

    Perhaps the fix here is to simply update PrintStream to delegate the Can* properties to the SpoolStream like it is doing for the actual Read|Write|Seek methods. That will let the calling code "know" that the backing stream cannot read or seek and write the Package/ZipArchive in a way that doesn't do any seeking.

  18. ryalanms commented on Jul 28, 2021

    @ryalanms
    Member

    This was fixed in .NET 5.0 in our internal repo (in PresentationNative), but was not backported to 3.1.

    /cc @dotnet/wpf-developers

  19. modified the milestones: 5.0.0, 6.0.0 RC2 on Aug 26, 2021
  20. removed this from the .NET 3.1 Servicing milestone on Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Bugurgent

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions