c# - 在 Windows 服务中,之前实例化 SciChart-Control 时 Microsoft 报告生成失败

标签 c# windows-services reporting scichart

我有一个应用程序,它使用一种方法来创建包括 SciChart-Control 的报告。这在 WPF 和控制台应用程序中都有效。 但是,当我需要在 Windows 服务中自动创建此类报告时,该服务似乎在调用报告方法时挂起。

使用过的包:

  • SciChart,版本 6 和 7 的测试
  • Microsoft.ReportViewer.WinForms,版本 10.0.40219.1

我想,如果我实际上在报告中包含 SciChart-Control 并不重要。重要的是,如果我在打印报告之前创建 SciChart-Control 的实例。 LocalReport.Render() 方法似乎会阻止进一步执行。

我创建了一个最小的工作示例,其中

  • SciChartControl 是一个空白 WPF 控件,仅包含 SciChartSurface-Element
  • Report1.rdlc 是一个空白报告,仅包含一个文本框元素。
public class Test
{
    private const string PathOfReport = @"Path\to\report\Report1.rdlc";
    private const string OutputPath = @"Path\to\output";
    private const string DeviceInfo = "<DeviceInfo><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>";

    public static void TestReport(string outputName, bool instantiateSciChart)
    {
        var t = new Thread(() =>
        {
            if (instantiateSciChart)
                _ = new SciChartTestControl();

            byte[] data = DoRenderReport();
            File.WriteAllBytes(@$"{OutputPath}\{outputName}.pdf", data);
        });
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

    private static byte[] DoRenderReport()
    {
        LocalReport localReport = new() { ReportPath = PathOfReport };
        // Blocks both at GetParameters() and localReport.Render()
        //localReport.GetParameters();
        return localReport.Render("PDF", DeviceInfo);
    }
}

我在服务的启动方法中调用它:

Test.TestReport("AutoPrintTest_Service", instantiateSciChart: true); // or false to succeed

我使用 sc create 创建并测试服务。

我希望在输出路径中生成一个 pdf 文件,但这仅在没有实例化 SciChart 的情况下才有效。否则服务将挂起。停止服务时,该服务的某个进程实际上还在运行,需要终止该进程。

也许我遗漏了一些东西,比如 Windows 服务的一些配置。 非常感谢任何帮助,非常感谢!

最佳答案

这在 Windows 服务应用程序中不起作用。服务应用程序没有交互式用户 session ,并且无法使用 GDI。引用:

Services have always run in session 0. Before Windows Vista, the first user to log on was also assigned to session 0. Now, session 0 is reserved exclusively for services and other applications not associated with an interactive user session. (The first user to log on is connected to session 1, the second user to log on is connected to session 2, and so on.) Session 0 does not support processes that interact with the user. This change means that a service cannot post or send a message to an application and an application cannot send or post a message to a service. In addition, services cannot display a user interface item such as a dialog box directly. A service can use the WTSSendMessage function to display a dialog box in another session. (https://learn.microsoft.com/en-us/windows/win32/services/service-changes-for-windows-vista)

您需要一个在当前登录用户下运行的应用程序,但如果没有用户登录到系统,则不可能有这样的用户。

您必须找到一种方法将这项工作委托(delegate)给一个在用户登录时运行的交互式应用程序,或者找到一个不依赖于 WPF/GDI 的替代报告工具。

关于c# - 在 Windows 服务中,之前实例化 SciChart-Control 时 Microsoft 报告生成失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76856830/

相关文章:

visual-studio-2012 - 如何创建 InstallShield LE 项目来安装 Windows 服务?

c# - 如何使用 Visual Studio 为 .net Windows 服务创建安装程序

java - c3p0 ComboPooledDataSource 报告最大池大小?

java - BIRT - 从 Linux 平台创建 PDF 报告

c# - 将预先存在的跟踪类与 CurveList 一起使用

c# - linq to nhibernate 在查询中重用业务逻辑

c# - 带有 UnityContainer 的 ASP MVC DependencyResolver 用于单元测试

configuration - Inno Setup Service 登录身份

mysql - Visual Studio 2015 报表设计器嵌套数据

c# - Visual Studio 数据库单元测试 - 校验和