简体   繁体   中英

Printing via ASP.NET Core-Web-Api hosted as Windows Service

I am new to ASP.NET Core-Web-Api , maybe you have some tips for me. I use ASP.NET Core 7.0 . I host my publication as a Windows service . As basis I use the demo application of the weather forecast from Microsoft . I have extended this so that I can list all local Windows printers and print a simple sting via another interface.

When I build my ASP.NET Core-Web-Api via Visual Studio I can choose to host it with " https " or " IIS Express " in debug or release mode. Via Swagger I can use all my functions even a print works.

在此处输入图像描述

Now I have my application hosted as a Windows service , also the application is accessible via Swagger . I can list my printers and also get data about my wather forecast. But I can not create a print anymore?

  • Endless loop when called but with status code 200 OK. There is also no job to be found in my printer queue.
  • Does my hosting via Visual Studio behave differently than my hosting in the Windows service?
  • My Windows-service is started via a local admin account, my printer is also usable for everyone.

在此处输入图像描述

I use the following packages

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="7.0.2" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
    <PackageReference Include="System.Drawing.Common" Version="7.0.0" />
  </ItemGroup>

I create my document with PageDocument, works fine from Visual Studio but not from Windows-service.

    PrintDocument pd = new PrintDocument();            
    pd.PrinterSettings.PrinterName = printer;            
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
    pd.Print();

For System.Printing and System.Drawing.Printing I have also seen the hints. Therefore I assume it is because of my hosting as Windows service.

Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

This issue should be the permission issue. Please find the printer service, and change the log on user.

You can use Process Monitor to verify it.

Related Link

Problem with Windows Service and.network printers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM