0

I am trying to print from a Windows Docker Container where I have a .NET 6 ASP.NET Api hosted. Here is my code:

private readonly PrintDocument _document;

// Some code here

public async Task PrintAsync(PrintInput input)
{
   // Some code here
   _document.PrinterSettings.PrinterName = "\\server_name\shared_printer_name";

   // Some code here
   _document.PrintPage += _document_PrintPage;

   _document.Print();
          
}

My docker container is running the following images:

FROM mcr.microsoft.com/windows/servercore:ltsc2019 AS base 
...
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
...
ENTRYPOINT ["dotnet", "App.Web.Host.dll"]

The error message that I am getting is:

The RPC server is unavailable

The host is a Windows 2019 Server and has the Service Spooler Running. The Windows container has not have a Spooler Service Running.

I was having the message:

Windows cannot connect to the printer, Operation failed with error 0x00000bcb

Then I tried: Fix Network Printer error, but it didn't help.

Also, I had tried this one: Print Spooler in Windows Containers. No success as well.

I am wondering if it is not possible to use: PrintDocument inside an application running under a Windows Docker Container. What do you guys think?

Back
  • 35
  • 11
Flavio Francisco
  • 755
  • 1
  • 8
  • 21

1 Answers1

1

I solved it creating a Windows Service that runs printer jobs (using Hangfire) on the host server. With that I just installed the printers needed directly on the host and I call/ trigger the jobs from the ASP.NET Core API running in our docker container.

This is an example of how I call/ trigger the printer job:

    public async Task PrintAsync(PrintRequest input)
    {            
        BackgroundJob.Enqueue<IPrintLabelJob>(x => x.DoWork(JobCancellationToken.Null, null, ObjectMapper.Map<PrintInput>(input)));
    }

This is the job that prints:

public class PrintLabelJob : IPrintLabelJob
{
    private readonly IPrinterManager _printerManager;

    public PrintLabelJob(IPrinterManager printerManager)
    {
        _printerManager = printerManager;
    }

    [UnitOfWork]
    public void DoWork(IJobCancellationToken cancellationToken, PerformContext context, PrintInput input)
    {
        AsyncHelper.RunSync(() => _printerManager.PrintAsync(input));
    }
}

For your info I am using: Hangfire + AspNetZero (AspNetBoilerPlate).

Flavio Francisco
  • 755
  • 1
  • 8
  • 21