从Windows打印服务在Windows 8失败 [英] Printing from windows service on windows 8 fails

查看:282
本文介绍了从Windows打印服务在Windows 8失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在C#.NET Windows服务应用程序。本申请是用来生成通过打印文档以生成PDF的本地软件打印机报告PDF格式。这适用于Windows XP和Windows 7。不幸的是,我发现在Windows 8失败。后来我发现,打印在Windows 8的(甚至物理)打印机时,我从服务打印失败。
缺什么在我的计划工作?我打印这种方式:

I have a windows service application written in C# .NET. This application is used to generate a report pdf by printing document to local software printer that generates PDFs. This works well on Windows XP and Windows 7. Unfortunately I found that on Windows 8 it fails. Then I found out that printing to any (even physical) printer on Windows 8 fails when I print from my service. What is missing in my program to work? I'm printing this way:

FlowDocument document = MyDocument;
var source = document as IDocumentPaginatorSource;
var documentPaginator = source.DocumentPaginator;

using (var printServer = new LocalPrintServer())
{
    PrintQueue queue = printServer.GetPrintQueue(printerName);
    XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(queue);

    // Print ticket - Approach 1
    // PrintTicket printTicket = queue.DefaultPrintTicket.Clone();

    // Print ticket - Approach 2
    var printTicket = new PrintTicket
    {
        PageOrientation = PageOrientation.Landscape,
        PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4), // set size of media (paper)
    };


    documentPaginator.PageSize = new Size(document.PageWidth, document.PageHeight);
    docWriter.Write(documentPaginator, printTicket);
}



服务设置为系统帐户没有与桌面交互(但我试过,太多或登录本地用户)

Service is set to 'system account' without 'interacting with desktop' (but I tried that too or to login as local user).

这将导致插入Windows 8的例外,当使用打印票 - 方法1:

This results into exception on Windows 8. When using 'Print ticket - Approach 1':

System.Printing.PrintQueueException: PrintTicket provider failed to bind to printer. Win32 error: -2147467231
at MS.Internal.Printing.Configuration.PTProvider..ctor(String deviceName, Int32 maxVersion, Int32 clientVersion)
at MS.Internal.Printing.Configuration.PTProviderBase.Create(String deviceName, Int32 maxVersion, Int32 clientVersion)
at System.Printing.PrintTicketManager..ctor(String deviceName, Int32 clientPrintSchemaVersion)
at System.Printing.PrintQueue.get_DefaultPrintTicket()

使用打印票 - 方法2:

Using 'Print ticket - Approach 2':

Exception encountered: System.Printing.PrintQueueException: Fehler beim Binden des PrintTicket-Anbieters an den Drucker. Win32-Fehler: -2147467231
bei MS.Internal.Printing.Configuration.PTProvider..ctor(String deviceName, Int32 maxVersion, Int32 clientVersion)
bei MS.Internal.Printing.Configuration.PTProviderBase.Create(String deviceName, Int32 maxVersion, Int32 clientVersion)
bei System.Printing.PrintTicketManager..ctor(String deviceName, Int32 clientPrintSchemaVersion)
bei System.Printing.PrintQueue.get_UserPrintTicket()
bei System.Printing.PrintQueue.get_CurrentJobSettings()
bei System.Printing.PrintQueue.CreateSerializationManager(Boolean isBatchMode, Boolean mustSetJobIdentifier)
bei System.Windows.Xps.XpsDocumentWriter.BeginWrite(Boolean batchMode, Boolean asyncMode, Boolean setPrintTicketHandler, PrintTicket printTicket, PrintTicketLevel printTicketLevel, Boolean printJobIdentifierSet)
bei System.Windows.Xps.XpsDocumentWriter.Write(DocumentPaginator documentPaginator, PrintTicket printTicket)

我会说,服务是能够找到这些打印机,因为当我试图打印到不存在的打印机,我得到了无效的打印机名称的异常。

I would say that service is able to find those printers because when I have tried to print to non-existing printer and I got "invalid printer name" exception.

下面我会继续为我自己的一些相关的问题:
从Windows服务
从Windows服务打印打印
的http://社会.msdn.microsoft.com /论坛/ EN-US / ieextensiondevelopment /线程/ b74bd27d-1cc8-4fca-a6de-2cd1371cf3b7 /

