MVC4 RDLC 停止所有其他请求
MVC4 RDLC Stops all other requests
我已经在 MVC4 网络应用程序中实现了 RDLC 报告(返回 pdf 文件字节)。每当用户请求报告时,Web 应用程序都会停止对所有用户的所有其他请求,除非呈现此报告。有些报告真的很大,大约有 100 页,因此 Web 服务器需要一段时间才能生成和呈现该报告。在此期间,Web 服务器不会处理其他请求。假设 Web 服务器正忙于呈现报告,并且在新选项卡中我尝试请求一些其他数据,除非 RDLC 完成其操作,否则它不会显示。开发机器中也会发生同样的情况。请有人建议这是我的网络应用程序中的设计缺陷还是默认行为?
有没有办法通过多线程或多任务解决这个问题?
这是报表呈现代码,以备不时之需。但我只是想让其他人分享他们的经验。
public byte[] RenderReport(PageType pageType, string ReportFormat, string ReportPath, ReportDataSource[] ReportDataModelList, out string mimeType, ReportParameter[] ReportParameters)
{
LocalReport lr = new LocalReport();
string path = Path.Combine(ReportPath);
lr.ReportPath = path;
foreach (ReportDataSource rds in ReportDataModelList)
{
lr.DataSources.Add(rds);
}
lr.EnableExternalImages = true;
lr.SetParameters(new ReportParameter("PrintedBy", PrintedBy));
foreach (ReportParameter rp in ReportParameters)
lr.SetParameters(rp);
string encoding;
string fileNameExtension;
string pageWidthHeight;
if (pageType == PageType.Portrait)
pageWidthHeight =
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>";
else if (pageType == PageType.Landscape)
pageWidthHeight =
" <PageWidth>11in</PageWidth>" +
" <PageHeight>8.5in</PageHeight>";
else
pageWidthHeight =
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>";
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>" + ReportFormat + "</OutputFormat>" +
pageWidthHeight +
" <MarginTop>0.3in</MarginTop>" +
" <MarginLeft>0.3in</MarginLeft>" +
" <MarginRight>0.3in</MarginRight>" +
" <MarginBottom>0.3in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = lr.Render(
ReportFormat,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
return renderedBytes;
}
谢谢。
我怀疑您观察到的问题与以下关于 ASP.NET sessions
的事实有关(我想您正在使用它):
Access to ASP.NET session state is exclusive per session, which means
that if two different users make concurrent requests, access to each
separate session is granted concurrently. However, if two concurrent
requests are made for the same session (by using the same SessionID
value), the first request gets exclusive access to the session
information. The second request executes only after the first request
is finished. [...]
并且由于浏览器选项卡 ASP.NET 会话,您基本上是在尝试从同一会话发出并发请求。
您可以简单地从您的应用程序中禁用会话状态,您将看到您可以发出并发请求:
<sessionState mode="Off" />
显然这只是观察到的行为的原因。在所有情况下,您都应该避免在 ASP.NET 应用程序中进行长时间的 运行 操作。这些操作会阻塞工作线程,从而限制应用程序的服务能力。理想情况下,那些长 运行 的操作应该从您的 ASP.NET 应用程序中卸载。例如,您可以有一个 Windows 服务负责生成这些报告,而 ASP.NET 应用程序将只安排报告生成,而该服务将执行实际工作。
我已经在 MVC4 网络应用程序中实现了 RDLC 报告(返回 pdf 文件字节)。每当用户请求报告时,Web 应用程序都会停止对所有用户的所有其他请求,除非呈现此报告。有些报告真的很大,大约有 100 页,因此 Web 服务器需要一段时间才能生成和呈现该报告。在此期间,Web 服务器不会处理其他请求。假设 Web 服务器正忙于呈现报告,并且在新选项卡中我尝试请求一些其他数据,除非 RDLC 完成其操作,否则它不会显示。开发机器中也会发生同样的情况。请有人建议这是我的网络应用程序中的设计缺陷还是默认行为?
有没有办法通过多线程或多任务解决这个问题?
这是报表呈现代码,以备不时之需。但我只是想让其他人分享他们的经验。
public byte[] RenderReport(PageType pageType, string ReportFormat, string ReportPath, ReportDataSource[] ReportDataModelList, out string mimeType, ReportParameter[] ReportParameters)
{
LocalReport lr = new LocalReport();
string path = Path.Combine(ReportPath);
lr.ReportPath = path;
foreach (ReportDataSource rds in ReportDataModelList)
{
lr.DataSources.Add(rds);
}
lr.EnableExternalImages = true;
lr.SetParameters(new ReportParameter("PrintedBy", PrintedBy));
foreach (ReportParameter rp in ReportParameters)
lr.SetParameters(rp);
string encoding;
string fileNameExtension;
string pageWidthHeight;
if (pageType == PageType.Portrait)
pageWidthHeight =
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>";
else if (pageType == PageType.Landscape)
pageWidthHeight =
" <PageWidth>11in</PageWidth>" +
" <PageHeight>8.5in</PageHeight>";
else
pageWidthHeight =
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>";
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>" + ReportFormat + "</OutputFormat>" +
pageWidthHeight +
" <MarginTop>0.3in</MarginTop>" +
" <MarginLeft>0.3in</MarginLeft>" +
" <MarginRight>0.3in</MarginRight>" +
" <MarginBottom>0.3in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = lr.Render(
ReportFormat,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
return renderedBytes;
}
谢谢。
我怀疑您观察到的问题与以下关于 ASP.NET sessions
的事实有关(我想您正在使用它):
Access to ASP.NET session state is exclusive per session, which means that if two different users make concurrent requests, access to each separate session is granted concurrently. However, if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. The second request executes only after the first request is finished. [...]
并且由于浏览器选项卡 ASP.NET 会话,您基本上是在尝试从同一会话发出并发请求。
您可以简单地从您的应用程序中禁用会话状态,您将看到您可以发出并发请求:
<sessionState mode="Off" />
显然这只是观察到的行为的原因。在所有情况下,您都应该避免在 ASP.NET 应用程序中进行长时间的 运行 操作。这些操作会阻塞工作线程,从而限制应用程序的服务能力。理想情况下,那些长 运行 的操作应该从您的 ASP.NET 应用程序中卸载。例如,您可以有一个 Windows 服务负责生成这些报告,而 ASP.NET 应用程序将只安排报告生成,而该服务将执行实际工作。