Search code examples
c#rdlcreportviewer.net-5.net-4.8

How to render RDLC reports in a desktop application using .NET 5.0?


My application is a desktop WPF/WinForms application with around a hundred reports heavily customized, some even use code in the report itself. I am willing to lose the report viewer but I need a report renderer for PDF, Excel, and Word.


Solution

  • A desktop app requires Windows (even in .NET 5.0), Windows already have .NET Framework and will support it as long as there are Windows and your desktop app, hence you don't need a service on a server that uses .NET Framework to render your reports. The "simple" solution would be to have a console project that renders reports and uses .NET Framework. All other projects can use .NET standard or .NET 5.0. Console app will of course not have references to your .NET standard or .NET 5.0 projects. I am also having an extra reports project in .NET 5 that will be an implementation for reporting interfaces which I can replace when one day I ditch Framework completely when a full-fledged RDLC renderer appears for new .NET.

    .NET 5.0 app will request a report render by calling a console app and getting the rendered report in this way:

    .NET 5.0 app:

        var process = new Process();
        var info = new ProcessStartInfo
        {
            FileName = "SyriliumRiF.ReportsGenerator.exe",
            //Arguments = "command line arguments to your executable",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        };
        process.StartInfo = info;
        process.Start();
    
        var base64EncodedData = process.StandardOutput.ReadToEnd();
        var reportBytes = System.Convert.FromBase64String(base64EncodedData);
    

    Console .NET Framework app:

    class Program
    {
        static void Main(string[] args)
        {
            byte[] reportBytes = RenderReport(args);
            string str = Convert.ToBase64String(reportBytes);
            Console.Write(str);
        }
    }
    

    My calls to report viewer in .NET 5 app did not change and I am not generating data sources for the reports in the Framework render app.

    Inputs that the render console app receives: report name, report parameters, and files with xml serialized DataTables. In this way, I was able to detach from .NET Framework and have over 100 reports rendering in just a few days of work.