Here I'll keep for myself some related questions: Printing from a Windows Service, Printing from a Windows Service, http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/b74bd27d-1cc8-4fca-a6de-2cd1371cf3b7/,

轻度相关:从.NET服务
印刷,

Mildly related: Printing from a .NET Service,

编辑:

在万一有人有兴趣尝试 - 这里是试图在配置选择打印简单的文件与打印机我的示例服务应用程序文件: http://bin.mypage.sk/FILES/PrintTestService.rar

In case someone is interested in trying - here is my sample service application that tries to print simple document to printer selected in config file: http://bin.mypage.sk/FILES/PrintTestService.rar

EDIT2:

有趣。当我尝试了不同的打印代码没有任何错误:

Interesting. When I tried a different printing code there is no error:

using (var printDocument = new PrintDocument())
{
    printDocument.PrinterSettings.PrinterName = printerName;
    printDocument.Print();
}



不幸的是这是一个使用System.Drawing.Graphics库年长GDI +代码,是不是我的代码,在System.Windows.Media.Visual对象的形式产生分页文件兼容。所以我不能用它来打印文档,除非我想花从头开始创建我的文档分页两周

Unfortunately this is a older GDI+ code using the System.Drawing.Graphics library which is not compatible with my code that produces paginated document in form of System.Windows.Media.Visual objects. So I can't use it to print my document unless I'd like to spend two weeks of creating the pagination of my document from scratch.

EDIT3:

有就是对这个问题的讨论在这里:的 http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/96e7fc10-3f08-4808-b748-692e30377293
有一个处理方法为它使用'值为anycpu'的平台。此解决方法还真管用(我试过),但是当我的服务需要是x86它不是我的情况下使用。我所接触的MS支持通过我们公司找一个真正的解决办法

There is discussion about this issue here: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/96e7fc10-3f08-4808-b748-692e30377293 There is a 'workaround' for it to use 'anyCPU' platform. This workaround really works (I tried it) but it is not usable in my case when my service needs to be x86. I have contacted MS support through our company to find a real solution.

推荐答案

解决方案:要等待微软修复这个bug。我报告的bug(REG:113040910349062),他们已经证实,但弄不好会不会很快(在接下来的几个月内),也没有为它指定的日期

Solution: To wait for Microsoft to fix this bug. I have reported the bug (REG:113040910349062), they have confirmed it, but fix won't be soon (in next months) and there is no date specified for it.

解决方法,我们使用:
创建一个小型应用程序,处理打印,其编译为'值为anycpu应用程序并从服务应用程序运行它(我不能编译为'值为anycpu' - 我需要'86'由于其依赖关系)。这种解决方法进行了测试和作品。但它是肮脏+引起的开销+额外的照顾

Workaround that we use: Create a small application that handles the printing, compile it as 'anyCPU' application and run it from service application (which I can't compile as 'anyCPU' - I need 'x86' because of its dependencies). This workaround was tested and works. But it is dirty + causes overhead + extra care.

编辑:微软提供了一个修补程序应该在Windows 8中解决这个问题:的 http://support.microsoft.com/kb/2872151/EN-US (你不能从32打印在Windows 8或Windows Server 2012)

Microsoft provides a hotfix that should fix this problem in Windows 8 : http://support.microsoft.com/kb/2872151/EN-US ( "You cannot print from a 32-bit WPF application in an x64-based version of Windows 8 or Windows Server 2012" )

编辑(2013年11月4日):我们已经测试了修补程序,并确实在Windows 8或Windows 8.1无法正常工作。目前我们与微软沟通的阶段又是

Edit (2013-11-04): we have tested the hotfix and it does not work on Windows 8 or Windows 8.1. Currently we are again in a phase of communicating with Microsoft.

编辑(2014年初)的:我们已经收到了来自微软的回应,他们不知道如何修复这个bug和漏洞被关闭。此刻我们因此唯一的解决办法是有编译为值为anycpu这是我们的x86服务,称为一个小应用程序的EXE。这种方法有效,但会导致经济放缓,我们的产品也困难,因为这个维护。

Edit (2014-beginning of the year): we have received a response from Microsoft that they don't know how to fix this bug and the bug is closed. So only solution for us at the moment is having a small app exe compiled as AnyCPU which is called by our x86 service. This approach works, but causes slowdown and our product is also harder to maintain because of this.

这篇关于从Windows打印服务在Windows 8失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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