简体   繁体   中英

XpsDocumenWriter hangs from Windows Service, but works fine when running from console

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?

I solved the issue, so if anyone gets in the same situation, here is what happend.

For testing the app, I was using Microsoft pdf printer, so when running the console as a standalone app (not through the service), when Microsoft pdf printer is used it would show a dialog at the end to ask for the file name and location. That could work because I could see a dialog and press the button. But, when running the same Console, through Windows Service, and using the same printer (Microsof pdf) for printing it would just hang and the reason is the service property "Alow service to interact with desktop" . This is by default false, so when registering a service, any user interaction won't be visible and in my case it was the exact point where PrintDialog hangs, because it is waiting for a user input to save the pdf. After changing to a real printer, it all worked as excpected.

Another way to solve this is to use different printing API, that could work without user interaction (by passing the arguments programatically).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM