Example #1
0
        public async Task <byte[]> RenderReport(string report, ParameterValue[] parameters, string exportFormat = "PDF")
        {
            var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);

            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
            binding.MaxReceivedMessageSize = 100485760;

            var rsExec = new ReportExecutionServiceSoapClient(binding, new EndpointAddress(url));

            var clientCredentials = new NetworkCredential(userName, password);

            rsExec.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            rsExec.ClientCredentials.Windows.ClientCredential          = clientCredentials;

            TrustedUserHeader trustedUserHeader = new TrustedUserHeader();

            LoadReportResponse taskLoadReport = null;
            string             historyID      = null;

            taskLoadReport = await rsExec.LoadReportAsync(trustedUserHeader, report, historyID);

            ExecutionHeader executionHeader = new ExecutionHeader
            {
                ExecutionID = taskLoadReport.executionInfo.ExecutionID
            };

            await rsExec.SetExecutionParametersAsync(executionHeader, trustedUserHeader, parameters, null);

            const string   deviceInfo = @"<DeviceInfo><Toolbar>False</Toolbar><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>";
            RenderResponse response   = await rsExec.RenderAsync(new RenderRequest(executionHeader, trustedUserHeader, exportFormat ?? "PDF", deviceInfo));

            return(response.Result);
        }
        public async static Task <byte[]> GenerateSSRSReport(string County)
        {
            var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            //binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
            //binding.MaxReceivedMessageSize = 10485760; //10MB limit

            //Create the execution service SOAP Client
            var rsExec = new ReportExecutionServiceSoapClient(binding, new EndpointAddress(SSRSReportExecutionUrl));

            //Setup access credentials.
            var clientCredentials = new NetworkCredential(SSRSUsername, SSRSPassword, SSRSDomain);

            if (rsExec.ClientCredentials != null)
            {
                rsExec.ClientCredentials.Windows.AllowedImpersonationLevel =
                    System.Security.Principal.TokenImpersonationLevel.Impersonation;
                rsExec.ClientCredentials.Windows.ClientCredential = clientCredentials;
            }

            //This handles the problem of "Missing session identifier"
            rsExec.Endpoint.EndpointBehaviors.Add(new ReportingServicesEndpointBehavior());

            await rsExec.LoadReportAsync(null, "/" + SSRSFolderPath + "/" + ReportName, null);

            //TODO: determine parameters
            //Set the parameters asked for by the report
            ReportExecutionService.ParameterValue[] reportParam = new ReportExecutionService.ParameterValue[1];

            reportParam[0]       = new ReportExecutionService.ParameterValue();
            reportParam[0].Name  = "County";
            reportParam[0].Value = County.ToString();

            await rsExec.SetExecutionParametersAsync(null, null, reportParam, "en-us");

            //run the report
            const string deviceInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
            var          response   = await rsExec.RenderAsync(new RenderRequest(null, null, "PDF", deviceInfo));

            //spit out the result
            var byteResults = response.Result;

            return(byteResults);
        }
