I have a Window Service that is used to perform various jobs, among them, one is to print a document that is generated through WPF. When running the console application, it works fine, but when running from windows service, it just hangs.
Let me explain a little bit the Window Service. In order to minimize any possible side effect, Windows Service is just a wrapper for the actual console. So, when Window Service is started (in its start method), there is a simple Process.Start call that calls the console, like this:
Process.Start("[path_to_my_console_exe]");
Now, when the code for printing is invoked it just hangs. I am using PrintDialog API to print a document. The call to print a document is simple:
var printDialog = new PrintDialog();
printDialog.PrintDocument(doc.DocumentPaginator);
When PrintDocument is hit, it will block the execution (method will never end).
So, I did a little investigation. Googling gave me some hints, so I made sure that Windows Service is running with same permissions as is standalone console.
I made sure that Windows Service is running as x64 process, same as standalone console
I tried to debug the
PrintDialog.PrintDocument
method.
I literately downloaded the code from referencesource and it gave me a new insight, but unluckily, that didn't solve the problem neither, it just pinpointed further which method is blocking the execution (it is
XpsDocumentWriter.Writer
method, line 460).
Now, after all this, I am sure it must be something regarding permissions. Although the Windows Service is using the same user to run as a standalone console app (admin user) it seems to me that is still doesn't have all permissions needed.
Is there something special with Windows Service here? Am I missing something, is there anything else what should be set for Windows Service before running the console?
What I have tried:
Process.Start("[path_to_my_console_exe]");
.
.
.
var printDialog = new PrintDialog();
printDialog.PrintDocument(doc.DocumentPaginator);