Printing EMF from a Windows service

I have some code that prints a document using the System.Drawing.Printing namespace. This now needs to be moved into a windows service. The documentation states that this is not possible, which would support my findings so far (in that it doesn't work). I'm working under the assumption that I can get this to work by using the System.Printing namespace instead.

Here is the code that I have so far:

private Metafile myDoc;

myDoc = GetEmfDoc();

PrintServer ps = new PrintServer("\\server1\printer1");
EnumeratedPrintQueueTypes[] flags = { EnumeratedPrintQueueTypes.Local };
PrintQueueCollection queues = ps.GetPrintQueues(flags);

PrintQueue pq = queues.First<PrintQueue>();                
pq.AddJob("JobName", ?);

So I want to pass myDoc to pq.AddJob(), but this doesn't seem to be possible. Using System.Drawing.Printing it was possible to simply render this on a page by page basis in the PrintDocument.PrintPage event.

Is what I'm trying to do possible, and if so, could someone point me in the right direction?

Answers


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.

It is important to understand what "not supported" means in this context. It literally means what it says, it you try to print from a service and discover a problem then you cannot call Microsoft Support and ask for help.

This is not a problem that's induced by System.Drawing, it is a generic problem with printer drivers. Chunks of code that are supplied by the printer manufacturer, not Microsoft. A significant problem with them is that they are invariably designed to be used from a desktop program. Word, Acrobat, etcetera. They readily take advantage of that, they often pop up a window to let you know that they are Working On It. Not in the least to spam the manufacturer name and reminding you that the toner cartridge is half empty. These drivers are likely to misbehave in a service. The "runtime exceptions" phrase mentioned in the note.

The worst problem with them is that they use a popup window to let you know that there's a problem. These windows are not visible when you use the printer driver from a service. So you'll have to deal with print jobs that for no discernible reason refuse to print. The "diminished service performance" phrase in the note. That's very difficult to deal with, IT staff is just powerless to have a shot at fixing the problem when they can't see any diagnostic.

Your approach doesn't solve that fundamental problem. If you really want to pursue this then contact the printer manufacturer and ask for specific advise. Don't get your hopes up, getting good answers out of hardware companies is pretty difficult.


Need Your Help

Drop all tables whose names begin with a certain string

sql sql-server scripting dynamic-sql

I'd like a script to drop all tables whose name begins with a given string. I'm sure this can be done with some dynamic sql and the INFORMATION_SCHEMA tables.

Using jQuery Plug-in (jquery-autosave) to an MVC 3 controller method via ajax

jquery ajax asp.net-mvc-3 callback autosave

My javascript/ajax inexperience is probably preventing me from bridging a gap here. I have implemented a jQuery plugin called jQuery-autosave. The change event on my form control fires the method...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.