0

Hopefully some of the experienced WPF developers have come across this issue before.

BACKGROUND: This information is probably not necessary to helping fixing the problem, but in case it is relevant. My solution consists of three projects. A front-end GUI, a business logic service, and a printer service. The three projects have IPC via named pipes. The business logic hands the printing logic a label type and a pallet id.

The Problem: The printing logic then creates the label and prints it (by adding it to the print queue of a printer) As the title suggests this all works fine when I am debugging in visual studio. However when I deploy / install the services on my developer pc it is not working.

Update: It is not throwing an exception but I am only logging "About to send doc to printer" and not the line "Sent doc to printer" So it is hanging on the dw1.Write(fixedDoc); line

More Information: I am using .Net 4.0 in the printing project / visual studio 2013

        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);
        }
Brandon
  • 915
  • 4
  • 23
  • 44

1 Answers1

1

SOLVED ... kind of

After several hours of trying different things and reading about this, I found that it was due to my application running as me when I'm debugging but as a LOCAL SYSTEM when I have it deployed. And a local system service does not have access to network resources such as printers. Despite learning this, I then started down the path of how to make a C# service print. Well after seeing many posts (too late in the game to be very helpful)

Like this and also this one I have learned that I was going down the wrong path.

The moral of the story is, if you're reading this post you're probably not at the level of "writing your own printing DLL using the Win32 API (in C/C++ for instance), then use it from your service with P/Invoke"

The solution that did work for me was instead of running this project as a service which was started via my GUI. I have instead turned it into a process which is still started and stopped via my GUI.

The code in question is

                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe"))
            {
                Process.Start(AppDomain.CurrentDomain.BaseDirectory + "\\yourAppNameGoesHere.exe");
            }

then when the GUI is closed I run the code

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

        }
Community
  • 1
  • 1
Brandon
  • 915
  • 4
  • 23
  • 44