【问题标题】:Printing via ASP.NET Core-Web-Api hosted as Windows Service通过作为 Windows 服务托管的 ASP.NET Core-Web-Api 进行打印
【发布时间】:2023-01-31 22:41:25
【问题描述】:

我是新来的ASP.NET Core-Web-Api,也许你对我有一些提示。我用ASP.NET 核心 7.0.我将我的出版物作为Windows 服务.作为基础,我使用 weather forecast from Microsoft 的演示应用程序。我对此进行了扩展,以便我可以列出所有本地 Windows 打印机并通过另一个界面打印一个简单的字符串。

当我通过 Visual Studio 构建 ASP.NET Core-Web-Api 时,我可以选择使用“https“ 或者 ”IIS 快递“在调试或发布模式下。通过 Swagger,我可以使用我的所有功能,即使是打印作品。

现在我将我的应用程序托管为Windows 服务, 该应用程序也可通过昂首阔步.我可以列出我的打印机,还可以获取有关我的天气预报的数据。但是我不能再打印了?

  • 调用时无限循环但状态代码为 200 OK。在我的打印机队列中也找不到作业。
  • 我通过 Visual Studio 托管的行为是否与我在 Windows 服务中托管的行为不同?
  • 我的 Windows 服务是通过本地管理员帐户启动的,我的打印机也可供所有人使用。

我使用以下包

  <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>

我使用 PageDocument 创建我的文档,在 Visual Studio 中工作正常,但在 Windows 服务中却不行。

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

为了系统.打印系统.制图.印刷我也看到了提示。因此我认为这是因为我作为 Windows 服务托管。

不支持使用 System.Printing 命名空间中的类 在 Windows 服务或 ASP.NET 应用程序或服务中。试图 从这些应用程序类型之一中使用这些类可能 产生意想不到的问题,例如服务性能下降 和运行时异常。

【问题讨论】:

    标签: printing swagger windows-services asp.net-core-webapi


    【解决方案1】:

    这个问题应该是权限问题。请找到打印机服务,并更改登录用户。

    您可以使用Process Monitor 来验证它。

    相关链接

    Problem with Windows Service and network printers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 2020-07-23
      • 2015-04-16
      相关资源
      最近更新 更多