简体   繁体   English

XpsDocumenWriter从Windows Service挂起,但是从控制台运行时可以正常工作

[英]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. 我有一个Window Service,用于执行各种作业,其中之一是打印通过WPF生成的文档。 When running the console application, it works fine, but when running from windows service, it just hangs. 运行控制台应用程序时,它可以正常运行,但是从Windows服务运行时,它只是挂起。

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. 为了最大程度地减少任何副作用,Windows Service只是实际控制台的包装。 So, when Window Service is started (in its start method), there is a simple Process.Start call that calls the console, like this: 因此,在启动Window Service(在其start方法中)时,有一个简单的Process.Start调用会调用控制台,如下所示:

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. 我正在使用PrintDialog API打印文档。 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). 当点击PrintDocument时,它将阻止执行(方法永远不会结束)。

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. 我确保Windows服务以与独立控制台相同的权限运行。
  • I made sure that Windows Service is running as x64 process, same as standalone console 我确保Windows Service与独立控制台一样以x64进程运行
  • I tried to debug the PrintDialog.PrintDocument method. 我试图调试PrintDialog.PrintDocument方法。 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). 我从参考资料源中下载了代码,它给了我新的见解,但是不幸的是,这也没有解决问题,它只是进一步指出了哪个方法阻止了执行(这是XpsDocumentWriter.Writer方法,第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. 尽管Windows服务使用同一用户作为独立控制台应用程序(管理员用户)运行,但在我看来,它仍然没有所需的所有权限。 Is there something special with Windows Service here? Windows服务在这里有什么特别之处吗? Am I missing something, is there anything else what should be set for Windows Service before running the console? 我是否缺少某些东西,在运行控制台之前,还应该为Windows Service设置什么?

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. 为了测试该应用程序,我使用的是Microsoft pdf打印机,因此当将控制台作为独立应用程序运行(不通过服务)时,使用Microsoft pdf打印机时,它将在最后显示一个对话框,询问文件名和位置。 。 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" . 但是,当运行相同的控制台,通过Windows Service并使用同一台打印机(Microsof pdf)进行打印时,它只会挂起,原因是服务属性“允许服务与桌面进行交互” 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. 默认情况下为false,因此在注册服务时,任何用户交互都不可见,在我的情况下,这正是PrintDialog挂起的确切时间,因为它正在等待用户输入以保存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). 解决此问题的另一种方法是使用不同的打印API,无需用户交互(通过以编程方式传递参数)即可使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从WebAPI项目运行静态类时出现StackOverflow异常-从控制台应用程序运行时运行正常 - StackOverflow exception when running static class from a WebAPI project - works fine when run from a console application 代码在控制台应用程序中有效,但在Windows Service中运行时不起作用 - Code works in a console application, but does not work when running in windows service SQLite ExecuteReader可从Console和Forms Apps运行,但不能从Windows Service运行 - SQLite ExecuteReader works from Console and Forms Apps but not from Windows Service 从Windows服务写入控制台时出现ArgumentOutOfRangeException - ArgumentOutOfRangeException when writing to console from windows service 从Windows服务和控制台应用程序访问相同的运行代码? - Access the same running code from a Windows Service and a console application? 在控制台中运行Windows服务 - Running a windows service in a console 从Web应用程序调用Web服务时拒绝连接时,可以从本地主机正常工作 - Getting connection refused when calling a web service from a web app works fine from localhost 当 exe 从启动运行时,log4net 不记录日志,但在手动启动时工作正常 - log4net not logging when the exe is running from startup but works fine when started manually PSExec暂停运行测试,但在调试会话上运行良好 - PSExec Hangs On Running A Test but Works Fine on Debug Session 访问拒绝运行远程PowerShell,但仅当从开发服务器运行时才能正常工作 - Access Denied running Remote PowerShell but only when ran from dev server, works fine localy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM