Windows服务凭据
问题描述:
我有一个Windows服务应打印出HTML文本。
操作系统是7,使用.NET 4.5.1和IE 11.Windows服务凭据
的代码:
private static void PrintOnStaThread(string text)
{
using (WebBrowser webBrowser = new WebBrowser())
{
webBrowser.Navigate("about:blank");
webBrowser.Document.OpenNew(false);
while (webBrowser.DocumentText != text && webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
webBrowser.Document.Write(text);
webBrowser.Refresh();
Application.DoEvents();
}
InternetExplorer internetExplorer = (InternetExplorer)webBrowser.ActiveXInstance;
internetExplorer.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, Win32.PRINT_WAITFORCOMPLETION);
}
}
问题:
当服务使用记录的帐户Local System
的ExecWB
函数什么都不做线程正在退出,没有任何异常。当使用管理员凭据登录服务时,ExecWB
函数完美工作,但我不想使用此帐户。
任何意见将受到欢迎。
答
这是可能的,但您需要将您的服务配置为“交互式”。
有关更多信息,请查看此文档http://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspx
+0
但这是一个坏主意...... – FZysset 2014-10-03 13:10:55
请勿在Windows服务中使用WinForms。问题很可能是由此造成的。使用不同的方式来打印HTML。如果您必须,请参阅[在C#中不使用打印对话框打印Windows Service中的html文档](http://*.com/questions/416314/print-html-document-from-windows-service-in-c-sharp-without -print-dialog),但仍然使用“WebBrowser”控件。另请参见[从Windows服务打印html文档而不打印对话框](http://*.com/questions/419412/print-html-document-from-windows-service-without-print-dialog)。 – CodeCaster 2014-10-03 13:01:10
我已经知道并在我的代码中使用了第一个示例。我不明白“不要使用WinForms”是什么意思。我不认为我是。 – toy4fun 2014-10-03 13:43:52