Xpsdocumenwriter从Windows服务挂起,但从控制台运行时工作正常 [英] Xpsdocumenwriter hangs from windows service, but works fine when running from console

查看:103
本文介绍了Xpsdocumenwriter从Windows服务挂起,但从控制台运行时工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于执行各种作业的Window Service,其中一个是打印通过WPF生成的文档。运行控制台应用程序时,它工作正常,但从Windows服务运行时,它只是挂起。



让我解释一下Window Service。为了尽量减少任何可能的副作用,Windows Service只是实际控制台的包装器。因此,当Window Service启动时(在其start方法中),有一个简单的Process.Start调用,它调用控制台,如下所示:

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]");



现在,当调用打印代码时,它就会挂起。我正在使用PrintDialog API来打印文档。打印文档的调用很简单:


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);

当PrintDocument被点击时,它将阻止执行(方法永远不会结束)。



所以,我做了一点调查。谷歌搜索给了我一些提示,所以我确保Windows服务的运行权限与独立控制台相同。

我确保Windows服务作为x64进程运行,与独立控制台相同

我试图调试 PrintDialog.PrintDocument 方法。

我精简地从referencesource下载了代码,它给了我一个新的见解,但不幸的是,这也没有解决问题,它只是进一步指出哪个方法阻止执行(它是 XpsDocumentWriter.Writer 方法,第460行。

现在,在所有这些之后,我确信它必须是关于权限的东西。虽然Windows服务使用相同的用户作为独立控制台应用程序(管理员用户)运行,但在我看来仍然没有所需的所有权限。



Windows服务有什么特别之处吗?我错过了什么,在运行控制台之前还有什么应该为Windows服务设置的吗?



我尝试了什么: < br $>


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);

推荐答案

这可能是因为不再允许它运行GUI应用程序来自Windows服务。

在Windows XP中,这仍然是允许的,但在Windows的更高版本中不允许。



如果你不这样做使用GUI的任何东西,这可能会有所帮助:使用XpsDocument.GetFixedDocumentSequence方法时的Windows服务Eception [ ^ ]

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:

大多数项目创建了b默认情况下,Visual Studio设置为MTA。我必须在STA线程中运行代码。

Most project created by Visual Studio is set to MTA by default. I have to run the code inside STA thread.



你可以尝试的另一件事是为你的项目添加一个清单作为Admin运行:

c# - 如何强制我的.NET应用程序运行为管理员? - 堆栈溢出 [ ^ ]


打印代码似乎挂起,因为它在服务桌面而不是用户桌面下运行。打印对话框显示,但显示在用户永远看不到的服务桌面上。对话框坐在那里等待永远不会来的输入。
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.


这篇关于Xpsdocumenwriter从Windows服务挂起,但从控制台运行时工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