Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF打印(XpsDocumentWriter)在调试中工作,但在部署中不工作_C#_Wpf_Printing - Fatal编程技术网

C# WPF打印(XpsDocumentWriter)在调试中工作,但在部署中不工作

C# WPF打印(XpsDocumentWriter)在调试中工作,但在部署中不工作,c#,wpf,printing,C#,Wpf,Printing,希望一些有经验的WPF开发人员以前遇到过这个问题 背景:此信息对于帮助解决问题可能不是必需的,但在相关情况下。 我的解决方案由三个项目组成。前端GUI、业务逻辑服务和打印机服务。这三个项目通过命名管道实现IPC。业务逻辑将标签类型和托盘id交给打印逻辑 问题:打印逻辑然后创建标签并打印(通过将其添加到打印机的打印队列),正如标题所示,在visual studio中调试时,这一切都可以正常工作。但是,当我在开发人员pc上部署/安装服务时,它不起作用 更新:它没有引发异常,但我只记录了“将文档发送到

希望一些有经验的WPF开发人员以前遇到过这个问题

背景:此信息对于帮助解决问题可能不是必需的,但在相关情况下。 我的解决方案由三个项目组成。前端GUI、业务逻辑服务和打印机服务。这三个项目通过命名管道实现IPC。业务逻辑将标签类型和托盘id交给打印逻辑

问题:打印逻辑然后创建标签并打印(通过将其添加到打印机的打印队列),正如标题所示,在visual studio中调试时,这一切都可以正常工作。但是,当我在开发人员pc上部署/安装服务时,它不起作用

更新:它没有引发异常,但我只记录了“将文档发送到打印机”而不是“将文档发送到打印机”行,因此它挂在
dw1.Write(fixedDoc)上

更多信息:我正在打印项目/visual studio 2013中使用.Net 4.0

        public void printLabel(string labelType, string _palletID = null)
    {
        try
        {
            ILabelTemplate Label                = createLabel(labelType, _palletID);

            PrintDialog pd                      = new PrintDialog();
            FixedDocument fixedDoc              = new FixedDocument();
            PageContent pageContent             = new PageContent();

            FixedPage fixedPage                 = getFixedPage();
            fixedDoc.DocumentPaginator.PageSize = new System.Windows.Size(fixedPage.Width, fixedPage.Height);

            IXamlTemplate vm                    = CreateViewModel(Label);
            ILabelPrintDocument template        = CreateTemplate(Label);

            template.dockPanel.DataContext      = vm;
            template.dockPanel.Height           = fixedPage.Height;
            template.dockPanel.Width            = fixedPage.Width;
            template.dockPanel.UpdateLayout();


            fixedPage.Children.Add(template.dockPanel);
            ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
            fixedDoc.Pages.Add(pageContent);

            XpsDocumentWriter dw1 = PrintQueue.CreateXpsDocumentWriter(new System.Printing.PrintQueue(new System.Printing.PrintServer(), Label.PrinterName));
            Library.WriteErrorLog("About to send doc to printer");
            dw1.Write(fixedDoc);
            Library.WriteErrorLog("Sent doc to printer");


        }
        catch (Exception ex)
        {

            Library.WriteErrorLog(ex);
        }

已解决。。。有点

经过几个小时的尝试和阅读,我发现这是由于我的应用程序在调试时以我的身份运行,但在部署时以本地系统的身份运行。本地系统服务不能访问网络资源,如打印机。尽管学会了这一点,我还是开始学习如何制作C#service打印。在看到很多帖子之后(在游戏中太晚了,没有太多帮助)

我也知道我走错了路

这个故事的寓意是,如果你正在阅读这篇文章,你可能还没有达到“使用Win32 API(例如,在C/C++中)编写你自己的打印DLL,然后从你的服务中使用p/Invoke”的水平

对我来说,可行的解决方案不是将此项目作为通过GUI启动的服务运行。相反,我把它变成了一个仍然通过GUI启动和停止的过程

有问题的代码是

                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe"))
            {
                Process.Start(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe");
            }
然后当GUI关闭时,我运行代码

        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe"))
        {
           Process[] myapps = Process.GetProcesses("yourAppNameGoesHere.exe");
           foreach (Process _p in myapps)
           {
               _p.Kill();
           }

        }