0

我是 web 服务的新手,特别是 WCF 服务,但我处理得很好。我的情况如下:我有一个调用 WCF 服务的客户端应用程序,显然是 WCF 服务。一切正常,但我现在需要打印,这就是我卡住的地方。我想要的是调用 WCF 服务来打印发票的客户端应用程序。我想为他们使用 .rdlc 报告。但我不知道如何将它们传递给客户端应用程序。正如我所说,我需要的是传递所有准备打印的信息,因此客户端应用程序无法更改任何内容。只是打印。

我需要一些关于如何实现这一目标的帮助或建议。如果没有其他办法,我可能会在客户端应用程序中创建 rdlc,但我真的想避免这种情况。

以防万一,我的客户端应用程序是一个 Winform 应用程序,我使用的是 C#、Entity framework 4 和 .NET 4。

4

1 回答 1

1

您可以让您的服务将 RDLC 转换为 PDF 并将 PDF 作为字节数组发送到您的 WCF 客户端。然后,客户端可以将字节数组保存为临时文件目录中的 .pdf 文件,然后要求操作系统启动注册用于处理 PDF 的默认应用程序(acrobat,...)。

例如,这是我在我的 MVVM 视图模型中为 WPF 客户端下载、保存和启动 PDF 报告的方法。服务器发送一个 ReportDTO,其中 Report 为字节数组,以及 FileName(扩展名为“.pdf”的报告名称):

[DataContract(Name="ReportDTO", Namespace="http://chasmx/2013/2")]
public sealed class ReportDTO
{
    /// <summary>
    /// Filename to save the report attached in <see cref="@Report"/> under.
    /// </summary>
    [DataMember]
    public String FileName { get; set; }

    /// <summary>
    /// Report as a byte array.
    /// </summary>
    [DataMember]
    public byte[] @Report { get; set; }

    /// <summary>
    /// Mimetype of the report attached in <see cref="@Report"/>. This can be used to launch the application registered for this mime type to view the saved report.
    /// </summary>
    [DataMember]
    public String MimeType { get; set; }
}

public void ViewReport()
{
    ServiceContracts.DataContract.ReportDTO report;
    String filename;

    this.Cursor = Cursors.Wait;
    try
    {
        // Download the report.
        report = _reportService.GetReport(this.SelectedReport.ID);

        // Save the report in the temporary directory
        filename = Path.Combine(Path.GetTempPath(), report.FileName);
        try
        {
            File.WriteAllBytes(filename, report.Report);
        }
        catch (Exception e)
        {
            string detailMessage = "There was a problem saving the report as '" + filename + "': " + e.Message;
            _userInteractionService.AskUser(String.Format("Saving report to local disk failed."), detailMessage, MessageBoxButtons.OK, MessageBoxImage.Error);
            return;
        }
    }
    finally
    {
        this.Cursor = null;
    }

    System.Diagnostics.Process.Start(filename); // The OS will figure out which app to open the file with.
}

这使您可以将所有报告定义保留在服务器上,并将 WCF 接口简化为客户端,以提供 PDF 字节数组。

于 2013-03-09T22:27:23.667 回答