尝试呈现和保存多个SSRS报告时遇到错误

时间:2016-07-20 04:40:45

标签: c# reporting-services async-await

我需要从SSRS生成大量报告并将它们存储在数据库中。一次执行一个操作太慢,所以我尝试利用asyncawait。这是我尝试做这项工作的地方。 xxx包含一组报告名称以及这些报告的参数。

Task.WaitAll(xxx.Select(x => CreateSaveReportTask(x.parameterValue, x.rType, x.batchReportParameter, conn, 
                                                  ssrsUser, ssrsPassword, ssrsDomain, ssrsServerUrl, ssrsVirtualPath)).ToArray());

这是返回任务的方法

private async Task CreateSaveReportTask(int parameterValue, HabReportTypeLkup rType, BatchReportConfig batchReportParameter, SqlConnection conn, 
    string user, string password, string domain, string reportServerUrl, string reportServerVirtualPath)
{
    await Task.Run(() =>
        {
            Log.Info("Saving " + parameterValue + " report of type " + rType.Value);
            var parameters = new[] { new ParameterValue { Name = batchReportParameter.ParameterName, Value = parameterValue.ToString() } };

            var rs = new ReportExecutionService();
            rs.Credentials = new NetworkCredential(user, password, domain);
            rs.Url = String.Format("{0}/{1}/{2}", reportServerUrl, reportServerVirtualPath, "ReportExecution2005.asmx");

            // Render arguments
            byte[] rpt = null;
            string reportPath = rType.ReportPath;
            string format = rType.Format;
            string historyID = null;
            string devInfo = null;

            string encoding, mimeType, extension;
            Warning[] warnings = null;
            string[] streamIDs = null;

            var execInfo = new ExecutionInfo();
            var execHeader = new ExecutionHeader();

            try
            {
                rs.ExecutionHeaderValue = execHeader;
                rs.LoadReport(reportPath, historyID);
                rs.SetExecutionParameters(parameters, "en-us");

                rpt = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
                Log.Info("SUCCESS RETRIEVING " + rpt.Count() + " bytes for " + parameterValue + " report of type " + rType.Id + "(" + rType.Value + ")");
            }
            catch (Exception ex)
            {
                Log.Error("ERROR RETRIEVING " + parameterValue + " report of type " + rType.Id + "(" + rType.Value + ")");
                Log.Error(ex.Message);
                throw;
            }
            try
            {
                using (var cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "INSERT INTO habAdmin.HAB_ReportDocument (ReportId, DateLastModified, ReportDocument, ReportTypeId) VALUES (@reportId, @dateLastModified, @report, @reportType)";
                    cmd.Parameters.AddRange(new IDbDataParameter[]
                    {
                        new SqlParameter("reportId", parameterValue),
                        new SqlParameter("dateLastModified", DateTime.Now),
                        new SqlParameter("report", rpt),
                        new SqlParameter("reportType", rType.Id)
                    });
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();

                    Log.Info("SUCCESS SAVING " + parameterValue + " report of type " + rType.Id + "(" + rType.Value + ")");
                }
            }
            catch (Exception ex)
            {
                Log.Error("ERROR SAVING " + parameterValue + " report of type " + rType.Id + "(" + rType.Value + ")");
                Log.Error(ex.Message);
                throw;
            }
        });
}

它开始工作,但很快,它开始抛出异常,如下:

//The underlying connection was closed: An unexpected error occurred on a receive.
//The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
//The request was aborted: The request was canceled.

如果我决定让它运行一段时间,并在出现错误时重试,它最终会回到稍微恢复报告,然后再开始抛出异常。

我猜服务器对我施加某种限制,但我无法检查那里的设置。有没有办法减缓我正在制作的请求数量?

0 个答案:

没有答案