簡體   English   中英

使用WCF服務打印信息

[英]Printing info using a WCF service

我是Web服務的新手,特別是WCF服務,但我處理得很好。 我的情況如下:我有一個客戶端應用程序,該應用程序調用WCF服務,而調用WCF服務則更為明顯。 一切都可以正常進行,但是我現在需要打印,這就是我遇到的問題。 我想要的是客戶端應用程序,它可以調用WCF服務來打印發票。 我想為他們使用.rdlc報告。 但是我不知道如何將它們傳遞給客戶端應用程序。 正如我所說,我需要傳遞所有准備打印的信息,因此客戶端應用程序無法更改任何內容。 只是打印。

我需要一些幫助或建議以實現此目標。 如果沒有其他方法,我可以在客戶端應用程序中創建rdlc,但我確實想避免這種情況。

以防萬一,我的客戶端應用程序是Winform應用程序,我正在使用C#,實體框架4和.NET 4。

您可以讓服務將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字節數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM