none
Is there really no way to print from a service? RRS feed

  • Question

  • I need to print from a server-side application (either a Windows Service or an ASP.NET application). 

    It looks like there is no way to do so, from managed code.  System.Drawing.Printing is out, due to the big warning here:  

    "Classes within the System.Drawing.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions."

    The other alternative is System.Printing.  However, that is considered to be part of WPF, and this page says:

    "The use of WPF to build Windows services is unsupported. Because WPF is a presentation technology, the Windows service requires the appropriate permissions to perform visual operations that involve user interaction. If the Windows service does not have the appropriate permissions, there may be unexpected results."

    So, going by the documentation, there is no supported way to print from managed code server side.  That seems like a bizarre situation.  Is it really true?

    John


    John Rusk, http://dotnet.AgileKiwi.com - .NET and Agility
    Sunday, August 23, 2009 8:55 PM

Answers

  • Hmm, that is a wee bit odd.  PrintDocument and PrintController are pretty thin wrappers around the Windows API functions (StartDoc, StartPage).  These APIs don't have the "do not use in a service" warning.  It is possible to invoke dialogs implemented by the printer driver, they are not going to work well of course.  But that's easy enough to avoid if you configure the machine properly.

    If you really need this, then just try it.  There's always a forum around to get help with weirdo exceptions.

    Hans Passant.
    Monday, August 24, 2009 10:16 PM
    Moderator

All replies

  • What is the end goal? Maybe we can provide an alternate.

    William Wegerson (www.OmegaCoder.Com)
    Monday, August 24, 2009 6:38 PM
    Moderator
  • You can print a text file using the Print command.
    Monday, August 24, 2009 6:46 PM
  • Hmm, that is a wee bit odd.  PrintDocument and PrintController are pretty thin wrappers around the Windows API functions (StartDoc, StartPage).  These APIs don't have the "do not use in a service" warning.  It is possible to invoke dialogs implemented by the printer driver, they are not going to work well of course.  But that's easy enough to avoid if you configure the machine properly.

    If you really need this, then just try it.  There's always a forum around to get help with weirdo exceptions.

    Hans Passant.
    Monday, August 24, 2009 10:16 PM
    Moderator
  • I think the company I work for has done this without problems (we didn't even see that warning in MSDN), but we may have been lucky. Printing from a service does have it's own issues though... can you see the relevant printer from the server, does the account the service is running as have permission to access it, how do you show the user the list of printers from the server rather than the ones their own PC can see etc.

    You could investigate the XPS document stuff in .Net 3.5, there may be a safe way to print those from services, but it will mean creating an XPS document from your data. I don't know if that will work, it's just a suggestion.

    As Hans said, perhaps just try it and see.
    Monday, August 24, 2009 10:46 PM
  • Update: yes, after dialog with Microsoft, I can confirm that there really is no supported way to print, from a service, in pure managed code.

    However, you can use PInvoke to call GDI or, my preferred option, create your desired output as an XPS document, then use a managed wrapper to call the new XPS Print API which is getting released this week (late Oct 2009) for Vista, Server 2008 and Windows 7.  For details, and link to sample code, see end of this thread: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0d5ef185-4ac9-47de-8f88-dc06c59375b9


    John Rusk, http://dotnet.AgileKiwi.com - .NET and Agility
    Monday, October 19, 2009 11:20 PM