Example #3
0
        // https://docs.microsoft.com/en-us/dotnet/api/reportexecution2005.reportexecutionservice.render?view=sqlserver-2016
        // https://docs.microsoft.com/en-us/sql/reporting-services/customize-rendering-extension-parameters-in-rsreportserver-config?view=sql-server-ver15
        // https://medium.com/@yates.programmer/generating-an-ssrs-report-using-wcf-from-net-core-application-730e22886da3
        public async Task <byte[]> Render(string reportPath, string reportType = "PDF", string deviceInfo = null)
        {
            if (string.IsNullOrEmpty(reportPath))
            {
                throw new Exception("Missing required reportPath parameter");
            }

            reportType = reportType.ToUpper().Trim();
            switch (reportType)
            {
            case "PDF":
            case "EXCEL":
            case "WORD":
            case "XML":
            case "CSV":
            case "IMAGE":
            case "HTML4.0":
            case "MHTML":
                break;

            default:
                throw new Exception("Invalid reportType: " + reportType);
            }

            TrustedUserHeader  trustedHeader = new TrustedUserHeader();
            LoadReportResponse loadReponse   = await LoadReport(RSExecutionClient, trustedHeader, reportPath);

            if (ReportParameters.Count > 0)
            {
                await RSExecutionClient.SetExecutionParametersAsync(
                    loadReponse.ExecutionHeader,
                    trustedHeader,
                    ReportParameters.ToArray(),
                    "en-US"
                    );
            }

            var            renderRequest = new RenderRequest(loadReponse.ExecutionHeader, trustedHeader, reportType, deviceInfo);
            RenderResponse response      = await RSExecutionClient.RenderAsync(renderRequest);

            if (response.Warnings != null)
            {
                foreach (ReportExecution.Warning warning in response.Warnings)
                {
                    Warnings.Add(
                        new KeyValuePair <string, string>(
                            warning.Code,
                            String.Format(
                                "Severity: {0} Object: {1} Message: {2}",
                                warning.Severity,
                                warning.ObjectName,
                                warning.Message
                                )
                            )
                        );
                }
            }

            Extension = response.Extension;
            MimeType  = response.MimeType;
            Encoding  = response.Encoding;
            StreamIds = response.StreamIds;

            return(response.Result);
        }
        public async Task <byte[]> RenderReport(string reportPath, IDictionary <string, string> parameters, ExportFormat exportFormat, string fileName)
        {
            byte[] fileContent = null;
            try
            {
                //My binding setup, since ASP.NET Core apps don't use a web.config file
                var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
                binding.MaxReceivedMessageSize = 10485760; //I wanted a 10MB size limit on response to allow for larger PDFs

                string _reportingServicesUrl = _config["WCFExternalServices:ReportingService:EndpointAddress"];

                //Create the execution service SOAP Client
                var rsExec = new ReportExecutionServiceSoapClient(binding, new EndpointAddress(_reportingServicesUrl));

                //Setup access credentials. I use windows credentials, yours may differ
                var clientCredentials = new NetworkCredential();
                //rsExec.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
                rsExec.ClientCredentials.Windows.ClientCredential = clientCredentials;

                //This handles the problem of "Missing session identifier"
                rsExec.Endpoint.EndpointBehaviors.Add(new ReportingServicesEndpointBehavior());

                string            historyID         = null;
                TrustedUserHeader trustedUserHeader = new TrustedUserHeader();
                ExecutionHeader   execHeader        = new ExecutionHeader();

                trustedUserHeader.UserName = clientCredentials.UserName;

                //Load the report
                var taskLoadReport = await rsExec.LoadReportAsync(trustedUserHeader, reportPath, historyID);

                // Fixed the exception of "session identifier is missing".
                execHeader.ExecutionID = taskLoadReport.executionInfo.ExecutionID;

                //
                //Set the parameteres asked for by the report
                //

                string lang = "";

                if (fileName == "LoanStatement" || fileName == "Payment_order" || fileName == "CreditLineStatement")
                {
                    if (Convert.ToInt32(parameters["lang_id"]) == 1)
                    {
                        lang = "hy-am";
                    }
                    else
                    {
                        lang = "en-us";
                    }
                }
                else
                {
                    lang = "en-us";
                }

                ParameterValue[] reportParameters = null;
                if (parameters != null && parameters.Count > 0)
                {
                    reportParameters = taskLoadReport.executionInfo.Parameters.Where(x => parameters.ContainsKey(x.Name)).Select(x => new ParameterValue()
                    {
                        Name = x.Name, Value = parameters[x.Name] != null ? parameters[x.Name].ToString() : null
                    }).ToArray();
                }
                await rsExec.SetExecutionParametersAsync(execHeader, trustedUserHeader, reportParameters, lang);

                //run the report
                string format = GetExportFormatString(exportFormat);
                // run the report
                const string deviceInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

                var response = await rsExec.RenderAsync(new RenderRequest(execHeader, trustedUserHeader, format, deviceInfo));

                //spit out the result
                return(response.Result);
            }
            catch (Exception ex)
            {
                // System.Web.HttpContext.Current.Response.BufferOutput = true;
                //   System.Web.HttpContext.Current.Response.TrySkipIisCustomErrors = true;
                //  System.Web.HttpContext.Current.Response.StatusCode = 422;
                //  System.Web.HttpContext.Current.Response.StatusDescription = Utils.ConvertAnsiToUnicode(ex.Message);
                throw ex;
            }
            return(fileContent);
        }
        private async Task <byte[]> RenderReport(string reportName, IDictionary <string, object> parameters, string languageCode, string exportFormat)
        {
            const string SSRSUsername           = "******";
            const string SSRSDomain             = "DESKTOP-FFAAKTS";
            const string SSRSPassword           = "******";
            const string SSRSReportExecutionUrl = "http://desktop-ffaakts/ReportServer/ReportExecution2005.asmx";
            const string SSRSFolderPath         = "DemoReports";
            string       reportPath             = string.Format("{0}{1}", SSRSFolderPath, reportName);

            //
            // Binding setup, since ASP.NET Core apps don't use a web.config file
            var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

            //binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
            binding.MaxReceivedMessageSize = 999999999; //10MB limit
            binding.MaxBufferSize          = 999999999;
            binding.OpenTimeout            = new TimeSpan(0, 90, 0);
            binding.CloseTimeout           = new TimeSpan(0, 90, 0);
            binding.SendTimeout            = new TimeSpan(0, 90, 0);
            binding.ReceiveTimeout         = new TimeSpan(0, 90, 0);

            //Create the execution service SOAP Client
            ReportExecutionServiceSoapClient reportClient = new ReportExecutionServiceSoapClient(binding, new EndpointAddress(SSRSReportExecutionUrl));

            //Setup access credentials. Here use windows credentials.
            var clientCredentials = new NetworkCredential(SSRSUsername, SSRSPassword, SSRSDomain);

            reportClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            reportClient.ClientCredentials.Windows.ClientCredential          = clientCredentials;

            //This handles the problem of "Missing session identifier"
            reportClient.Endpoint.EndpointBehaviors.Add(new ReportingServiceEndPointBehavior());

            //string historyID = null;
            //TrustedUserHeader trustedUserHeader = new TrustedUserHeader();
            //ExecutionHeader execHeader = new ExecutionHeader();

            //trustedUserHeader.UserName = clientCredentials.UserName;

            //
            // Load the report
            //
            var taskLoadReport = await reportClient.LoadReportAsync(null, "/" + SSRSFolderPath + "/" + reportName, null);

            // Fixed the exception of "session identifier is missing".
            reportClient.Endpoint.EndpointBehaviors.Add(new ReportingServicesEndpointBehavior());

            //
            //Set the parameteres asked for by the report
            //
            ParameterValue[] reportParameters = null;
            if (parameters != null && parameters.Count > 0)
            {
                reportParameters = taskLoadReport.executionInfo.Parameters.Where(x => parameters.ContainsKey(x.Name)).Select(x => new ParameterValue()
                {
                    Name = x.Name, Value = parameters[x.Name].ToString()
                }).ToArray();
            }

            await reportClient.SetExecutionParametersAsync(null, null, reportParameters, languageCode);

            // run the report
            const string deviceInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

            var response = await reportClient.RenderAsync(new RenderRequest(null, null, exportFormat ?? exportFormat, deviceInfo));

            //spit out the result
            return(response.Result);
        }