简体   繁体   English

通过作为 Windows 服务托管的 ASP.NET Core-Web-Api 打印

[英]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.我是ASP.NET Core-Web-Api 的新手,也许你对我有一些提示。 I use ASP.NET Core 7.0 .我使用ASP.NET Core 7.0 I host my publication as a Windows service .我将我的出版物托管为Windows 服务 As basis I use the demo application of the weather forecast from Microsoft .作为基础,我使用Microsoft 的天气预报演示应用程序。 I have extended this so that I can list all local Windows printers and print a simple sting via another interface.我对此进行了扩展,以便我可以列出所有本地 Windows 打印机并通过另一个界面打印一个简单的字符串。

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.当我通过 Visual Studio 构建我的 ASP.NET Core-Web-Api 时,我可以选择在调试或发布模式下使用“ https ”或“ IIS Express ”来托管它。 Via Swagger I can use all my functions even a print works.通过 Swagger 我可以使用我所有的功能,即使是打印作品。

在此处输入图像描述

Now I have my application hosted as a Windows service , also the application is accessible via Swagger .现在我将我的应用程序托管为Windows 服务,也可以通过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.调用时无限循环但状态代码为 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?我通过 Visual Studio 托管的行为是否与我在 Windows 服务中托管的行为不同?
  • My Windows-service is started via a local admin account, my printer is also usable for everyone.我的 Windows 服务是通过本地管理员帐户启动的,我的打印机也可供所有人使用。

在此处输入图像描述

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.我使用 PageDocument 创建我的文档,在 Visual Studio 中工作正常,但在 Windows 服务中却不行。

    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.对于System.PrintingSystem.Drawing.Printing我也看到了提示。 Therefore I assume it is because of my hosting as Windows service.因此我认为这是因为我托管为 Windows 服务。

Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. System.Printing 命名空间中的类不支持在 Windows 服务或 ASP.NET 应用程序或服务中使用。 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.您可以使用Process Monitor来验证它。

Related Link相关链接

Problem with Windows Service and.network printers 问题 Windows 服务和网络打印机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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