How to call report server in .net 5

Even if we are using the last Microsoft releases we also need from time to time to call old or unsupported features. For example, how to call a report (rldc) from a report server in a new .net 5 application.

First of all, I must say that I’m using a nuget package for this, AspNetCore.ReportViewer, developed by amh1979. This is a wrapper over Report Server Execution Service (ReportExecution2005.asmx).

So, let’s start the implementation. Let’s say we have one empty console application. First of all using Package Manager let’s install the nuget package described above.

Install-Package AspNetCore.ReportViewer -Version 3.0.0.200103

After that, we need some private constats in application for ReportServer path, executed report path and the credentials used to connect to report server.

        private const string ReportExecution2005EndPointUrl = "https://YourReportServer/ReportExecution2005.asmx?wsdl";
        private const string ReportServerUserName = "YourUserName";
        private const string ReportServerPassword = "YourPassword";
        private const string ReportServerDomain = "YourDomain";
        private const string ReportPath = "YouReportPath";

Now, it’s time to set the report settings.

        private static ReportSettings SetReportSettings()
        {
            var reportSettings = new ReportSettings
            {
                Credential = new NetworkCredential(ReportServerUserName, ReportServerPassword, ReportServerDomain),
                ReportServer = ReportExecution2005EndPointUrl
            };

            return reportSettings;
        }

Let’s create a method for calling our specific report, in my case the invoice report. Here we have multiple Execute types and render types. For me the Export as PDF is what I need for this report. In your reports, you can have multiple reports, but in my case it was only one parameter.

        private static ReportRequest SetInvoiceReportRequest()
        {
            var reportRequest = new ReportRequest
            {
                ExecuteType = ReportExecuteType.Export,
                RenderType = ReportRenderType.Pdf,
                Path = ReportPath
            };

            reportRequest.Parameters.Add("OrderId", "1234");

            return reportRequest;
        }

And now it’s time to execute the report.

        private static ReportResponse ExecuteReport()
        {
            var reportViewer = new ReportViewer(SetReportSettings());

            var reportRequest = SetInvoiceReportRequest();

            var response = reportViewer.Execute(reportRequest);

            return response;
        }

In our Main method we’ll have only to call the ExecuteReport method, save the PDF on local PC and check the report. Status = 0 means OK 🙂

        static void Main(string[] args)
        {
            var response = ExecuteReport();

            Console.WriteLine(response.Message);

            if (response.Status == 0)
            {
              System.IO.File.WriteAllBytes("C:\\Integrations\\Report.pdf", response.Data.Stream);
            }
        }

This is a easy and simple way to call SSRS reports in .net 5 applications.