Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:
C#
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);
Posted
Updated 9-Apr-19 3:40am
v2

This is probably because it is not allowed anymore to run GUI applications from a Windows Service.
In Windows XP this was still allowed, but not in later versions of Windows.

If you don't use anything with a GUI, then this might help: Windows Service Eception when using XpsDocument.GetFixedDocumentSequence Method[^]
Quote:
Most project created by Visual Studio is set to MTA by default. I have to run the code inside STA thread.

Another thing you can try is adding a manifest to your project to run as Admin:
c# - How do I force my .NET application to run as administrator? - Stack Overflow[^]
 
Share this answer
 
v3
The print code appears to hang because it's running under the services desktop, not the user desktop. The dialog for printing shows up, but is shown on the services desktop which the user can never see. The dialog is sitting there waiting for input that will never come.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900